import React, { createContext, useEffect, useMemo, useState } from "react";
import { useRequestService } from "./../request-service-provider/request-service-provider.js";
const fetchBaseUrl = async (makeRequest) => {
  const response = await makeRequest(`/_api/ricos-site-pages/base-url`, {
    headers: {
      "Content-Type": "application/json"
    },
    method: "GET"
  });
  const json = await response.json();
  return typeof json?.baseUrl === "string" ? json.baseUrl : "";
};
const EditorBaseUrlContext = createContext({
  baseUrl: ""
});
const EditorBaseUrlContextProvider = ({ baseUrl, children }) => {
  const { makeRequest } = useRequestService();
  const [fetchResult, setFetchResult] = useState();
  const value = useMemo(
    () => ({
      baseUrl: baseUrl ?? fetchResult ?? ""
    }),
    [baseUrl, fetchResult]
  );
  useEffect(() => {
    if (baseUrl === void 0) {
      fetchBaseUrl(makeRequest).then(setFetchResult).catch((error) => {
        console.warn("Failed to get baseUrl", error);
      });
    }
  }, [baseUrl, makeRequest]);
  return /* @__PURE__ */ React.createElement(EditorBaseUrlContext.Provider, { children, value });
};
export {
  EditorBaseUrlContext,
  EditorBaseUrlContextProvider
};
//# sourceMappingURL=editor-base-url-context.js.map
