"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildWorkerTemplateCatalogEntries = buildWorkerTemplateCatalogEntries;
exports.buildWorkerTemplateImpactContext = buildWorkerTemplateImpactContext;
const validation_1 = require("../workflows/validation");
const template_registry_1 = require("../workflows/template-registry");
function buildWorkerTemplateCatalogEntries(catalog) {
    return catalog.templates.map((template) => ({
        path: template.templatePath,
        description: template.metadata?.description ?? null,
        requiredSkills: template.metadata?.requiredSkills ?? [],
        requiredMcp: template.metadata?.requiredMcp ?? [],
        targets: (0, template_registry_1.resolveWorkflowTemplateRecordTargets)(template),
        autoSelect: template.autoSelect ?? true,
        body: template.body,
    }));
}
function buildWorkerTemplateImpactContext(catalog, targetTemplatePath) {
    const targetTemplate = catalog.templateByPath.get(targetTemplatePath);
    return {
        incomingReferences: buildIncomingWorkflowReferences(catalog, targetTemplatePath),
        outgoingReferences: targetTemplate === undefined
            ? []
            : (0, validation_1.scanWorkflowTemplateDirectives)(targetTemplate).directives.flatMap((directive) => toWorkerTemplateReferenceContext(catalog, directive.templateName, {
                lineNumber: directive.lineNumber,
                sourceLine: directive.sourceLine,
            })),
    };
}
function buildIncomingWorkflowReferences(catalog, targetTemplatePath) {
    return catalog.templates.flatMap((template) => (0, validation_1.scanWorkflowTemplateDirectives)(template).directives.flatMap((directive) => directive.templateName === targetTemplatePath
        ? toWorkerTemplateReferenceContext(catalog, template.templatePath, {
            lineNumber: directive.lineNumber,
            sourceLine: directive.sourceLine,
        })
        : []));
}
function toWorkerTemplateReferenceContext(catalog, templatePath, callSite) {
    const template = catalog.templateByPath.get(templatePath);
    if (template === undefined) {
        return [];
    }
    return [
        {
            templatePath: template.templatePath,
            description: template.metadata?.description ?? null,
            requiredSkills: template.metadata?.requiredSkills ?? [],
            requiredMcp: template.metadata?.requiredMcp ?? [],
            targets: (0, template_registry_1.resolveWorkflowTemplateRecordTargets)(template),
            autoSelect: template.autoSelect ?? true,
            lineNumber: callSite.lineNumber,
            sourceLine: callSite.sourceLine,
        },
    ];
}
