"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createUpstreamSpecEntryFromSpecIndex = createUpstreamSpecEntryFromSpecIndex;
exports.normalizeUpstreamSpecsToLocalSpecPaths = normalizeUpstreamSpecsToLocalSpecPaths;
const node_fs_1 = __importDefault(require("node:fs"));
const spec_registry_1 = require("../spec-registry");
const path_resolution_1 = require("../spec-registry/path-resolution");
function createUpstreamSpecEntryFromSpecIndex(entry) {
    if (entry.depth === undefined) {
        throw new TypeError(`Spec '${entry.id}' is a planning-optional draft without a planning depth.`);
    }
    return {
        id: entry.id,
        depth: entry.depth,
        ref: entry.spec_path,
        sourceType: "markdown"
    };
}
function normalizeUpstreamSpecsToLocalSpecPaths(repositoryRoot, entries) {
    const specIndexPath = (0, path_resolution_1.resolveSpecIndexAbsolutePath)(repositoryRoot);
    if (!node_fs_1.default.existsSync(specIndexPath)) {
        return entries;
    }
    const specIndex = (0, spec_registry_1.parseSpecIndexDocumentText)(node_fs_1.default.readFileSync(specIndexPath, "utf8"));
    const specIndexEntriesById = new Map(specIndex.entries.map((entry) => [entry.id, entry]));
    return entries.map((entry) => {
        const registeredSpec = specIndexEntriesById.get(entry.id);
        if (registeredSpec === undefined) {
            return entry;
        }
        return {
            ...entry,
            ref: registeredSpec.spec_path,
            sourceType: entry.sourceType
        };
    });
}
