import {
  defineExecutorConfig
} from "./chunk-2LU3SC64.js";
import {
  SecretBackedMap,
  SecretBackedValue,
  isSecretBackedRef,
  resolveSecretBackedMap
} from "./chunk-MLNVNVRI.js";
import {
  ElicitationAction,
  ElicitationResponse,
  FormElicitation,
  RemoveConnectionInput,
  RemoveSecretInput,
  Scope,
  SecretRef,
  SetSecretInput,
  UrlElicitation,
  collectTables,
  createExecutor
} from "./chunk-W3GGXFXE.js";
import {
  NoHandlerError,
  PluginNotLoadedError,
  PolicyId,
  ScopeId,
  SecretId,
  SecretNotFoundError,
  SecretResolutionError,
  SourceDetectionResult,
  SourceNotFoundError,
  SourceRemovalNotAllowedError,
  ToolId,
  ToolInvocationError,
  ToolNotFoundError,
  ToolSchema
} from "./chunk-ZANQD7E2.js";
import "./chunk-7D4SUZUM.js";

// src/promise-executor.ts
import { Effect } from "effect";
var isPlainObject = (v) => v !== null && typeof v === "object" && !Array.isArray(v) && !(v instanceof Date) && !(v instanceof Promise);
var isPromiseOnElicitation = (value) => value === "accept-all" || typeof value === "function";
var toEffectOnElicitation = (handler) => handler === "accept-all" ? "accept-all" : (ctx) => Effect.promise(() => Promise.resolve(handler(ctx)));
var adaptPromiseInvokeOptions = (value) => {
  if (!isPlainObject(value) || !Object.hasOwn(value, "onElicitation")) return value;
  const onElicitation = value.onElicitation;
  if (onElicitation === void 0 || !isPromiseOnElicitation(onElicitation)) return value;
  return {
    ...value,
    onElicitation: toEffectOnElicitation(onElicitation)
  };
};
var adaptPromiseArgs = (args) => args.map((arg) => adaptPromiseInvokeOptions(arg));
var promisifyDeep = (value) => {
  if (typeof value === "function") {
    return ((...args) => {
      const result = value.apply(
        void 0,
        adaptPromiseArgs(args)
      );
      if (Effect.isEffect(result)) {
        return Effect.runPromise(result);
      }
      return result;
    });
  }
  if (!isPlainObject(value)) return value;
  return new Proxy(value, {
    get(target, prop, receiver) {
      const v = Reflect.get(target, prop, receiver);
      if (typeof v === "function") {
        return (...args) => {
          const result = v.apply(target, adaptPromiseArgs(args));
          if (Effect.isEffect(result)) {
            return Effect.runPromise(result);
          }
          return result;
        };
      }
      if (isPlainObject(v)) return promisifyDeep(v);
      return v;
    }
  });
};
var createExecutor2 = async (config) => {
  const plugins = config?.plugins ?? [];
  const db = typeof config.db === "function" ? await config.db({ tables: collectTables(plugins) }) : config.db;
  const scopes = config.scopes && config.scopes.length > 0 ? config.scopes.map(
    (s, i) => Scope.make({
      id: ScopeId.make(s.id ?? (i === 0 ? "default-scope" : `scope-${i}`)),
      name: s.name ?? (i === 0 ? "default" : `scope-${i}`),
      createdAt: /* @__PURE__ */ new Date()
    })
  ) : [
    Scope.make({
      id: ScopeId.make("default-scope"),
      name: "default",
      createdAt: /* @__PURE__ */ new Date()
    })
  ];
  const effectConfig = {
    scopes,
    plugins,
    onElicitation: toEffectOnElicitation(config.onElicitation),
    ...db ? { db } : {}
  };
  const effectExecutor = await Effect.runPromise(createExecutor(effectConfig));
  const executor = promisifyDeep(effectExecutor);
  return {
    ...executor,
    close: async () => {
      await Effect.runPromise(effectExecutor.close());
    }
  };
};
export {
  ElicitationAction,
  ElicitationResponse,
  FormElicitation,
  NoHandlerError,
  PluginNotLoadedError,
  PolicyId,
  RemoveConnectionInput,
  RemoveSecretInput,
  Scope,
  ScopeId,
  SecretBackedMap,
  SecretBackedValue,
  SecretId,
  SecretNotFoundError,
  SecretRef,
  SecretResolutionError,
  SetSecretInput,
  SourceDetectionResult,
  SourceNotFoundError,
  SourceRemovalNotAllowedError,
  ToolId,
  ToolInvocationError,
  ToolNotFoundError,
  ToolSchema,
  UrlElicitation,
  createExecutor2 as createExecutor,
  defineExecutorConfig,
  isSecretBackedRef,
  resolveSecretBackedMap
};
//# sourceMappingURL=index.js.map