"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 wix_instance_context_exports = {};
__export(wix_instance_context_exports, {
  WixInstanceContext: () => WixInstanceContext,
  WixInstanceContextProvider: () => WixInstanceContextProvider,
  useWixInstanceService: () => useWixInstanceService
});
module.exports = __toCommonJS(wix_instance_context_exports);
var import_react = __toESM(require("react"));
var import_ricos_context = require("./../../ricos-context/index.js");
var import_log_invariant = require("./../log-invariant.js");
var import_use_context_selector = require("./../use-context-selector.js");
var import_parse_instance = require("./parse-instance.js");
var import_request_service_provider = require("./request-service-provider.js");
const WixInstanceContext = (0, import_use_context_selector.createContext)({});
const WixInstanceContextProvider = ({ children, instance }) => {
  const { getWixClientPromise } = (0, import_request_service_provider.useRequestService)();
  const [state, setState] = import_react.default.useState(() => ({
    ...instance ? (0, import_parse_instance.parseInstance)(instance) : {},
    getWixClientPromise
  }));
  const devContext = import_react.default.useContext(import_ricos_context.DevContext);
  import_react.default.useEffect(() => {
    if (devContext.decodedInstance) {
      setState((prev) => ({
        ...prev,
        ...devContext.decodedInstance
      }));
      return;
    }
    const decode = async () => {
      try {
        if (instance) {
          return;
        }
        const wixClient = await getWixClientPromise();
        const { headers } = await wixClient.auth.getAuthHeaders();
        const authorizationHeader = headers.Authorization;
        setState((prev) => ({
          ...prev,
          ...authorizationHeader ? (0, import_parse_instance.parseInstance)(authorizationHeader) : {}
        }));
      } catch (error) {
        (0, import_log_invariant.logInvariant)(new Error(`Failed to decode instance: ${error}`));
        setState((prev) => ({ ...prev, getWixClientPromise }));
      }
    };
    decode();
  }, [instance, getWixClientPromise, devContext.decodedInstance]);
  return /* @__PURE__ */ import_react.default.createElement(WixInstanceContext.Provider, { value: state }, children);
};
const useWixInstanceService = () => {
  const context = (0, import_use_context_selector.useContext)(WixInstanceContext);
  if (!context) {
    throw new Error("useWixInstanceService must be used within a WixInstanceContextProvider");
  }
  const serviceRef = import_react.default.useRef(context);
  serviceRef.current = context;
  return import_react.default.useCallback(() => serviceRef.current, []);
};
//# sourceMappingURL=wix-instance-context.js.map
