import "./chunk-7D4SUZUM.js";

// src/client.ts
import {
  createContext,
  createElement,
  useContext,
  useEffect,
  useMemo
} from "react";
import { HttpApi } from "effect/unstable/httpapi";
import { FetchHttpClient, HttpClient, HttpClientRequest } from "effect/unstable/http";
import * as AtomHttpApi from "effect/unstable/reactivity/AtomHttpApi";
import { Schema } from "effect";
import { HttpApi as HttpApi2, HttpApiEndpoint, HttpApiGroup } from "effect/unstable/httpapi";
import * as AsyncResult from "effect/unstable/reactivity/AsyncResult";
import * as Atom from "effect/unstable/reactivity/Atom";
import * as AtomHttpApi2 from "effect/unstable/reactivity/AtomHttpApi";
import { useAtomValue, useAtomSet, useAtomMount, useAtomRefresh } from "@effect/atom-react";
var defineClientPlugin = (spec) => spec;
var applyPluginAtomClientRequestTransform = (request, options) => {
  let next = options.baseUrl ? HttpClientRequest.prependUrl(request, options.baseUrl()) : request;
  const authorization = typeof options.authorizationHeader === "function" ? options.authorizationHeader() : options.authorizationHeader;
  if (authorization) {
    next = HttpClientRequest.setHeader(next, "authorization", authorization);
  }
  const headers = typeof options.headers === "function" ? options.headers() : options.headers;
  if (headers) {
    for (const [name, value] of Object.entries(headers)) {
      if (value !== void 0 && value !== null) {
        next = HttpClientRequest.setHeader(next, name, value);
      }
    }
  }
  return next;
};
var createPluginAtomClient = (group, options = {}) => {
  const { baseUrl = "/api", authorizationHeader, headers } = options;
  const pluginId = group.identifier;
  const bundle = HttpApi.make(`plugin-${pluginId}`).add(group);
  const getBaseUrl = typeof baseUrl === "function" ? baseUrl : null;
  const staticBaseUrl = typeof baseUrl === "function" ? void 0 : baseUrl;
  const getAuthorizationHeader = typeof authorizationHeader === "function" ? authorizationHeader : null;
  const hasAuthorization = authorizationHeader !== void 0 && authorizationHeader !== null;
  const hasHeaders = headers !== void 0;
  const transformClient = getBaseUrl || hasAuthorization || hasHeaders ? HttpClient.mapRequest(
    (request) => applyPluginAtomClientRequestTransform(request, {
      ...getBaseUrl ? { baseUrl: getBaseUrl } : {},
      ...getAuthorizationHeader ? { authorizationHeader: getAuthorizationHeader } : authorizationHeader !== void 0 ? { authorizationHeader } : {},
      ...headers !== void 0 ? { headers } : {}
    })
  ) : void 0;
  return AtomHttpApi.Service()(`Plugin_${pluginId}Client`, {
    api: bundle,
    httpClient: FetchHttpClient.layer,
    ...staticBaseUrl !== void 0 ? { baseUrl: staticBaseUrl } : {},
    ...transformClient ? { transformClient } : {}
  });
};
var ExecutorPluginsContext = createContext(null);
ExecutorPluginsContext.displayName = "ExecutorPluginsContext";
function ExecutorPluginsProvider(props) {
  const { plugins, children } = props;
  const value = useMemo(
    () => ({
      plugins,
      integrationPlugins: plugins.flatMap(
        (p) => p.integrationPlugin ? [p.integrationPlugin] : []
      ),
      secretProviderPlugins: plugins.flatMap(
        (p) => p.secretProviderPlugin ? [p.secretProviderPlugin] : []
      )
    }),
    [plugins]
  );
  useEffect(() => {
    for (const ip of value.integrationPlugins) ip.preload?.();
  }, [value.integrationPlugins]);
  return createElement(ExecutorPluginsContext.Provider, { value }, children);
}
var usePluginsCtx = (hookName) => {
  const ctx = useContext(ExecutorPluginsContext);
  if (!ctx) {
    throw new Error(`${hookName} must be called inside an <ExecutorPluginsProvider>.`);
  }
  return ctx;
};
var useClientPlugins = () => usePluginsCtx("useClientPlugins").plugins;
var useIntegrationPlugins = () => usePluginsCtx("useIntegrationPlugins").integrationPlugins;
var useSecretProviderPlugins = () => usePluginsCtx("useSecretProviderPlugins").secretProviderPlugins;
export {
  AsyncResult,
  Atom,
  AtomHttpApi2 as AtomHttpApi,
  ExecutorPluginsProvider,
  HttpApi2 as HttpApi,
  HttpApiEndpoint,
  HttpApiGroup,
  Schema,
  applyPluginAtomClientRequestTransform,
  createPluginAtomClient,
  defineClientPlugin,
  useAtomMount,
  useAtomRefresh,
  useAtomSet,
  useAtomValue,
  useClientPlugins,
  useIntegrationPlugins,
  useSecretProviderPlugins
};
//# sourceMappingURL=client.js.map