"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var translated_content_viewer_exports = {};
__export(translated_content_viewer_exports, {
  TranslatedContentViewer: () => TranslatedContentViewer
});
module.exports = __toCommonJS(translated_content_viewer_exports);
var import_react = __toESM(require("react"));
var import_utils = require("./../utils.js");
var import_wix_rich_content_common = require("./../../wix-rich-content-common/index.js");
var import_fullscreen_provider = require("../modals/fullscreen/fullscreen-provider.js");
var import_vitalTranslations_json = __toESM(require("../translations/vitalTranslations.json.js"));
var import_content_viewer = require("./content-viewer.js");
const vitalTranslationsWithDefaults = addDefaultsFromEnglish(import_vitalTranslations_json.default);
const TranslatedContentViewer = (props) => {
  const locale = props.locale;
  const [resourcesState, setResourcesState] = import_react.default.useState(() => {
    return {
      status: "READY",
      loadedLocales: ["en"],
      i18nextInstance: createI18nextInstance({
        activeLocale: locale,
        resources: vitalTranslationsWithDefaults
      })
    };
  });
  const { i18nextInstance, loadedLocales } = resourcesState;
  if (locale !== i18nextInstance.language) {
    i18nextInstance.changeLanguage(locale);
  }
  import_react.default.useEffect(() => {
    if (loadedLocales.includes(locale) || resourcesState.status !== "READY") {
      return;
    }
    const loadLocaleResource = async () => {
      try {
        setResourcesState((prevState) => ({ ...prevState, status: "LOADING" }));
        const response = await Promise.resolve().then(() => (
          /* webpackChunkName: "ricos_viewer_messages_[request]" */
          __toESM(require(`../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 Promise.resolve().then(() => __toESM(require(
              /* 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 = import_react.default.useMemo(() => i18nextInstance.t.bind(i18nextInstance), [i18nextInstance]);
  return (0, import_utils.hasFullscreen)(props.config) ? /* @__PURE__ */ import_react.default.createElement(
    import_fullscreen_provider.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__ */ import_react.default.createElement(import_content_viewer.ContentViewer, { ...props, config, t, onHover })
  ) : /* @__PURE__ */ import_react.default.createElement(import_content_viewer.ContentViewer, { ...props, t });
};
function createI18nextInstance({
  activeLocale,
  resources
}) {
  const resourcesWithNs = Object.keys(resources).reduce((acc, key) => {
    acc[key] = { translation: resources[key] };
    return acc;
  }, {});
  const instance = import_wix_rich_content_common.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;
  }, {});
}
//# sourceMappingURL=translated-content-viewer.js.map
