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/i18n/LocaleProvider.tsx
var LocaleProvider_exports = {};
__export(LocaleProvider_exports, {
  LocaleContext: () => LocaleContext,
  LocaleProvider: () => LocaleProvider,
  useLanguage: () => useLanguage,
  useLocale: () => useLocale
});
module.exports = __toCommonJS(LocaleProvider_exports);
var import_react2 = require("react");

// src/hooks/useLocalStorage.ts
var import_react = require("react");

// src/utils/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/utils/resolveSetState.ts
function resolveSetState(action, prev) {
  return typeof action === "function" ? action(prev) : action;
}

// src/hooks/useLocalStorage.ts
var useLocalStorage = (key, backupValue) => {
  const get = (0, import_react.useCallback)(() => {
    if (typeof window === "undefined") {
      return backupValue;
    }
    const storageService = new LocalStorageService();
    try {
      const value = storageService.get(key);
      return value || backupValue;
    } catch (_) {
      console.warn("useLocalStorage: Error while reading the stored value. Make sure your typing is correct.");
      storageService.delete(key);
      console.info(`useLocalStorage: deleted erroneous value for key: ${key}`);
      return backupValue;
    }
  }, [backupValue, key]);
  const [storedValue, setStoredValue] = (0, import_react.useState)(get);
  const setValue = (0, import_react.useCallback)((action) => {
    const newValue = resolveSetState(action, storedValue);
    const storageService = new LocalStorageService();
    storageService.set(key, newValue);
    setStoredValue(newValue);
  }, [storedValue, setStoredValue, key]);
  const deleteValue = () => {
    const storageService = new LocalStorageService();
    storageService.delete(key);
    setStoredValue(backupValue);
  };
  return { value: storedValue, setValue, deleteValue };
};

// src/i18n/translations.ts
var import_internationalization = require("@helpwave/internationalization");
var hightideTranslationLocales = ["de-DE", "en-US"];

// src/i18n/util.ts
var localsNames = {
  "en-US": "English (US)",
  "de-DE": "Deutsch"
};
var DEFAULT_LOCALE = "en-US";
function localeToLanguage(locale) {
  return locale.split("-")[0];
}
var LocalizationUtil = {
  locals: hightideTranslationLocales,
  localToLanguage: localeToLanguage,
  DEFAULT_LOCALE,
  languagesLocalNames: localsNames
};

// src/i18n/LocaleProvider.tsx
var import_jsx_runtime = require("react/jsx-runtime");
var LocaleContext = (0, import_react2.createContext)({
  locale: LocalizationUtil.DEFAULT_LOCALE,
  setLocale: (v) => v
});
var LocaleProvider = ({ children, locale, onChangedLocale }) => {
  const {
    value: storedLocale,
    setValue: setStoredLocale,
    deleteValue: deleteStoredLocale
  } = useLocalStorage("locale", "system");
  const [localePreference, setLocalePreference] = (0, import_react2.useState)("system");
  const resolvedLocale = (0, import_react2.useMemo)(() => {
    if (locale && locale !== "system") {
      return locale;
    }
    if (storedLocale && storedLocale !== "system") {
      return storedLocale;
    }
    if (localePreference !== "system") {
      return localePreference;
    }
    return LocalizationUtil.DEFAULT_LOCALE;
  }, [locale, localePreference, storedLocale]);
  (0, import_react2.useEffect)(() => {
    if (!locale) return;
    if (locale === "system") {
      deleteStoredLocale();
    } else {
      setStoredLocale(locale);
    }
  }, [locale]);
  (0, import_react2.useEffect)(() => {
    onChangedLocale?.(resolvedLocale);
  }, [resolvedLocale]);
  (0, import_react2.useEffect)(() => {
    const localesToTestAgainst = Object.values(LocalizationUtil.locals);
    const detectLanguage = () => {
      const matchingBrowserLanguage = window.navigator.languages.map((locale2) => localesToTestAgainst.find(
        (test) => locale2 === test || locale2.split("-")[0] === LocalizationUtil.localToLanguage(test)
      )).filter((entry) => entry !== void 0);
      if (matchingBrowserLanguage.length === 0) return;
      const firstMatch = matchingBrowserLanguage[0];
      setLocalePreference(firstMatch);
    };
    detectLanguage();
    window.addEventListener("languagechange", detectLanguage);
    return () => {
      window.removeEventListener("languagechange", detectLanguage);
    };
  }, []);
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(LocaleContext.Provider, { value: {
    locale: resolvedLocale,
    setLocale: (newLocale) => {
      if (locale !== "system") {
        console.warn("LocaleProvider: Attempting to change the locale while setting a fixed locale won't have any effect. Change the locale provided to the LocaleProvider instead.");
      }
      setStoredLocale(newLocale);
    }
  }, children });
};
var useLocale = () => {
  const context = (0, import_react2.useContext)(LocaleContext);
  if (!context) {
    throw new Error("useLocale must be used within LocaleContext. Try adding a LocaleProvider around your app.");
  }
  return context;
};
var useLanguage = () => {
  const context = (0, import_react2.useContext)(LocaleContext);
  if (!context) {
    throw new Error("useLanguage must be used within LocaleContext. Try adding a LocaleProvider around your app.");
  }
  return { language: LocalizationUtil.localToLanguage(context.locale) };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  LocaleContext,
  LocaleProvider,
  useLanguage,
  useLocale
});
//# sourceMappingURL=LocaleProvider.cjs.map