import {
  defineExecutorConfig
} from "./chunk-7MT7NZUQ.js";
import {
  collectTables,
  createExecutor
} from "./chunk-QEKKFEJL.js";
import {
  ConnectionNotFoundError,
  CredentialProviderNotRegisteredError,
  CredentialResolutionError,
  ElicitationAction,
  ElicitationResponse,
  FormElicitation,
  IntegrationDetectionResult,
  IntegrationNotFoundError,
  IntegrationRemovalNotAllowedError,
  NoHandlerError,
  PluginNotLoadedError,
  ToolBlockedError,
  ToolInvocationError,
  ToolNotFoundError,
  ToolSchemaView,
  UrlElicitation
} from "./chunk-6EED5LAL.js";
import "./chunk-EIHWBY6T.js";
import {
  AuthTemplateSlug,
  ConnectionName,
  IntegrationSlug,
  Owner,
  PolicyId,
  ProviderItemId,
  ProviderKey,
  Subject,
  Tenant,
  ToolAddress,
  ToolName
} from "./chunk-7GJMCW76.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() }) : config.db;
  const effectConfig = {
    tenant: Tenant.make(config.tenant ?? "default-tenant"),
    ...config.subject !== void 0 ? { subject: Subject.make(config.subject) } : {},
    plugins,
    ...config.providers ? { providers: config.providers } : {},
    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 {
  AuthTemplateSlug,
  ConnectionName,
  ConnectionNotFoundError,
  CredentialProviderNotRegisteredError,
  CredentialResolutionError,
  ElicitationAction,
  ElicitationResponse,
  FormElicitation,
  IntegrationDetectionResult,
  IntegrationNotFoundError,
  IntegrationRemovalNotAllowedError,
  IntegrationSlug,
  NoHandlerError,
  Owner,
  PluginNotLoadedError,
  PolicyId,
  ProviderItemId,
  ProviderKey,
  Subject,
  Tenant,
  ToolAddress,
  ToolBlockedError,
  ToolInvocationError,
  ToolName,
  ToolNotFoundError,
  ToolSchemaView,
  UrlElicitation,
  createExecutor2 as createExecutor,
  defineExecutorConfig
};
//# sourceMappingURL=index.js.map