"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LEGACY_PLANNING_OVERVIEW_FILENAME = exports.PLANNING_OVERVIEW_LAYERS = void 0;
exports.getCanonicalPlanningOverviewFilename = getCanonicalPlanningOverviewFilename;
exports.composeCanonicalPlanningOverviewPath = composeCanonicalPlanningOverviewPath;
exports.composeLegacyPlanningOverviewPath = composeLegacyPlanningOverviewPath;
exports.getPlanningOverviewCandidates = getPlanningOverviewCandidates;
exports.resolvePlanningOverviewPath = resolvePlanningOverviewPath;
exports.resolvePlanningOverviewInputPath = resolvePlanningOverviewInputPath;
const node_fs_1 = __importDefault(require("node:fs"));
const node_path_1 = __importDefault(require("node:path"));
const path_contract_1 = require("./path-contract");
exports.PLANNING_OVERVIEW_LAYERS = [
    "slice",
    "phase",
    "milestone"
];
const CANONICAL_FILENAMES = {
    slice: "slice-overview.md",
    phase: "phase-overview.md",
    milestone: "milestone-overview.md"
};
exports.LEGACY_PLANNING_OVERVIEW_FILENAME = "overview.md";
function getCanonicalPlanningOverviewFilename(layer) {
    return CANONICAL_FILENAMES[layer];
}
function composeCanonicalPlanningOverviewPath(layer, directoryPath) {
    return composeOverviewPath(directoryPath, getCanonicalPlanningOverviewFilename(layer));
}
function composeLegacyPlanningOverviewPath(directoryPath) {
    return composeOverviewPath(directoryPath, exports.LEGACY_PLANNING_OVERVIEW_FILENAME);
}
function getPlanningOverviewCandidates(layer, directoryPath) {
    const canonicalFilename = getCanonicalPlanningOverviewFilename(layer);
    return [
        {
            kind: "canonical",
            layer,
            filename: canonicalFilename,
            path: composeOverviewPath(directoryPath, canonicalFilename),
            exists: false
        },
        {
            kind: "legacy",
            layer,
            filename: exports.LEGACY_PLANNING_OVERVIEW_FILENAME,
            path: composeOverviewPath(directoryPath, exports.LEGACY_PLANNING_OVERVIEW_FILENAME),
            exists: false
        }
    ];
}
function resolvePlanningOverviewPath(repositoryRoot, options) {
    const candidates = getPlanningOverviewCandidates(options.layer, options.directoryPath)
        .map((candidate) => ({
        ...candidate,
        exists: node_fs_1.default.existsSync((0, path_contract_1.resolveRepositoryRelativePath)(repositoryRoot, (0, path_contract_1.parseRepositoryRelativePath)(candidate.path)))
    }));
    const existingCandidates = candidates.filter((candidate) => candidate.exists);
    if (existingCandidates.length === 0) {
        return {
            status: "missing",
            layer: options.layer,
            selected: null,
            candidates,
            noWriteRequired: true
        };
    }
    if (existingCandidates.length > 1) {
        const conflictPaths = existingCandidates.map((candidate) => candidate.path);
        return {
            status: "conflict",
            layer: options.layer,
            selected: null,
            candidates,
            conflictPaths,
            message: [
                `Planning ${options.layer} overview path is ambiguous.`,
                `Found both canonical and legacy candidates: ${conflictPaths.join(", ")}.`,
                "No write was applied; remove the duplicate or choose an explicit migration workflow before continuing."
            ].join(" "),
            noWriteRequired: true
        };
    }
    const selected = existingCandidates[0];
    return {
        status: selected.kind,
        layer: options.layer,
        selected,
        candidates,
        noWriteRequired: true
    };
}
function resolvePlanningOverviewInputPath(repositoryRoot, options) {
    const parsed = (0, path_contract_1.parseRepositoryRelativePath)(options.inputPath);
    const inputAbsolutePath = (0, path_contract_1.resolveRepositoryRelativePath)(repositoryRoot, parsed);
    const directoryPath = node_path_1.default.posix.dirname(parsed.normalizedPath);
    const resolution = resolvePlanningOverviewPath(repositoryRoot, {
        layer: options.layer,
        directoryPath
    });
    if (resolution.status === "conflict") {
        throw new Error(resolution.message);
    }
    if (node_fs_1.default.existsSync(inputAbsolutePath)) {
        return {
            relative: parsed.normalizedPath,
            absolute: inputAbsolutePath,
            resolution
        };
    }
    if (resolution.selected !== null) {
        return {
            relative: resolution.selected.path,
            absolute: (0, path_contract_1.resolveRepositoryRelativePath)(repositoryRoot, (0, path_contract_1.parseRepositoryRelativePath)(resolution.selected.path)),
            resolution
        };
    }
    return {
        relative: parsed.normalizedPath,
        absolute: inputAbsolutePath,
        resolution
    };
}
function composeOverviewPath(directoryPath, filename) {
    const parsedDirectory = (0, path_contract_1.parseRepositoryRelativePath)(directoryPath);
    return node_path_1.default.posix.join(parsedDirectory.normalizedPath, filename);
}
