// import i18next from "../i18n/config";
// import { Locale, I18NEXT_LOCALE_KEY } from "./constants";
//
// const ACCESS_COOKIE = "expiration-access";
// const REFRESH_COOKIE = "expiration-refresh";
//
// function hasCookie(value: string): boolean {
//   return document.cookie.includes(value);
// }
//
// export function getI18nextStorageLocale(): Locale | null {
//   const i18nextLng = localStorage.getItem(I18NEXT_LOCALE_KEY);
//
//   if (!i18nextLng) {
//     return null;
//   }
//
//   return i18nextLng as Locale;
// }
//
// export function getNavigatorLocale(): Locale {
//   const locale = window.navigator.language.slice(0, 2);
//   const hasLocale = Object.values(Locale).includes(locale as Locale);
//
//   if (hasLocale) {
//     return locale as Locale;
//   }
//
//   return Locale.EN;
// }
//
// export function getInitialLocale(): Locale {
//   if (hasCookie(ACCESS_COOKIE) || hasCookie(REFRESH_COOKIE)) {
//     const i18nextLng = getI18nextStorageLocale();
//
//     if (i18nextLng) {
//       return i18nextLng;
//     }
//   }
//
//   return getNavigatorLocale();
// }
//
// export function hasResourceBundles(
//   locale: Locale,
//   namespaces: string[]
// ): boolean {
//   return namespaces.every((ns) => i18next.hasResourceBundle(locale, ns));
// }

import i18next from 'core/i18n/config';
import { Locale, I18NEXT_LOCALE_KEY } from './constants';
export const ACCESS_COOKIE = 'expiration-access';
export const REFRESH_COOKIE = 'expiration-refresh';

export function getI18nextStorageLocale(): Locale | null {
  const i18nextLng = localStorage.getItem(I18NEXT_LOCALE_KEY);

  if (!i18nextLng) {
    return null;
  }

  return i18nextLng as Locale;
}
function hasCookie(value: string): boolean {
  return document.cookie.includes(value);
}
export function getNavigatorLocale(): Locale {
  const locale = window.navigator.language.slice(0, 2);
  const hasLocale = Object.values(Locale).includes(locale as Locale);

  if (hasLocale) {
    return locale as Locale;
  }

  return Locale.EN;
}

export function getInitialLocale(): Locale {
  if (hasCookie(ACCESS_COOKIE) || hasCookie(REFRESH_COOKIE)) {
    const i18nextLng = getI18nextStorageLocale();

    if (i18nextLng) {
      return i18nextLng;
    }
  }

  return getNavigatorLocale();
}

export function hasResourceBundles(
  locale: Locale,
  namespaces: string[],
): boolean {
  return namespaces.every((ns) => i18next.hasResourceBundle(locale, ns));
}

export const getCurrentLocale = (): string => i18next.language;
