"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WORKFLOW_TOPOLOGY_PARENT_KINDS = void 0;
exports.projectWorkflowTopology = projectWorkflowTopology;
exports.WORKFLOW_TOPOLOGY_PARENT_KINDS = [
    "work_overview",
    "milestone_overview",
    "phase_overview",
    "slice_overview"
];
const BASIC_PARENTS = Object.freeze([]);
const STRUCTURED_PARENTS = Object.freeze([
    "work_overview"
]);
const GOVERNED_STANDARD_PARENTS = Object.freeze([
    "slice_overview",
    "phase_overview",
    "milestone_overview"
]);
const GOVERNED_LIGHTWEIGHT_PARENTS = Object.freeze([
    "slice_overview"
]);
function projectWorkflowTopology(input) {
    const findings = [];
    const compatibilityEvidence = [];
    const organizationParents = [...(input.organizationParents ?? [])];
    const parentCounts = countParents(organizationParents, findings);
    const workflow = resolveWorkflowLevel(input.workflowLevel ?? null, parentCounts, findings, compatibilityEvidence);
    if (workflow.level !== null) {
        validateParentApplicability(workflow.level, input.planningDepth ?? null, parentCounts, findings);
    }
    const topology = workflow.level === null || findings.length > 0
        ? null
        : createTopology(workflow.level, workflow.source, input.planningDepth ?? null);
    return Object.freeze({
        topology,
        workflowLevel: workflow.level,
        workflowLevelSource: workflow.source,
        compatibilityEvidence: Object.freeze(compatibilityEvidence),
        blockingFindings: Object.freeze(findings)
    });
}
function countParents(parentKinds, findings) {
    const counts = new Map(exports.WORKFLOW_TOPOLOGY_PARENT_KINDS.map((kind) => [kind, 0]));
    for (const kind of parentKinds) {
        if (!isWorkflowTopologyParentKind(kind)) {
            findings.push(finding("unsupported-parent-relationship", "upstream", `Organization parent relationship '${kind}' is unsupported.`));
            continue;
        }
        counts.set(kind, (counts.get(kind) ?? 0) + 1);
    }
    for (const kind of exports.WORKFLOW_TOPOLOGY_PARENT_KINDS) {
        if ((counts.get(kind) ?? 0) > 1) {
            findings.push(finding("duplicate-parent-relationship", `upstream.${kind}`, `Parent relationship '${kind}' is duplicated.`));
        }
    }
    return counts;
}
function resolveWorkflowLevel(value, counts, findings, compatibilityEvidence) {
    if (value !== null) {
        if (!isWorkflowLevel(value)) {
            findings.push(finding("unsupported-workflow-level", "workflow_level", `Workflow level '${value}' is unsupported.`));
            return { level: null, source: null };
        }
        return { level: value, source: "artifact" };
    }
    const hasWork = count(counts, "work_overview") > 0;
    const hasGoverned = [
        "milestone_overview",
        "phase_overview",
        "slice_overview"
    ].some((kind) => count(counts, kind) > 0);
    if (hasWork && hasGoverned) {
        findings.push(finding("ambiguous-legacy-parent-shape", "upstream", "Legacy artifact mixes Structured and Governed parent relationships."));
        return { level: null, source: null };
    }
    const level = hasWork
        ? "structured"
        : hasGoverned
            ? "governed"
            : "basic";
    compatibilityEvidence.push(Object.freeze({
        code: "missing-workflow-level",
        field: "workflow_level",
        sourceValue: null,
        projectedValue: level
    }));
    return { level, source: "legacy-parent-shape" };
}
function validateParentApplicability(level, planningDepth, counts, findings) {
    if (level === "basic") {
        if (exports.WORKFLOW_TOPOLOGY_PARENT_KINDS.some((kind) => count(counts, kind) > 0)) {
            findings.push(finding("wrong-level-parent", "upstream", "Basic artifacts must not declare a planning parent."));
        }
        return;
    }
    if (level === "structured") {
        if (count(counts, "work_overview") !== 1) {
            findings.push(finding("missing-structured-parent", "upstream.work_overview", "Structured artifacts require exactly one work overview parent."));
        }
        if (["milestone_overview", "phase_overview", "slice_overview"].some((kind) => count(counts, kind) > 0)) {
            findings.push(finding("wrong-level-parent", "upstream", "Structured artifacts must not declare Governed hierarchy parents."));
        }
        return;
    }
    if (planningDepth === "lightweight") {
        if (count(counts, "slice_overview") !== 1) {
            findings.push(finding("missing-governed-hierarchy", "upstream.slice_overview", "Lightweight Governed artifacts require exactly one slice overview relationship."));
        }
        if (count(counts, "milestone_overview") > 0
            || count(counts, "phase_overview") > 0) {
            findings.push(finding("wrong-level-parent", "upstream", "Lightweight Governed artifacts must not declare phase or milestone parents."));
        }
    }
    else {
        if (count(counts, "milestone_overview") !== 1) {
            findings.push(finding("missing-governed-milestone", "upstream.milestone_overview", "Governed artifacts require exactly one milestone overview parent."));
        }
        for (const kind of ["phase_overview", "slice_overview"]) {
            if (count(counts, kind) !== 1) {
                findings.push(finding("missing-governed-hierarchy", `upstream.${kind}`, `Governed artifacts require exactly one ${kind.replace("_", " ")} relationship.`));
            }
        }
    }
    if (count(counts, "work_overview") > 0) {
        findings.push(finding("wrong-level-parent", "upstream.work_overview", "Governed artifacts must not declare a Structured work overview parent."));
    }
}
function createTopology(workflowLevel, workflowLevelSource, planningDepth) {
    if (workflowLevel === "basic") {
        return Object.freeze({
            workflowLevel,
            workflowLevelSource,
            variant: "direct",
            organizationParents: BASIC_PARENTS,
            taskSummaryReviewBoundary: "basic-task-summary",
            reconciliationArtifactKind: null,
            reconciliationRequired: false,
            projectReviewCapability: "none"
        });
    }
    if (workflowLevel === "structured") {
        return Object.freeze({
            workflowLevel,
            workflowLevelSource,
            variant: "direct",
            organizationParents: STRUCTURED_PARENTS,
            taskSummaryReviewBoundary: "structured-task-summary",
            reconciliationArtifactKind: null,
            reconciliationRequired: false,
            projectReviewCapability: "separately-authorized"
        });
    }
    if (planningDepth === "lightweight") {
        return Object.freeze({
            workflowLevel,
            workflowLevelSource,
            variant: "lightweight",
            organizationParents: GOVERNED_LIGHTWEIGHT_PARENTS,
            taskSummaryReviewBoundary: "lightweight-task-summary",
            reconciliationArtifactKind: null,
            reconciliationRequired: false,
            projectReviewCapability: "governed"
        });
    }
    return Object.freeze({
        workflowLevel,
        workflowLevelSource,
        variant: "standard",
        organizationParents: GOVERNED_STANDARD_PARENTS,
        taskSummaryReviewBoundary: "milestone-reconciliation",
        reconciliationArtifactKind: "milestone-reconciliation",
        reconciliationRequired: true,
        projectReviewCapability: "governed"
    });
}
function count(counts, kind) {
    return counts.get(kind) ?? 0;
}
function isWorkflowLevel(value) {
    return value === "basic" || value === "structured" || value === "governed";
}
function isWorkflowTopologyParentKind(value) {
    return exports.WORKFLOW_TOPOLOGY_PARENT_KINDS.includes(value);
}
function finding(code, field, message) {
    return Object.freeze({ code, field, message });
}
