import {
  ToolInputError,
  findCustomTool,
  isAppToolName
} from "./chunk-YEFFHORB.js";

// src/tools/dispatch.ts
async function dispatchAppTool(toolName, rawArgs, ctx, opts) {
  try {
    if (!isAppToolName(toolName)) {
      const custom = findCustomTool(toolName, opts.customTools);
      if (!custom) return { ok: false, code: "unknown_tool", message: `${toolName} is not an app tool.` };
      const result = await custom.execute(rawArgs, ctx);
      return { ok: true, result };
    }
    if (toolName === "submit_proposal") {
      const type = String(rawArgs.type ?? "").trim();
      const title = String(rawArgs.title ?? "").trim();
      if (!type || !opts.taxonomy.proposalTypes.includes(type)) {
        return { ok: false, code: "invalid_type", message: `type must be one of: ${opts.taxonomy.proposalTypes.join(", ")}.` };
      }
      if (!title) return { ok: false, code: "missing_title", message: "title is required." };
      const description = rawArgs.description == null ? null : String(rawArgs.description);
      let regulated = opts.taxonomy.regulatedTypes.includes(type);
      if (opts.needsApproval) {
        try {
          regulated = await opts.needsApproval(type, { title, description }, ctx);
        } catch {
          regulated = true;
        }
      }
      const r2 = await opts.handlers.submitProposal({ type, title, description, regulated }, ctx);
      const { proposalId, deduped, status, ...extra } = r2;
      const effectiveStatus = status ?? "queued_for_approval";
      opts.onProduced?.({
        type: "proposal_created",
        proposalId,
        title,
        status: effectiveStatus === "executed" ? "executed" : "pending",
        content: description ?? void 0
      });
      return { ok: true, result: { ...extra, status: effectiveStatus, proposalId, deduped, regulated } };
    }
    if (toolName === "schedule_followup") {
      const r2 = await opts.handlers.scheduleFollowup(
        { title: String(rawArgs.title ?? ""), dueDate: String(rawArgs.dueDate ?? ""), priority: rawArgs.priority },
        ctx
      );
      return { ok: true, result: { followupId: r2.id, dueDate: r2.dueDate, deduped: r2.deduped } };
    }
    if (toolName === "render_ui") {
      const r2 = await opts.handlers.renderUi({ title: String(rawArgs.title ?? ""), schema: rawArgs.schema }, ctx);
      opts.onProduced?.({ type: "artifact", path: r2.path, content: r2.content });
      return { ok: true, result: { path: r2.path } };
    }
    const r = await opts.handlers.addCitation(
      { path: String(rawArgs.path ?? ""), quote: String(rawArgs.quote ?? ""), label: rawArgs.label },
      ctx
    );
    return { ok: true, result: { citationId: r.citationId, path: r.path } };
  } catch (err) {
    if (err instanceof ToolInputError) return { ok: false, code: err.code, message: err.message, status: err.status };
    return { ok: false, code: "app_tool_error", message: err instanceof Error ? err.message : String(err), status: 500 };
  }
}
function outcomeStatus(outcome) {
  return outcome.status ?? 400;
}

// src/tools/runtime.ts
function createAppToolRuntimeExecutor(opts) {
  return ({ toolName, args }) => dispatchAppTool(toolName, args, opts.ctx, opts);
}

export {
  dispatchAppTool,
  outcomeStatus,
  createAppToolRuntimeExecutor
};
//# sourceMappingURL=chunk-UOAYS72M.js.map