var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/localization/LanguageProvider.tsx
var LanguageProvider_exports = {};
__export(LanguageProvider_exports, {
  LanguageContext: () => LanguageContext,
  LanguageProvider: () => LanguageProvider,
  useLanguage: () => useLanguage,
  useLocale: () => useLocale
});
module.exports = __toCommonJS(LanguageProvider_exports);
var import_react2 = require("react");

// src/hooks/useLocalStorage.ts
var import_react = require("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 = (0, import_react.useCallback)(() => {
    if (typeof window === "undefined") {
      return initValue;
    }
    const storageService = new LocalStorageService();
    const value = storageService.get(key);
    return value || initValue;
  }, [initValue, key]);
  const [storedValue, setStoredValue] = (0, import_react.useState)(get);
  const setValue = (0, import_react.useCallback)((value) => {
    const newValue = value instanceof Function ? value(storedValue) : value;
    const storageService = new LocalStorageService();
    storageService.set(key, value);
    setStoredValue(newValue);
  }, [storedValue, setStoredValue, key]);
  (0, import_react.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
var import_jsx_runtime = require("react/jsx-runtime");
var LanguageContext = (0, import_react2.createContext)({
  language: LanguageUtil.DEFAULT_LANGUAGE,
  setLanguage: (v) => v
});
var useLanguage = () => (0, import_react2.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] = (0, import_react2.useState)(initialLanguage ?? LanguageUtil.DEFAULT_LANGUAGE);
  const [storedLanguage, setStoredLanguage] = useLocalStorage("language", initialLanguage ?? LanguageUtil.DEFAULT_LANGUAGE);
  (0, import_react2.useEffect)(() => {
    if (language !== initialLanguage && initialLanguage) {
      console.warn("LanguageProvider initial state changed: Prefer using languageProvider's setLanguage instead");
      setLanguage(initialLanguage);
    }
  }, [initialLanguage]);
  (0, import_react2.useEffect)(() => {
    setStoredLanguage(language);
  }, [language, setStoredLanguage]);
  (0, import_react2.useEffect)(() => {
    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__ */ (0, import_jsx_runtime.jsx)(LanguageContext.Provider, { value: {
    language,
    setLanguage
  }, children });
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  LanguageContext,
  LanguageProvider,
  useLanguage,
  useLocale
});
//# sourceMappingURL=LanguageProvider.js.map