"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateSpecRegistryRouting = validateSpecRegistryRouting;
exports.parseSpecRegistryRouting = parseSpecRegistryRouting;
exports.assertSpecRegistryRouting = assertSpecRegistryRouting;
const paths_1 = require("../paths");
const destination_1 = require("./destination");
const traceability_1 = require("./traceability");
function validateSpecRegistryRouting(input) {
    return parseSpecRegistryRouting(input);
}
function parseSpecRegistryRouting(input) {
    const destination = (0, destination_1.parseSpecRegistryDestination)(input.destination);
    const specPath = parseSpecRegistrySpecPath(input.spec_path);
    const linkedSlice = input.linked_slice === undefined ? undefined : (0, traceability_1.parseLinkedSliceReference)(input.linked_slice);
    if (linkedSlice !== undefined) {
        const expectedPlanningPaths = getExpectedPlanningOverviewPaths({
            destination: destination.destination,
            specPath,
            implementationOrder: input.implementation_order
        });
        if (!expectedPlanningPaths.includes(linkedSlice.path)) {
            throw new TypeError(`Linked slice path '${linkedSlice.path}' must match planning overview '${expectedPlanningPaths.join("' or '")}'.`);
        }
    }
    return {
        destination: destination.destination,
        spec_path: specPath.spec_path,
        sliceSlug: specPath.sliceSlug,
        linked_slice: linkedSlice
    };
}
function getExpectedPlanningOverviewPaths(input) {
    if (input.implementationOrder === "00" &&
        input.specPath.sliceSlug === "00-emergent" &&
        input.destination.startsWith("emergent/")) {
        const emergentSlug = input.destination.slice("emergent/".length);
        if (emergentSlug.trim() !== "") {
            const directory = `.day-shift/planning/00-emergent-${emergentSlug}`;
            return [`${directory}/slice-overview.md`, `${directory}/overview.md`];
        }
    }
    const directory = `.day-shift/planning/${input.specPath.sliceSlug}`;
    return [`${directory}/slice-overview.md`, `${directory}/overview.md`];
}
function assertSpecRegistryRouting(input) {
    return parseSpecRegistryRouting(input);
}
function parseSpecRegistrySpecPath(input) {
    const repositoryRelativePath = typeof input === "string" ? (0, paths_1.parseRepositoryRelativePath)(input) : input;
    if (typeof input === "string" && input !== repositoryRelativePath.normalizedPath) {
        throw new TypeError(`Spec path must use the canonical repository-relative path '${repositoryRelativePath.normalizedPath}'.`);
    }
    const segments = repositoryRelativePath.segments;
    if (segments.length < 3 || segments[0] !== ".day-shift" || segments[1] !== "specs") {
        throw new TypeError("Spec path must point to a concrete spec file under .day-shift/specs/<slice-slug>/...");
    }
    const sliceSlug = segments.length === 3 ? stripFileExtension(segments[2]) : segments[2];
    if (sliceSlug.trim() === "") {
        throw new TypeError("Spec path must include a non-empty slice slug under .day-shift/specs/.");
    }
    return {
        spec_path: repositoryRelativePath.normalizedPath,
        sliceSlug,
        repositoryRelativePath
    };
}
function stripFileExtension(value) {
    const extensionIndex = value.lastIndexOf(".");
    if (extensionIndex <= 0) {
        return value;
    }
    return value.slice(0, extensionIndex);
}
