"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.composeDescriptorWorkflowAction = composeDescriptorWorkflowAction;
exports.composeNonCallableWorkflowAction = composeNonCallableWorkflowAction;
exports.isWorkflowActionAllowed = isWorkflowActionAllowed;
function composeDescriptorWorkflowAction(options) {
    if (options.descriptor.availability !== "callable") {
        throw new TypeError(`Workflow command '${options.descriptor.command}' is ${options.descriptor.availability} and cannot be composed as callable.`);
    }
    const descriptorPreconditions = options.descriptor.preconditions.map((precondition) => {
        if (precondition.kind === "selection") {
            return {
                id: precondition.id,
                description: precondition.description,
                status: options.selectedArtifactPath.length > 0 ? "satisfied" : "blocked",
                expected: "current selected artifact",
                observed: options.selectedArtifactPath || null
            };
        }
        return {
            id: precondition.id,
            description: precondition.description,
            status: "satisfied",
            expected: precondition.required ? "required evidence present" : "optional evidence",
            observed: summarizeObservedState(options.observedState)
        };
    });
    const blockerCount = options.currentActionBlockerCount ?? 0;
    const preconditions = blockerCount === 0 ? descriptorPreconditions : [...descriptorPreconditions, {
            id: "current-action-blockers",
            description: "No blocking finding is relevant to the recommended transition.",
            status: "blocked",
            expected: "0 relevant blockers",
            observed: `${blockerCount} relevant blocker${blockerCount === 1 ? "" : "s"}`
        }];
    return Object.freeze({
        id: options.descriptor.command.replace(/\s+/g, "."),
        callable: true,
        command: options.descriptor.command,
        argv: Object.freeze([...options.argv]),
        writeCapable: options.descriptor.writeCapable,
        effects: options.descriptor.effects,
        authorization: options.descriptor.authorization,
        confirmation: options.descriptor.confirmation,
        selectionRequired: options.descriptor.selectionRequired,
        idempotency: options.descriptor.idempotency,
        preconditions: Object.freeze(preconditions),
        transition: Object.freeze({
            id: options.descriptor.transition.id,
            kind: options.descriptor.transition.kind,
            expectedPostconditions: options.descriptor.transition.expectedPostconditions
        })
    });
}
function composeNonCallableWorkflowAction(options) {
    const id = `guidance.${slug(options.recommendation)}`;
    return Object.freeze({
        id,
        callable: false,
        command: options.command,
        argv: null,
        writeCapable: null,
        effects: null,
        authorization: null,
        confirmation: null,
        selectionRequired: null,
        idempotency: null,
        preconditions: Object.freeze([{
                id: "authoritative-descriptor",
                description: "The recommendation resolves to an authoritative callable command descriptor.",
                status: "blocked",
                expected: "registered command descriptor",
                observed: null
            }]),
        transition: Object.freeze({
            id: `${id}:state-observed`,
            kind: "state-observed",
            expectedPostconditions: Object.freeze([{
                    id: "guidance-preserved",
                    description: "The unresolved recommendation remains explicit and no transition is claimed."
                }])
        }),
        nonCallableReason: options.reason
    });
}
function isWorkflowActionAllowed(action) {
    return action.callable && action.preconditions.every((precondition) => precondition.status === "satisfied");
}
function summarizeObservedState(state) {
    return [
        `status=${state.status ?? "absent"}`,
        `readiness=${state.readiness ?? "absent"}`,
        `implementationStatus=${state.implementationStatus ?? "absent"}`,
        `reconciliationStatus=${state.reconciliationStatus ?? "absent"}`
    ].join(", ");
}
function slug(value) {
    const normalized = value.toLowerCase().replace(/[^a-z0-9]+/g, ".").replace(/^\.|\.$/g, "");
    return normalized.length > 0 ? normalized : "unresolved";
}
