import { DEFAULT_LOCALE, LOCALE_STORAGE_KEY, type Locale } from "../../../common/core/i18n";

function isLocale(value: string): value is Locale {
  return value === "ko" || value === "en";
}

export function getAdminLocale(): Locale {
  if (typeof window === "undefined") {
    return DEFAULT_LOCALE;
  }

  const stored = window.localStorage.getItem(LOCALE_STORAGE_KEY);
  if (stored && isLocale(stored)) {
    return stored;
  }

  const pathLocale = window.location.pathname.split("/")[1];
  if (pathLocale && isLocale(pathLocale)) {
    return pathLocale;
  }

  const browserLang = window.navigator.language.toLowerCase();
  if (browserLang.startsWith("ko")) {
    return "ko";
  }

  return DEFAULT_LOCALE;
}

export function getAdminDateLocale() {
  return getAdminLocale() === "ko" ? "ko-KR" : "en-US";
}
