import React from "react";
import { hasFullscreen } from "./../utils.js";
import { i18next } from "./../../wix-rich-content-common/index.js";
import { FullscreenProvider } from "../modals/fullscreen/fullscreen-provider.js";
import viewerVitalTranslations from "../translations/vitalTranslations.json.js";
import { ContentViewer } from "./content-viewer.js";
const vitalTranslationsWithDefaults = addDefaultsFromEnglish(viewerVitalTranslations);
const TranslatedContentViewer = (props) => {
  const locale = props.locale;
  const [resourcesState, setResourcesState] = React.useState(() => {
    return {
      status: "READY",
      loadedLocales: ["en"],
      i18nextInstance: createI18nextInstance({
        activeLocale: locale,
        resources: vitalTranslationsWithDefaults
      })
    };
  });
  const { i18nextInstance, loadedLocales } = resourcesState;
  if (locale !== i18nextInstance.language) {
    i18nextInstance.changeLanguage(locale);
  }
  React.useEffect(() => {
    if (loadedLocales.includes(locale) || resourcesState.status !== "READY") {
      return;
    }
    const loadLocaleResource = async () => {
      try {
        setResourcesState((prevState) => ({ ...prevState, status: "LOADING" }));
        const response = await import(
          /* webpackChunkName: "ricos_viewer_messages_[request]" */
          `../translations/locale/messages_${locale}.json`
        );
        const localeResource = response.default;
        i18nextInstance.addResourceBundle(locale, "translation", localeResource, true, true);
        setResourcesState((prevState) => {
          return {
            ...prevState,
            status: "READY",
            loadedLocales: [...prevState.loadedLocales, locale]
          };
        });
      } catch (err) {
        console.error(`Failed to load locale resource for ${locale}:`, err);
        if (locale !== "en" && !loadedLocales.includes("en")) {
          try {
            console.warn(`Falling back to English locale for ${locale}`);
            const fallbackResponse = await import(
              /* webpackChunkName: "ricos_viewer_messages_en" */
              "../translations/locale/messages_en.json"
            );
            const fallbackResource = fallbackResponse.default;
            i18nextInstance.addResourceBundle("en", "translation", fallbackResource, true, true);
            setResourcesState((prevState) => {
              return {
                ...prevState,
                status: "READY",
                loadedLocales: [...prevState.loadedLocales, "en"]
              };
            });
          } catch (fallbackErr) {
            console.error("Failed to load fallback English locale:", fallbackErr);
            setResourcesState((prevState) => {
              return { ...prevState, status: "ERROR" };
            });
          }
        } else {
          setResourcesState((prevState) => {
            return { ...prevState, status: "ERROR" };
          });
        }
      }
    };
    loadLocaleResource();
  }, [locale, loadedLocales]);
  const t = React.useMemo(() => i18nextInstance.t.bind(i18nextInstance), [i18nextInstance]);
  return hasFullscreen(props.config) ? /* @__PURE__ */ React.createElement(
    FullscreenProvider,
    {
      key: "RicosFullscreenProvider",
      fullscreenProps: props?.fullscreenProps,
      isMobile: !!props.isMobile,
      content: props.content,
      experiments: props.experiments,
      config: props.config,
      jsdomMode: props.jsdomMode,
      t
    },
    ({ config, onHover }) => /* @__PURE__ */ React.createElement(ContentViewer, { ...props, config, t, onHover })
  ) : /* @__PURE__ */ React.createElement(ContentViewer, { ...props, t });
};
function createI18nextInstance({
  activeLocale,
  resources
}) {
  const resourcesWithNs = Object.keys(resources).reduce((acc, key) => {
    acc[key] = { translation: resources[key] };
    return acc;
  }, {});
  const instance = i18next.createInstance({
    lng: activeLocale,
    keySeparator: "$",
    interpolation: { escapeValue: false },
    resources: resourcesWithNs
  });
  instance.init();
  return instance;
}
function addDefaultsFromEnglish(resources) {
  const english = resources.en;
  return Object.keys(resources).reduce((acc, locale) => {
    acc[locale] = { ...english, ...resources[locale] };
    return acc;
  }, {});
}
export {
  TranslatedContentViewer
};
//# sourceMappingURL=translated-content-viewer.js.map
