// src/localization/LanguageProvider.tsx
import { createContext, useContext, useEffect as useEffect2, useState as useState2 } from "react";

// src/hooks/useLocalStorage.ts
import { useCallback, useEffect, useState } from "react";

// src/util/storage.ts
var StorageService = class {
  // this seems to be a bug in eslint as 'paramter-properties' is a special syntax of typescript
  constructor(storage) {
    this.storage = storage;
  }
  get(key) {
    const value = this.storage.getItem(key);
    if (value === null) {
      return null;
    }
    return JSON.parse(value);
  }
  set(key, value) {
    this.storage.setItem(key, JSON.stringify(value));
  }
  delete(key) {
    this.storage.removeItem(key);
  }
  deleteAll() {
    this.storage.clear();
  }
};
var LocalStorageService = class extends StorageService {
  constructor() {
    super(window.localStorage);
  }
};

// src/hooks/useLocalStorage.ts
var useLocalStorage = (key, initValue) => {
  const get = useCallback(() => {
    if (typeof window === "undefined") {
      return initValue;
    }
    const storageService = new LocalStorageService();
    const value = storageService.get(key);
    return value || initValue;
  }, [initValue, key]);
  const [storedValue, setStoredValue] = useState(get);
  const setValue = useCallback((value) => {
    const newValue = value instanceof Function ? value(storedValue) : value;
    const storageService = new LocalStorageService();
    storageService.set(key, value);
    setStoredValue(newValue);
  }, [storedValue, setStoredValue, key]);
  useEffect(() => {
    setStoredValue(get());
  }, []);
  return [storedValue, setValue];
};

// src/localization/util.ts
var languages = ["en", "de"];
var languagesLocalNames = {
  en: "English",
  de: "Deutsch"
};
var DEFAULT_LANGUAGE = "en";
var LanguageUtil = {
  languages,
  DEFAULT_LANGUAGE,
  languagesLocalNames
};

// src/localization/LanguageProvider.tsx
import { jsx } from "react/jsx-runtime";
var LanguageContext = createContext({
  language: LanguageUtil.DEFAULT_LANGUAGE,
  setLanguage: (v) => v
});
var useLanguage = () => useContext(LanguageContext);
var useLocale = (overWriteLanguage) => {
  const { language } = useLanguage();
  const mapping = {
    en: "en-US",
    de: "de-DE"
  };
  return mapping[overWriteLanguage ?? language];
};
var LanguageProvider = ({ initialLanguage, children }) => {
  const [language, setLanguage] = useState2(initialLanguage ?? LanguageUtil.DEFAULT_LANGUAGE);
  const [storedLanguage, setStoredLanguage] = useLocalStorage("language", initialLanguage ?? LanguageUtil.DEFAULT_LANGUAGE);
  useEffect2(() => {
    if (language !== initialLanguage && initialLanguage) {
      console.warn("LanguageProvider initial state changed: Prefer using languageProvider's setLanguage instead");
      setLanguage(initialLanguage);
    }
  }, [initialLanguage]);
  useEffect2(() => {
    setStoredLanguage(language);
  }, [language, setStoredLanguage]);
  useEffect2(() => {
    if (storedLanguage !== null) {
      setLanguage(storedLanguage);
      return;
    }
    const LanguageToTestAgainst = Object.values(LanguageUtil.languages);
    const matchingBrowserLanguage = window.navigator.languages.map((language2) => LanguageToTestAgainst.find((test) => language2 === test || language2.split("-")[0] === test)).filter((entry) => entry !== void 0);
    if (matchingBrowserLanguage.length === 0) return;
    const firstMatch = matchingBrowserLanguage[0];
    setLanguage(firstMatch);
  }, []);
  return /* @__PURE__ */ jsx(LanguageContext.Provider, { value: {
    language,
    setLanguage
  }, children });
};
export {
  LanguageContext,
  LanguageProvider,
  useLanguage,
  useLocale
};
//# sourceMappingURL=LanguageProvider.mjs.map