"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 request_service_provider_exports = {};
__export(request_service_provider_exports, {
  RequestServiceProvider: () => RequestServiceProvider,
  useRequestService: () => useRequestService
});
module.exports = __toCommonJS(request_service_provider_exports);
var import_react = __toESM(require("react"));
var import_ricos_context = require("./../../ricos-context/index.js");
var import_use_get_wix_client_promise = require("./../request-service-provider/use-get-wix-client-promise.js");
var import_wrap_with_network_conditions = require("./../../wix-rich-content-common/devTools/wrap-with-network-conditions.js");
const RequestServiceContext = import_react.default.createContext(
  null
);
const RequestServiceProvider = ({ children, wixClient: existingWixClient, instance }) => {
  const connectionState = (0, import_ricos_context.useDevConnectionState)();
  const { getWixClientPromise } = (0, import_use_get_wix_client_promise.useGetWixClientPromise)(instance ?? "", existingWixClient);
  import_react.default.useEffect(() => {
    if (!existingWixClient && !instance) {
      console.warn(
        "Ricos: neither wixClient nor instance was provided. Requests will not be authenticated."
      );
    }
  }, [existingWixClient, instance]);
  const service = import_react.default.useMemo(() => {
    return {
      getWixClientPromise,
      makeRequest: (0, import_wrap_with_network_conditions.wrapWithNetworkConditions)(
        async (relativeUrl, options) => {
          const wixClient = await getWixClientPromise();
          return wixClient.fetchWithAuth(relativeUrl, options);
        },
        () => connectionState
      )
    };
  }, [getWixClientPromise, connectionState]);
  return /* @__PURE__ */ import_react.default.createElement(RequestServiceContext.Provider, { value: service }, children);
};
const useRequestService = () => {
  const context = import_react.default.useContext(RequestServiceContext);
  if (!context) {
    throw new Error("useRequestService must be used within a RequestServiceProvider");
  }
  return context;
};
//# sourceMappingURL=request-service-provider.js.map
