"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatLinkedSliceReferenceText = formatLinkedSliceReferenceText;
exports.parseLinkedSliceReference = parseLinkedSliceReference;
exports.serializeLinkedSliceReference = serializeLinkedSliceReference;
exports.validateLinkedSliceReference = validateLinkedSliceReference;
exports.parseRegistryTimestampField = parseRegistryTimestampField;
const paths_1 = require("../paths");
const TIMESTAMP_FIELDS = new Set([
    "registered_at",
    "updated_at",
    "archived_at"
]);
function formatLinkedSliceReferenceText(linkedSlice) {
    return `${linkedSlice.slice} (${linkedSlice.path})`;
}
function parseLinkedSliceReference(input) {
    const linkedSlice = readRecord(input, "Spec index entry field 'linked_slice'");
    const canonicalSlice = linkedSlice.slice;
    const canonicalPath = linkedSlice.path;
    const legacySlice = linkedSlice.id;
    const legacyPath = linkedSlice.overview_path;
    if (typeof canonicalSlice === "string" && typeof canonicalPath === "string") {
        if (legacySlice !== undefined || legacyPath !== undefined) {
            throw new TypeError([
                "Spec index entry field 'linked_slice' must not mix canonical { slice, path } and legacy { id, overview_path } fields.",
                `canonical path: ${canonicalPath}`,
                `legacy overview_path: ${typeof legacyPath === "string" ? legacyPath : "<missing>"}`
            ].join(" "));
        }
        return normalizeLinkedSliceReference({
            slice: readNonEmptyString(canonicalSlice, "Spec index entry field 'linked_slice.slice'"),
            path: readNonEmptyString(canonicalPath, "Spec index entry field 'linked_slice.path'")
        });
    }
    if (typeof legacySlice === "string" && typeof legacyPath === "string") {
        return normalizeLinkedSliceReference({
            slice: readNonEmptyString(legacySlice, "Spec index entry field 'linked_slice.id'"),
            path: readNonEmptyString(legacyPath, "Spec index entry field 'linked_slice.overview_path'")
        });
    }
    throw new TypeError("Spec index entry field 'linked_slice' must use canonical { slice, path } or legacy { id, overview_path } string fields.");
}
function serializeLinkedSliceReference(linkedSlice) {
    return {
        slice: readNonEmptyString(linkedSlice.slice, "Linked slice field 'slice'"),
        path: readNonEmptyString(linkedSlice.path, "Linked slice field 'path'")
    };
}
function validateLinkedSliceReference(input) {
    return parseLinkedSliceReference(input);
}
function parseRegistryTimestampField(value, field) {
    if (!TIMESTAMP_FIELDS.has(field)) {
        throw new TypeError(`Unsupported registry timestamp field '${field}'.`);
    }
    if (typeof value !== "string") {
        throw new TypeError(`Spec index entry field '${field}' must be a non-empty string.`);
    }
    return readNonEmptyString(value, `Spec index entry field '${field}'`);
}
function readRecord(input, label) {
    if (typeof input !== "object" || input === null || Array.isArray(input)) {
        throw new TypeError(`${label} must be an object.`);
    }
    return input;
}
function readNonEmptyString(value, label) {
    if (value.trim() === "") {
        throw new TypeError(`${label} must be a non-empty string.`);
    }
    return value;
}
function normalizeLinkedSliceReference(linkedSlice) {
    const normalizedPath = (0, paths_1.parseRepositoryRelativePath)(linkedSlice.path);
    if (!isPlanningSliceOverviewPath(normalizedPath)) {
        throw new TypeError("Spec index entry field 'linked_slice.path' must point to a planning slice overview file under .day-shift/planning and end with /slice-overview.md or supported legacy /overview.md.");
    }
    return {
        slice: linkedSlice.slice,
        path: normalizedPath.normalizedPath
    };
}
function isPlanningSliceOverviewPath(pathValue) {
    const segments = pathValue.normalizedPath.split("/");
    const filename = segments[segments.length - 1];
    return pathValue.normalizedPath.startsWith(".day-shift/planning/")
        && segments.length >= 4
        && (filename === (0, paths_1.getCanonicalPlanningOverviewFilename)("slice")
            || filename === paths_1.LEGACY_PLANNING_OVERVIEW_FILENAME);
}
