import { jsx } from "react/jsx-runtime";
import get from "lodash/get.js";
import mergeWith from "lodash/mergeWith.js";
import { Fragment } from "react";
import { locales, localesDetails } from "./locales.js";
const buildTranslatable = (_translations) => (translatable2) => translatable2;
const translatable = buildTranslatable();
const buildTranslate = (translations, locale) => {
  const translate2 = (translationKey, replacements) => {
    const englishValue = structuredClone(get(translations.en, translationKey));
    const expectedTypes = replacements ? ["string"] : ["string", "object"];
    if (!expectedTypes.includes(typeof englishValue)) {
      throw new Error(
        `Expected translation key '${translationKey}' to be a ${expectedTypes.join(" or ")}, but got '${typeof englishValue}'`
      );
    }
    if (englishValue === void 0) {
      return "";
    }
    const translatedValue = structuredClone(get(translations[locale], translationKey));
    if (typeof englishValue === "object") {
      if (typeof translatedValue !== "object") {
        return englishValue;
      }
      return mergeWith(englishValue, translatedValue, (english, translated) => {
        if (typeof english !== typeof translated) {
          return english;
        }
      });
    }
    if (typeof translatedValue === "object") {
      return englishValue;
    }
    const value = translatedValue ?? englishValue;
    if (!(replacements == null ? void 0 : replacements.length)) {
      return value;
    }
    const hasReactNodes = !replacements.every((replacement) => typeof replacement === "string");
    if (hasReactNodes) {
      const parts = value.split(/(\{[0-9]+\})/g);
      return /* @__PURE__ */ jsx(Fragment, { children: parts.map((part) => {
        const matches = /\{([0-9]+)\}/g.exec(part);
        if (matches && matches[1]) {
          const replacementIndex = parseInt(matches[1], 10);
          return replacements[replacementIndex] ?? part;
        }
        return part;
      }) });
    } else {
      return replacements.reduce(
        (str, replacement, index) => str.replace(new RegExp(`\\{${index}\\}`, "g"), replacement),
        value
      );
    }
  };
  const wrappedTranslate = function(keyOrTranslatable, replacements) {
    if (typeof keyOrTranslatable === "function") {
      return keyOrTranslatable(wrappedTranslate, locale);
    }
    return translate2(keyOrTranslatable, replacements);
  };
  return wrappedTranslate;
};
function translate(translations, locale, translationKeyOrTranslatable, replacements) {
  const translate2 = buildTranslate(translations, locale);
  return translate2(translationKeyOrTranslatable, replacements);
}
const extractLocaleFromPath = (path, supportedLocales = locales) => {
  const pathSegments = path.split("/").filter(Boolean);
  let locale = null;
  let pathWithoutLocale = path;
  if (pathSegments[0] && supportedLocales.includes(pathSegments[0])) {
    locale = pathSegments.shift();
    pathWithoutLocale = `/${pathSegments.join("/")}`;
  }
  return { locale, pathWithoutLocale };
};
const extractLocaleFromRouter = (router, supportedLocales = locales) => {
  const path = router.asPath ? router.asPath.split(/[?#]/)[0] : router.pathname;
  if (router.locale && supportedLocales.includes(router.locale)) {
    return { locale: router.locale, pathWithoutLocale: path };
  }
  return extractLocaleFromPath(path, supportedLocales);
};
const getHtmlAttributesForLocale = (locale) => ({
  lang: locale,
  dir: localesDetails[locale].dir
});
export {
  buildTranslatable,
  buildTranslate,
  extractLocaleFromPath,
  extractLocaleFromRouter,
  getHtmlAttributesForLocale,
  translatable,
  translate
};
