"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.normalizeSpecificationReferencesSync = normalizeSpecificationReferencesSync;
const spec_readiness_1 = require("../spec-readiness");
const types_1 = require("./types");
/** Resolves canonical revision-bound specification inputs without writing repository state. */
function normalizeSpecificationReferencesSync(repositoryRoot, input) {
    if (input.level === "basic") {
        throw new types_1.WorkRoutingError("invalid-specification-input", "Basic task routing does not normalize specifications for work identity allocation.");
    }
    if (!Array.isArray(input.specificationPaths) || input.specificationPaths.length === 0) {
        throw new types_1.WorkRoutingError("invalid-specification-input", `${capitalize(input.level)} planning requires at least one canonical specification path.`);
    }
    const references = [];
    const observedIdentities = new Set();
    for (const specPath of input.specificationPaths) {
        if (typeof specPath !== "string" || specPath.trim() === "") {
            throw new types_1.WorkRoutingError("invalid-specification-input", "Specification paths must be non-empty repository-relative strings.");
        }
        let resolution;
        try {
            resolution = (0, spec_readiness_1.resolveSpecificationPlanningReadinessSync)(repositoryRoot, specPath);
        }
        catch (error) {
            throw new types_1.WorkRoutingError("invalid-specification-input", error instanceof Error ? error.message : `Specification '${specPath}' could not be resolved.`);
        }
        if (observedIdentities.has(resolution.specId)) {
            throw new types_1.WorkRoutingError("duplicate-specification-identity", `Specification identity '${resolution.specId}' was supplied more than once.`);
        }
        observedIdentities.add(resolution.specId);
        if (resolution.status !== "current-ready" && resolution.status !== "current-waived") {
            throw new types_1.WorkRoutingError("ineligible-specification", `Specification '${resolution.specId}' is not eligible for planning: ${resolution.status}. ${resolution.reason}`);
        }
        references.push({
            specId: resolution.specId,
            specPath: resolution.specPath,
            revision: resolution.currentSpecRevision,
            readiness: resolution.status
        });
    }
    return references;
}
function capitalize(value) {
    return `${value.slice(0, 1).toUpperCase()}${value.slice(1)}`;
}
