"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolvePlanningDestinationSync = resolvePlanningDestinationSync;
const node_path_1 = __importDefault(require("node:path"));
const paths_1 = require("../paths");
const canonical_write_target_1 = require("../paths/canonical-write-target");
const ordered_root_identity_1 = require("../planning/ordered-root-identity");
const types_1 = require("./types");
const PLANNING_ID_PATTERN = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
const GOVERNED_SLICE_ID_PATTERN = /^(?:0\d|[1-9]\d+)-[a-z0-9]+(?:-[a-z0-9]+)*$/;
/** Resolves one level-local planning destination through the canonical write-target guard. */
function resolvePlanningDestinationSync(repositoryRoot, input) {
    const planningRoot = normalizePlanningRoot(input.planningRoot);
    const resolvedIdentity = resolveDestinationIdentity(input);
    const levelRoot = types_1.WORKFLOW_LEVEL_ROOTS[input.level];
    const orderedRootName = resolveOrderedRootName(input, resolvedIdentity.identity);
    const destination = input.level === "basic"
        ? node_path_1.default.posix.join(planningRoot, levelRoot, "tasks", orderedRootName)
        : node_path_1.default.posix.join(planningRoot, levelRoot, orderedRootName);
    const canonical = (0, canonical_write_target_1.resolveCanonicalWriteTargetSync)(repositoryRoot, destination, input.canonicalTargetOptions);
    return {
        ...canonical,
        planningRoot,
        level: input.level,
        levelRoot,
        workId: resolvedIdentity.workId,
        taskId: resolvedIdentity.taskId,
        sliceId: resolvedIdentity.sliceId,
        implementationOrder: input.implementationOrder === undefined
            || input.implementationOrder === null
            ? null
            : orderedRootName.slice(0, orderedRootName.indexOf("-")),
        orderedRootName
    };
}
function resolveOrderedRootName(input, stableId) {
    if (input.level === "governed") {
        if (input.implementationOrder !== undefined && input.implementationOrder !== null) {
            throw new types_1.WorkRoutingError("invalid-planning-identity", "Governed slice identity already owns its ordered prefix; implementationOrder is not applicable.");
        }
        return stableId;
    }
    if (input.implementationOrder === undefined || input.implementationOrder === null) {
        return stableId;
    }
    return (0, ordered_root_identity_1.composeOrderedDirectoryName)(input.level === "basic" ? "basic-root" : "structured-root", stableId, input.implementationOrder);
}
function normalizePlanningRoot(input) {
    try {
        return (0, paths_1.parseRepositoryRelativePath)(input).normalizedPath;
    }
    catch (error) {
        throw new types_1.WorkRoutingError("invalid-planning-destination", error instanceof Error ? error.message : "Planning root is invalid.");
    }
}
function resolveDestinationIdentity(input) {
    if (input.level === "basic") {
        if (isProvided(input.workId) || isProvided(input.sliceId)) {
            throw new types_1.WorkRoutingError("invalid-planning-identity", "Basic task routing must not receive a work or slice identity.");
        }
        const taskId = validatePlanningIdentity(input.taskId, "taskId", "Basic");
        return { identity: taskId, workId: null, taskId, sliceId: null };
    }
    if (input.level === "structured") {
        if (isProvided(input.taskId) || isProvided(input.sliceId)) {
            throw new types_1.WorkRoutingError("invalid-planning-identity", "Structured work-root routing must not receive a task or slice identity.");
        }
        const workId = validatePlanningIdentity(input.workId, "workId", "Structured");
        return { identity: workId, workId, taskId: null, sliceId: null };
    }
    if (isProvided(input.taskId) || isProvided(input.workId)) {
        throw new types_1.WorkRoutingError("invalid-planning-identity", "Governed slice-root routing must not receive a task or work identity.");
    }
    const sliceId = validateGovernedSliceIdentity(input.sliceId);
    return { identity: sliceId, workId: null, taskId: null, sliceId };
}
function validatePlanningIdentity(input, field, level) {
    if (typeof input !== "string" || !PLANNING_ID_PATTERN.test(input)) {
        throw new types_1.WorkRoutingError("invalid-planning-identity", `${level} planning requires '${field}' as a lowercase kebab-case identifier.`);
    }
    return input;
}
function validateGovernedSliceIdentity(input) {
    if (typeof input !== "string" || !GOVERNED_SLICE_ID_PATTERN.test(input)) {
        throw new types_1.WorkRoutingError("invalid-planning-identity", "Governed planning requires 'sliceId' as an ordered lowercase kebab-case identifier with a prefix of at least two digits.");
    }
    return input;
}
function isProvided(input) {
    return input !== undefined && input !== null;
}
