"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildMcpToolsSnapshot = exports.getOutputPath = exports.getRepoRoot = void 0;
const tslib_1 = require("tslib");
const node_fs_1 = tslib_1.__importDefault(require("node:fs"));
const node_path_1 = tslib_1.__importDefault(require("node:path"));
const zod_to_json_schema_1 = require("zod-to-json-schema");
const toolDefinitions_1 = require("../ee/AiAgent/schemas/tools/toolDefinitions");
const SNAPSHOT_VERSION = '1';
const getRepoRoot = () => node_path_1.default.resolve(__dirname, '../../../../');
exports.getRepoRoot = getRepoRoot;
const getOutputPath = () => node_path_1.default.join((0, exports.getRepoRoot)(), 'packages/common/src/schemas/json/mcp-tools-1.0.json');
exports.getOutputPath = getOutputPath;
const schemaToJson = (schema) => {
    if (!schema)
        return null;
    return (0, zod_to_json_schema_1.zodToJsonSchema)(schema, { target: 'jsonSchema7' });
};
/**
 * PURE. Build the snapshot object from the MCP tool definitions. Exported for
 * testing/reuse; the IO `run` wraps it.
 */
const buildMcpToolsSnapshot = () => {
    const tools = toolDefinitions_1.mcpToolDefinitions
        .map((definition) => {
        const view = definition.for('mcp');
        const entry = {
            name: view.name,
            title: view.title,
            description: view.description,
            annotations: (view.annotations ?? {}),
            inputSchema: schemaToJson(view.inputSchema),
            outputSchema: 'outputSchema' in view && view.outputSchema
                ? schemaToJson(view.outputSchema)
                : null,
        };
        return entry;
    })
        .sort((left, right) => String(left.name).localeCompare(String(right.name)));
    return {
        schemaVersion: SNAPSHOT_VERSION,
        tools,
    };
};
exports.buildMcpToolsSnapshot = buildMcpToolsSnapshot;
const sortKeysDeep = (value) => {
    if (Array.isArray(value)) {
        return value.map((item) => sortKeysDeep(item));
    }
    if (!value || typeof value !== 'object') {
        return value;
    }
    const objectValue = value;
    const sortedEntries = Object.entries(objectValue)
        .sort(([left], [right]) => left.localeCompare(right))
        .map(([key, child]) => [key, sortKeysDeep(child)]);
    return Object.fromEntries(sortedEntries);
};
const toStableJson = (value) => `${JSON.stringify(sortKeysDeep(value), null, 4)}\n`;
const run = () => {
    const checkMode = process.argv.includes('--check');
    const outputPath = (0, exports.getOutputPath)();
    const nextContent = toStableJson((0, exports.buildMcpToolsSnapshot)());
    if (checkMode) {
        const currentContent = node_fs_1.default.readFileSync(outputPath, 'utf8');
        const normalizedCurrentContent = toStableJson(JSON.parse(currentContent));
        if (normalizedCurrentContent !== nextContent) {
            console.error('mcp-tools snapshot is out of date. Run `pnpm generate:mcp-tools-snapshot`.');
            process.exit(1);
        }
        return;
    }
    node_fs_1.default.writeFileSync(outputPath, nextContent, 'utf8');
};
if (require.main === module) {
    run();
}
//# sourceMappingURL=generateMcpToolsSnapshot.js.map