import React from "react";
import { useDevConnectionState } from "./../../ricos-context/index.js";
import {
  useGetWixClientPromise
} from "./../request-service-provider/use-get-wix-client-promise.js";
import { wrapWithNetworkConditions } from "./../../wix-rich-content-common/devTools/wrap-with-network-conditions.js";
const RequestServiceContext = React.createContext(
  null
);
const RequestServiceProvider = ({ children, wixClient: existingWixClient, instance }) => {
  const connectionState = useDevConnectionState();
  const { getWixClientPromise } = useGetWixClientPromise(instance ?? "", existingWixClient);
  React.useEffect(() => {
    if (!existingWixClient && !instance) {
      console.warn(
        "Ricos: neither wixClient nor instance was provided. Requests will not be authenticated."
      );
    }
  }, [existingWixClient, instance]);
  const service = React.useMemo(() => {
    return {
      getWixClientPromise,
      makeRequest: wrapWithNetworkConditions(
        async (relativeUrl, options) => {
          const wixClient = await getWixClientPromise();
          return wixClient.fetchWithAuth(relativeUrl, options);
        },
        () => connectionState
      )
    };
  }, [getWixClientPromise, connectionState]);
  return /* @__PURE__ */ React.createElement(RequestServiceContext.Provider, { value: service }, children);
};
const useRequestService = () => {
  const context = React.useContext(RequestServiceContext);
  if (!context) {
    throw new Error("useRequestService must be used within a RequestServiceProvider");
  }
  return context;
};
export {
  RequestServiceProvider,
  useRequestService
};
//# sourceMappingURL=request-service-provider.js.map
