"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PREFLIGHT_RESOLUTION_MARKER = void 0;
exports.buildPreflightResolutionMarkerBlock = buildPreflightResolutionMarkerBlock;
exports.parsePreflightResolutionMetadata = parsePreflightResolutionMetadata;
exports.isPreflightResolutionIssue = isPreflightResolutionIssue;
exports.isPreflightResolutionForParentIssue = isPreflightResolutionForParentIssue;
exports.PREFLIGHT_RESOLUTION_MARKER = "<!-- alpaca-preflight-resolution -->";
const PREFLIGHT_RESOLUTION_KINDS = [
    "required-skills",
    "required-mcp",
    "missing-workflow-template",
    "workflow-template-fit",
];
function buildPreflightResolutionMarkerBlock(input) {
    return [
        exports.PREFLIGHT_RESOLUTION_MARKER,
        `<!-- resolutionKind: ${input.resolutionKind} -->`,
        `<!-- parentIssueId: ${input.parentIssueId} -->`,
        `<!-- resolutionKey: ${input.resolutionKey} -->`,
    ].join("\n");
}
function parsePreflightResolutionMetadata(body) {
    if (!body.includes(exports.PREFLIGHT_RESOLUTION_MARKER)) {
        return null;
    }
    const resolutionKind = readPreflightMarkerField(body, "resolutionKind");
    const parentIssueId = readPreflightMarkerField(body, "parentIssueId");
    const resolutionKey = readPreflightMarkerField(body, "resolutionKey");
    if (!isPreflightResolutionKind(resolutionKind) ||
        parentIssueId === null ||
        resolutionKey === null) {
        return null;
    }
    return {
        resolutionKind,
        parentIssueId,
        resolutionKey,
    };
}
function isPreflightResolutionIssue(issue) {
    return issue.body.includes(exports.PREFLIGHT_RESOLUTION_MARKER);
}
function isPreflightResolutionForParentIssue(input) {
    return (parsePreflightResolutionMetadata(input.issue.body)?.parentIssueId ===
        input.parentIssueId);
}
function readPreflightMarkerField(body, fieldName) {
    const match = new RegExp(`<!--\\s*${fieldName}:\\s*([^\\n]*?)\\s*-->`, "u").exec(body);
    const value = match?.[1]?.trim();
    return value ? value : null;
}
function isPreflightResolutionKind(value) {
    return (value !== null &&
        PREFLIGHT_RESOLUTION_KINDS.includes(value));
}
