"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolvePhaseOverviewPath = resolvePhaseOverviewPath;
exports.resolveMilestoneSlug = resolveMilestoneSlug;
exports.resolveMilestoneTitle = resolveMilestoneTitle;
exports.ensureDirectory = ensureDirectory;
exports.parsePhaseOverview = parsePhaseOverview;
exports.extractFrontMatter = extractFrontMatter;
exports.readTopLevelField = readTopLevelField;
exports.readUpstreamField = readUpstreamField;
exports.parseUpstreamSpecs = parseUpstreamSpecs;
exports.extractMarkdownSections = extractMarkdownSections;
exports.normalizeSectionText = normalizeSectionText;
exports.sectionBullets = sectionBullets;
exports.parseDeclaredMilestoneDescriptions = parseDeclaredMilestoneDescriptions;
exports.resolveMilestoneDescription = resolveMilestoneDescription;
const node_fs_1 = __importDefault(require("node:fs"));
const errors_1 = require("../../errors");
const paths_1 = require("../../paths");
const front_matter_utils_1 = require("../front-matter-utils");
const governed_hierarchy_context_1 = require("../governed-hierarchy-context");
const planning_document_utils_1 = require("../planning-document-utils");
const runtime_acceptance_1 = require("../runtime-acceptance");
function resolvePhaseOverviewPath(repositoryRoot, inputPath) {
    try {
        return (0, paths_1.resolvePlanningOverviewInputPath)(repositoryRoot, {
            layer: "phase",
            inputPath
        });
    }
    catch (error) {
        throw (0, errors_1.createUsageError)(`Invalid --phase-overview path '${inputPath}'.`, [error instanceof Error ? error.message : String(error)]);
    }
}
function resolveMilestoneSlug(rawMilestoneSlug) {
    if ((0, paths_1.isCanonicalSlug)(rawMilestoneSlug)) {
        return rawMilestoneSlug;
    }
    const normalized = (0, paths_1.normalizeCanonicalSlug)(rawMilestoneSlug);
    if (normalized.kind === "canonical-slug") {
        return normalized.normalizedSlug;
    }
    throw (0, errors_1.createUsageError)(`Invalid milestone slug '${rawMilestoneSlug}'.`, [normalized.reason.message]);
}
function resolveMilestoneTitle(milestoneSlug, requestedTitle) {
    const trimmed = requestedTitle?.trim();
    if (trimmed !== undefined && trimmed !== "") {
        return trimmed;
    }
    return (0, front_matter_utils_1.toTitleCase)(milestoneSlug);
}
function ensureDirectory(directoryPath) {
    if (node_fs_1.default.existsSync(directoryPath)) {
        return "kept";
    }
    node_fs_1.default.mkdirSync(directoryPath, { recursive: true });
    return "created";
}
function parsePhaseOverview(phaseOverviewPath, fileText) {
    const sections = extractMarkdownSections(fileText);
    const frontMatter = extractFrontMatter(phaseOverviewPath, fileText);
    const artifactType = readTopLevelField(frontMatter.lines, "artifact_type");
    if (artifactType !== "phase_overview") {
        throw (0, errors_1.createUsageError)(`Parent overview '${(0, front_matter_utils_1.toRepositoryLikePath)(phaseOverviewPath)}' must use artifact_type: phase_overview.`);
    }
    const slice = readTopLevelField(frontMatter.lines, "slice");
    if (slice === undefined || slice.trim() === "") {
        throw (0, errors_1.createUsageError)(`Parent overview '${(0, front_matter_utils_1.toRepositoryLikePath)(phaseOverviewPath)}' is missing required top-level field 'slice'.`);
    }
    const phase = readTopLevelField(frontMatter.lines, "phase");
    if (phase === undefined || phase.trim() === "") {
        throw (0, errors_1.createUsageError)(`Parent overview '${(0, front_matter_utils_1.toRepositoryLikePath)(phaseOverviewPath)}' is missing required top-level field 'phase'.`);
    }
    const planningDepth = readTopLevelField(frontMatter.lines, "planning_depth")?.trim() || "standard";
    const sliceOverviewPath = readUpstreamField(frontMatter.lines, "slice_overview");
    if (sliceOverviewPath === undefined || sliceOverviewPath.trim() === "") {
        throw (0, errors_1.createUsageError)(`Parent overview '${(0, front_matter_utils_1.toRepositoryLikePath)(phaseOverviewPath)}' is missing required upstream field 'slice_overview'.`);
    }
    const upstreamSpecs = parseUpstreamSpecs(frontMatter.lines, phaseOverviewPath);
    const runtimeAcceptance = (0, runtime_acceptance_1.assertRuntimeAcceptanceInspection)((0, runtime_acceptance_1.inspectRuntimeAcceptance)({
        artifactPath: phaseOverviewPath,
        artifactKind: "phase",
        frontMatterLines: frontMatter.lines,
        markdownText: fileText
    }));
    const governedPolicy = (0, governed_hierarchy_context_1.readGovernedHierarchyPolicy)(frontMatter.lines);
    if (upstreamSpecs.length === 0) {
        throw (0, errors_1.createUsageError)(`Parent overview '${(0, front_matter_utils_1.toRepositoryLikePath)(phaseOverviewPath)}' must include at least one upstream spec entry under upstream.specs.`);
    }
    return {
        slice: slice.trim(),
        phase: phase.trim(),
        planningDepth,
        sliceOverviewPath: sliceOverviewPath.trim(),
        upstreamSpecs,
        purpose: normalizeSectionText(sections.get("Purpose")),
        scope: sectionBullets(sections.get("Scope")),
        acceptanceCriteria: sectionBullets(sections.get("Acceptance Criteria")),
        dependencies: sectionBullets(sections.get("Dependencies")),
        risks: sectionBullets(sections.get("Risks")),
        validationFocus: sectionBullets(sections.get("Validation Focus")),
        milestoneDescriptions: parseDeclaredMilestoneDescriptions(sections.get("Milestones") ?? []),
        runtimeAcceptance,
        governedPolicy
    };
}
function extractFrontMatter(filePath, text) {
    const lines = text.split(/\r?\n/);
    if (lines[0] !== "---") {
        throw (0, errors_1.createUsageError)(`Parent overview '${(0, front_matter_utils_1.toRepositoryLikePath)(filePath)}' is missing YAML front matter.`);
    }
    let closingIndex = -1;
    for (let index = 1; index < lines.length; index += 1) {
        if (lines[index] === "---") {
            closingIndex = index;
            break;
        }
    }
    if (closingIndex === -1) {
        throw (0, errors_1.createUsageError)(`Parent overview '${(0, front_matter_utils_1.toRepositoryLikePath)(filePath)}' has malformed YAML front matter.`);
    }
    return {
        lines: lines.slice(1, closingIndex)
    };
}
function readTopLevelField(lines, key) {
    for (const line of lines) {
        if (/^\s/.test(line)) {
            continue;
        }
        const match = line.match(/^([A-Za-z0-9_]+):\s*(.*)$/);
        if (match === null) {
            continue;
        }
        if (match[1] === key) {
            return (0, front_matter_utils_1.parseScalar)(match[2]);
        }
    }
    return undefined;
}
function readUpstreamField(lines, key) {
    let inUpstream = false;
    for (const line of lines) {
        if (!inUpstream) {
            if (line.trim() === "upstream:") {
                inUpstream = true;
            }
            continue;
        }
        if (!/^\s/.test(line)) {
            break;
        }
        const match = line.match(/^\s{2}([A-Za-z0-9_]+):\s*(.*)$/);
        if (match !== null && match[1] === key) {
            return (0, front_matter_utils_1.parseScalar)(match[2]);
        }
    }
    return undefined;
}
function parseUpstreamSpecs(lines, filePath) {
    const entries = [];
    for (let index = 0; index < lines.length; index += 1) {
        const idMatch = lines[index].match(/^\s*-\s+id:\s*(.+)$/);
        if (idMatch === null) {
            continue;
        }
        const depthLine = lines[index + 1];
        const refLine = lines[index + 2];
        const sourceTypeLine = lines[index + 3];
        const depthMatch = depthLine?.match(/^\s+depth:\s*(.+)$/);
        const refMatch = refLine?.match(/^\s+ref:\s*(.+)$/);
        const sourceTypeMatch = sourceTypeLine?.match(/^\s+source_type:\s*(.+)$/);
        if (depthMatch === null || refMatch === null || sourceTypeMatch === null) {
            throw (0, errors_1.createUsageError)(`Parent overview '${(0, front_matter_utils_1.toRepositoryLikePath)(filePath)}' has malformed upstream.specs metadata near '${lines[index].trim()}'.`);
        }
        entries.push({
            id: (0, front_matter_utils_1.parseScalar)(idMatch[1]),
            depth: (0, front_matter_utils_1.parseScalar)(depthMatch[1]),
            ref: (0, front_matter_utils_1.parseScalar)(refMatch[1]),
            sourceType: (0, front_matter_utils_1.parseScalar)(sourceTypeMatch[1])
        });
        index += 3;
    }
    return entries;
}
function extractMarkdownSections(text) {
    const lines = text.split(/\r?\n/);
    const sections = new Map();
    let currentSection;
    for (const line of lines) {
        const headingMatch = line.match(/^##\s+(.+?)\s*$/);
        if (headingMatch !== null) {
            currentSection = headingMatch[1];
            sections.set(currentSection, []);
            continue;
        }
        if (line.startsWith("# ")) {
            currentSection = undefined;
            continue;
        }
        if (currentSection !== undefined) {
            sections.get(currentSection)?.push(line);
        }
    }
    return sections;
}
function normalizeSectionText(lines) {
    return (lines ?? [])
        .map((line) => line.trim())
        .filter((line) => line !== "")
        .join(" ");
}
function sectionBullets(lines) {
    return (0, planning_document_utils_1.parseMarkdownListItems)(lines);
}
function parseDeclaredMilestoneDescriptions(lines) {
    const descriptions = new Map();
    for (const declaration of (0, planning_document_utils_1.parseDeclaredChildSlugBullets)(lines)) {
        descriptions.set(declaration.slug, declaration.description);
    }
    return descriptions;
}
function resolveMilestoneDescription(milestoneSlug, requestedDescription, milestoneDescriptions) {
    const trimmed = requestedDescription?.trim();
    if (trimmed !== undefined && trimmed !== "") {
        return trimmed;
    }
    return milestoneDescriptions.get(milestoneSlug) ?? "";
}
