"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createNativeToolDescriptor = createNativeToolDescriptor;
exports.createNativeToolManifest = createNativeToolManifest;
const command_descriptor_projections_1 = require("./command-descriptor-projections");
const automation_protocol_1 = require("../automation-protocol");
function createNativeToolDescriptor(descriptor, protocolVersion = (0, automation_protocol_1.getSelectedAutomationProtocolVersion)()) {
    if (descriptor.availability !== "callable") {
        throw new TypeError(`Native tool '${descriptor.command}' requires a callable command descriptor.`);
    }
    const properties = {};
    const required = [];
    for (const input of descriptor.inputs) {
        const scalarType = input.valueType ?? "string";
        properties[input.name] = {
            type: input.variadic ? "array" : scalarType,
            ...(input.variadic ? { items: { type: scalarType } } : {}),
            description: input.validation,
            ...(input.defaultValue === undefined || input.defaultValue === null ? {} : { default: input.defaultValue })
        };
        if (input.required)
            required.push(input.name);
    }
    return Object.freeze({
        name: descriptor.command.replace(/[^a-z0-9]+/g, "_").replace(/^_|_$/g, ""),
        command: descriptor.command,
        description: descriptor.summary,
        inputSchema: Object.freeze({
            type: "object",
            additionalProperties: false,
            properties: Object.freeze(properties),
            ...(required.length === 0 ? {} : { required: Object.freeze(required) }),
            ...(descriptor.semantics === null
                ? {}
                : { "x-day-shift-semantics": descriptor.semantics })
        }),
        outputSchema: Object.freeze({ $ref: (0, automation_protocol_1.getAutomationProtocolEnvelopeSchemaRef)(protocolVersion) }),
        examples: descriptor.examples,
        effects: descriptor.effects,
        authorization: descriptor.authorization,
        confirmation: descriptor.confirmation,
        selectionRequired: descriptor.selectionRequired,
        idempotency: descriptor.idempotency,
        preconditions: descriptor.preconditions,
        transition: descriptor.transition
    });
}
function createNativeToolManifest(registry, options = {}) {
    const protocolVersion = (0, automation_protocol_1.getSelectedAutomationProtocolVersion)();
    const runtimeCommands = options.runtimeCommands === undefined
        ? null
        : new Set(options.runtimeCommands);
    const callable = registry.filter((descriptor) => descriptor.availability === "callable"
        && (runtimeCommands === null || runtimeCommands.has(descriptor.command)));
    const inspectedNonCallable = options.includeNonCallable === true
        ? registry
            .filter((descriptor) => descriptor.availability !== "callable"
            || (runtimeCommands !== null && !runtimeCommands.has(descriptor.command)))
            .map((descriptor) => (0, command_descriptor_projections_1.projectCommandDescriptorInspection)(descriptor, { forceNonCallable: true }))
        : [];
    return Object.freeze({
        schemaVersion: 1,
        protocolVersion,
        generatedFrom: "command-descriptor-runtime-intersection",
        tools: Object.freeze(callable.map((descriptor) => createNativeToolDescriptor(descriptor, protocolVersion))),
        inspectedNonCallable: Object.freeze(inspectedNonCallable),
        readOnly: true
    });
}
