import { makeAutoObservable } from "mobx";
import { Language } from "../SightsStore";

class LanguageStore {
  language: Language = "ru";

  constructor() {
    makeAutoObservable(this);

    if (typeof window !== "undefined") {
      const storedLanguage = window.localStorage.getItem("appLanguage");
      if (
        storedLanguage &&
        ["ru", "en", "zh"].includes(storedLanguage.toLowerCase())
      ) {
        this.language = storedLanguage.toLowerCase() as Language;
      }
    }
  }

  setLanguage = (language: Language) => {
    this.language = language;

    if (typeof window !== "undefined") {
      window.localStorage.setItem("appLanguage", language);
    }
  };
}

export const languageStore = new LanguageStore();
