"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MCP_VERIFY_SCHEMA_VERSION = exports.MCP_VERIFY_KIND = void 0;
exports.verifyMcp = verifyMcp;
const catalog_1 = require("../i18n/catalog");
const required_mcp_1 = require("../workflows/required-mcp");
const inventory_1 = require("./inventory");
exports.MCP_VERIFY_KIND = "alpaca.mcpVerify";
exports.MCP_VERIFY_SCHEMA_VERSION = 2;
async function verifyMcp(input) {
    const inventory = await (0, inventory_1.listAvailableMcpServers)({
        repoRoot: input.repoRoot,
        workers: input.workers,
        configuredWorkers: input.configuredWorkers,
        ...(input.authProfiles === undefined
            ? {}
            : { authProfiles: input.authProfiles }),
        ...(input.env === undefined ? {} : { env: input.env }),
        ...(input.homeDirectory === undefined
            ? {}
            : { homeDirectory: input.homeDirectory }),
        ...(input.run === undefined ? {} : { run: input.run }),
        includeExcluded: input.refs.length > 0,
    });
    const refs = input.refs.length > 0
        ? [...input.refs]
        : inventory.items
            .filter((item) => item.usable)
            .map((item) => `${item.worker}:${item.ref}`);
    if (input.refs.length === 0) {
        return {
            kind: exports.MCP_VERIFY_KIND,
            schemaVersion: exports.MCP_VERIFY_SCHEMA_VERSION,
            repositoryRoot: input.repoRoot,
            ok: refs.length > 0 ||
                (inventory.items.length === 0 && inventory.diagnostics.length === 0),
            workers: [...input.workers],
            refs,
            summary: refs.length === 0
                ? (0, catalog_1.translate)(input.locale, "workflow.requiredMcp.report.verify.none")
                : (0, catalog_1.translate)(input.locale, "workflow.requiredMcp.report.verify.visible", {
                    mcpRefs: refs.join(", "),
                }),
            items: inventory.items,
            diagnostics: inventory.diagnostics,
        };
    }
    const report = await (0, required_mcp_1.checkRequiredMcpAvailability)({
        repoRoot: input.repoRoot,
        workers: input.workers,
        ...(input.authProfiles === undefined
            ? {}
            : { authProfiles: input.authProfiles }),
        ...(input.env === undefined ? {} : { env: input.env }),
        ...(input.homeDirectory === undefined
            ? {}
            : { homeDirectory: input.homeDirectory }),
        ...(input.run === undefined ? {} : { run: input.run }),
        mcpRefs: refs,
        inventory,
    });
    return {
        kind: exports.MCP_VERIFY_KIND,
        schemaVersion: exports.MCP_VERIFY_SCHEMA_VERSION,
        repositoryRoot: input.repoRoot,
        ok: report.ok,
        workers: [...input.workers],
        refs,
        summary: (0, required_mcp_1.formatRequiredMcpAvailabilityReport)({
            report,
            locale: input.locale,
        }),
        items: inventory.items,
        diagnostics: inventory.diagnostics,
    };
}
