const supported = new Set(["ko", "en"]);
const preferred = navigator.language?.toLowerCase().startsWith("ko") ? "ko" : "en";
const saved = localStorage.getItem("crm-site-language");
const initial = supported.has(saved) ? saved : preferred;

function setLanguage(language) {
  if (!supported.has(language)) return;
  document.documentElement.dataset.lang = language;
  document.documentElement.lang = language;
  localStorage.setItem("crm-site-language", language);
  for (const button of document.querySelectorAll("[data-set-lang]")) {
    button.setAttribute("aria-pressed", String(button.dataset.setLang === language));
  }
}

for (const button of document.querySelectorAll("[data-set-lang]")) {
  button.addEventListener("click", () => setLanguage(button.dataset.setLang));
}
setLanguage(initial);
