import { makeCustomToolFormatter } from '../custom-tools/formatter.js';
import { buildHandler } from '../custom-tools/handler.js';
import { buildJsonSchema, buildValidator } from '../custom-tools/schema-builder.js';
import { jsonSchema, tool } from '../types/core.js';
import { createFileToolApproval } from '../utils/tool-approval.js';
import { withValidation } from '../utils/tool-validation.js';
/**
 * Compose a registry-ready `ToolEntry` from a loaded custom tool.
 *
 * Mirrors how built-in tools are constructed: a `tool()` carrying the JSON
 * schema, plus a synthesized handler, validator, formatter, and a mode-aware
 * `approval` policy resolved from the tool's declared approval mode.
 */
export function buildToolEntry(loaded, projectRoot) {
    const { metadata, body } = loaded;
    const handler = buildHandler(metadata, body, projectRoot);
    const validator = buildValidator(metadata);
    const formatter = makeCustomToolFormatter(metadata.name);
    const aiSdkTool = tool({
        description: metadata.description,
        inputSchema: jsonSchema(buildJsonSchema(metadata)),
        execute: async (args, _options) => {
            return handler(args);
        },
    });
    return {
        name: metadata.name,
        tool: aiSdkTool,
        // Validate inside the handler so every execution path is covered.
        handler: withValidation(handler, validator),
        validator,
        formatter,
        readOnly: metadata.readOnly,
        approval: approvalForPolicy(metadata.name, metadata.approval),
    };
}
function approvalForPolicy(name, policy) {
    switch (policy) {
        case 'never':
            return false;
        case 'always':
            return true;
        case 'destructive':
            return createFileToolApproval(name);
    }
}
//# sourceMappingURL=build-tool.js.map