"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 overrides_exports = {};
__export(overrides_exports, {
  OverrideFullRicosEditorLazy: () => OverrideFullRicosEditorLazy,
  getOverrideValue: () => getOverrideValue,
  hasWixInsiderOverride: () => hasWixInsiderOverride,
  useOverridePluginsMap: () => useOverridePluginsMap
});
module.exports = __toCommonJS(overrides_exports);
var import_react = __toESM(require("react"));
const STATIC_OVERRIDES_COOKIE = "wixStaticsVersions";
const ARTIFACT_ID = "wix-ricos";
const QUERY_PARAM_KEY = `${ARTIFACT_ID}-override`;
const getQueryParam = (key) => {
  if (typeof window === "undefined") {
    return "";
  }
  const urlParams = new URLSearchParams(window.location.search);
  return decodeURIComponent(urlParams.get(key) || "");
};
const getCookie = (name) => {
  if (typeof document === "undefined") {
    return null;
  }
  const value = `; ${document.cookie}`;
  const parts = value.split(`; ${name}=`);
  if (parts.length === 2) {
    return parts.pop()?.split(";").shift();
  }
  return null;
};
const getOverrideValue = () => {
  const overridesCookieValue = getCookie(STATIC_OVERRIDES_COOKIE);
  const cookiesValueMap = overridesCookieValue?.split("|").reduce(
    (acc, cookie) => {
      const [key, value2] = cookie.split("#");
      return { ...acc, [key]: value2 };
    },
    {}
  );
  const value = getQueryParam(QUERY_PARAM_KEY) || cookiesValueMap?.[ARTIFACT_ID];
  if (!value) {
    return null;
  }
  return value;
};
const isValidHash = (hash) => {
  return /^[a-f0-9]+$/.test(hash);
};
const isValidVersion = (version) => {
  return /^\d+\.\d+\.\d+$/.test(version);
};
const getOverrideUrl = () => {
  const value = getOverrideValue();
  if (!value) {
    return null;
  }
  if (value.startsWith("http://localhost") || value.startsWith("https://localhost")) {
    const url = new URL(value);
    return `${url.href}${url.href.endsWith("/") ? "" : "/"}full-ricos-editor.js`;
  }
  if (isValidVersion(value) || isValidHash(value)) {
    return `https://static.parastorage.com/services/${ARTIFACT_ID}/${value}/full-ricos-editor.js`;
  }
  return null;
};
const wixInsiderOverrideUrl = getOverrideUrl();
const hasWixInsiderOverride = !!wixInsiderOverrideUrl;
let overrideBundlePromise = null;
const loadOverrideBundle = () => {
  if (!overrideBundlePromise) {
    overrideBundlePromise = (0, eval)(`import('${wixInsiderOverrideUrl}')`).then(
      (bundled) => {
        bundled.loadCSS();
        return bundled;
      }
    );
  }
  return overrideBundlePromise;
};
const OverrideFullRicosEditorLazy = import_react.default.lazy(async () => {
  const bundled = await loadOverrideBundle();
  return { default: bundled.FullRicosEditor };
});
const useOverridePluginsMap = () => {
  const [pluginsMap, setPluginsMap] = import_react.default.useState(null);
  import_react.default.useEffect(() => {
    if (hasWixInsiderOverride) {
      loadOverrideBundle().then((bundled) => setPluginsMap(bundled.pluginsMap));
    }
  }, []);
  return pluginsMap;
};
//# sourceMappingURL=overrides.js.map
