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 createPluginAtomClient = (group, options = {}) => {
  const { baseUrl = "/api" } = options;
  const pluginId = group.identifier;
  const bundle = HttpApi.make(`plugin-${pluginId}`).add(group);
  return AtomHttpApi.Service()(`Plugin_${pluginId}Client`, {
    api: bundle,
    httpClient: FetchHttpClient.layer,
    ...typeof baseUrl === "function" ? {
      transformClient: HttpClient.mapRequest(
        (request) => HttpClientRequest.prependUrl(request, baseUrl())
      )
    } : { baseUrl }
  });
};
var ExecutorPluginsContext = createContext(null);
ExecutorPluginsContext.displayName = "ExecutorPluginsContext";
function ExecutorPluginsProvider(props) {
  const { plugins, children } = props;
  const value = useMemo(
    () => ({
      plugins,
      sourcePlugins: plugins.flatMap((p) => p.sourcePlugin ? [p.sourcePlugin] : []),
      secretProviderPlugins: plugins.flatMap(
        (p) => p.secretProviderPlugin ? [p.secretProviderPlugin] : []
      )
    }),
    [plugins]
  );
  useEffect(() => {
    for (const sp of value.sourcePlugins) sp.preload?.();
  }, [value.sourcePlugins]);
  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 useSourcePlugins = () => usePluginsCtx("useSourcePlugins").sourcePlugins;
var useSecretProviderPlugins = () => usePluginsCtx("useSecretProviderPlugins").secretProviderPlugins;
export {
  AsyncResult,
  Atom,
  AtomHttpApi2 as AtomHttpApi,
  ExecutorPluginsProvider,
  HttpApi2 as HttpApi,
  HttpApiEndpoint,
  HttpApiGroup,
  Schema,
  createPluginAtomClient,
  defineClientPlugin,
  useAtomMount,
  useAtomRefresh,
  useAtomSet,
  useAtomValue,
  useClientPlugins,
  useSecretProviderPlugins,
  useSourcePlugins
};
//# sourceMappingURL=client.js.map