import { jsx } from "react/jsx-runtime";
import { createMessageChannel, withReaderTransform, createWindowMessageReader, UIExtensionHostEventSchema, createWindowMessageWriter, initializeSDK } from "@netlify/sdk--ui-core";
import { createContext, useState, useEffect, useMemo } from "react";
import { ListLoader } from "./components/vendor/ListLoader/ListLoader.js";
import { Card } from "./components/vendor/Card/Card.js";
import "@netlify/ui";
const DefaultLoader = () => /* @__PURE__ */ jsx(Card, { children: /* @__PURE__ */ jsx(ListLoader, {}) });
const NetlifySDKContext = createContext({
  _updateMarker: 0,
  sdk: null
});
const NetlifySDKProvider = ({
  children,
  loader: Loader = DefaultLoader,
  messageChannel: initialMessageChannel,
  sdk: initialSDK
}) => {
  const [sdk, setSDK] = useState(initialSDK ?? null);
  const [updateMarker, setUpdateMarker] = useState(0);
  const [abortController] = useState(new AbortController());
  useEffect(() => {
    return () => {
      abortController.abort();
    };
  }, [abortController]);
  const messageChannel = useMemo(
    () => initialMessageChannel ?? createMessageChannel(
      withReaderTransform(
        createWindowMessageReader({
          signal: abortController.signal,
          // TODO(ndhoule): Set these origins depending on the environment
          isOriginPermitted: (origin) => {
            if (origin === window.location.origin) {
              return true;
            }
            if (origin === "https://app.netlify.com") {
              return true;
            }
            if (origin === "https://app.netlifystg.com") {
              return true;
            }
            if (/^https:\/\/deploy-preview-\d+--app\.netlify\.com$/.test(origin)) {
              return true;
            }
            if (origin === "http://localhost:8888") {
              return true;
            }
            return false;
          }
        }),
        (message) => {
          const result = UIExtensionHostEventSchema.safeParse(message);
          return result.success ? result.data : null;
        }
      ),
      createWindowMessageWriter({
        signal: abortController.signal,
        // TODO(ndhoule): Set the target origin depending on the environment
        targetOrigin: "*",
        targetWindow: window.parent
      })
    ),
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [abortController.signal]
  );
  useEffect(() => {
    if (sdk === null) {
      void (async () => {
        setSDK(await initializeSDK({ messageChannel }));
      })();
    }
  }, [messageChannel, sdk, setSDK]);
  useEffect(() => {
    if (sdk === null) {
      return;
    }
    return sdk.onChange(() => {
      setUpdateMarker(
        (marker) => marker === Number.MAX_SAFE_INTEGER ? 0 : marker + 1
      );
    });
  }, [sdk]);
  if (sdk === null) {
    if (typeof Loader === "function") {
      return /* @__PURE__ */ jsx(Loader, {});
    }
    return Loader;
  }
  return /* @__PURE__ */ jsx(NetlifySDKContext.Provider, { value: { sdk, _updateMarker: updateMarker }, children });
};
export {
  NetlifySDKContext,
  NetlifySDKProvider
};
//# sourceMappingURL=NetlifySDKProvider.js.map
