// --- Internal builders ---
function computeDoD(specId, items) {
    const requiredItems = items.filter((i) => i.required);
    const passedCount = items.filter((i) => i.status === 'passed').length;
    const isDone = requiredItems.every((i) => i.status === 'passed');
    return {
        specId,
        items,
        passedCount,
        requiredCount: requiredItems.length,
        isDone,
        completedAt: isDone ? new Date().toISOString() : null,
    };
}
function buildDoDItems(spec, criteriaCount) {
    const hasActuals = spec.actuals !== null;
    const hasCriteria = criteriaCount > 0;
    return [
        {
            id: 'dod-1',
            description: `All ${hasCriteria ? criteriaCount : ''} acceptance criteria implemented`,
            category: 'code',
            required: true,
            autoCheck: false,
            status: 'pending',
        },
        {
            id: 'dod-2',
            description: 'No critical quality issues (validate score >= 80)',
            category: 'quality',
            required: true,
            autoCheck: false,
            status: 'pending',
        },
        {
            id: 'dod-3',
            description: 'Unit tests written and passing for new code',
            category: 'tests',
            required: true,
            autoCheck: false,
            status: 'pending',
        },
        {
            id: 'dod-4',
            description: 'No regressions in existing tests',
            category: 'tests',
            required: true,
            autoCheck: false,
            status: 'pending',
        },
        {
            id: 'dod-5',
            description: 'Linting and formatting clean (zero errors, zero warnings)',
            category: 'quality',
            required: true,
            autoCheck: false,
            status: 'pending',
        },
        {
            id: 'dod-6',
            description: 'Code reviewed and approved',
            category: 'review',
            required: false,
            autoCheck: false,
            status: 'pending',
        },
        {
            id: 'dod-7',
            description: 'Documentation updated (HU.md, FICHA-TECNICA.md, PLAN.md)',
            category: 'docs',
            required: false,
            autoCheck: false,
            status: 'pending',
        },
        {
            id: 'dod-8',
            description: 'Actuals recorded (hours, tokens, cost)',
            category: 'review',
            required: true,
            autoCheck: true,
            status: hasActuals ? 'passed' : 'pending',
        },
    ];
}
function buildDoDHumanMessage(title, blockingItems) {
    /* v8 ignore start -- generateDoD always produces required pending items; zero-blocking path unreachable at runtime */
    if (blockingItems.length === 0) {
        return `Spec "${title}" is done and fully verified.`;
    }
    /* v8 ignore stop */
    const count = blockingItems.length;
    const listed = blockingItems.slice(0, 3).join('; ');
    /* v8 ignore next — validateDoD always produces ≥5 blocking items; count≤3 path unreachable */
    const extra = count > 3 ? ` (and ${count - 3} more)` : '';
    return `Spec "${title}" is NOT done yet. Missing ${count} required item(s): ${listed}${extra}`;
}
// --- Public API ---
/**
 * Generate a Definition of Done checklist derived from spec metadata.
 * criteriaCount reflects how many acceptance criteria exist in HU.md.
 */
export function generateDoD(spec, criteriaCount = 0) {
    const items = buildDoDItems(spec, criteriaCount);
    return computeDoD(spec.id, items);
}
/**
 * Validate a spec's DoD.
 * Both failed AND pending required items are blocking (manual items need explicit confirmation).
 */
export function validateDoD(spec) {
    const dod = generateDoD(spec);
    /* v8 ignore start -- auto-check DoD items are 'passed'|'pending', never 'failed' */
    const failed = dod.items
        .filter((i) => i.required && i.status === 'failed')
        .map((i) => i.description);
    /* v8 ignore stop */
    const pendingRequired = dod.items
        .filter((i) => i.required && !i.autoCheck && i.status === 'pending')
        .map((i) => i.description);
    const blockingItems = [...failed, ...pendingRequired];
    const warningItems = dod.items
        .filter((i) => !i.required && (i.status === 'failed' || i.status === 'pending'))
        .map((i) => i.description);
    const isValid = blockingItems.length === 0;
    const humanMessage = buildDoDHumanMessage(spec.title, blockingItems);
    const expertSummary = `DoD: ${dod.passedCount}/${dod.items.length} passed | Blocking: ${blockingItems.length} | Warnings: ${warningItems.length}`;
    return { isValid, blockingItems, warningItems, humanMessage, expertSummary };
}
/**
 * Add a custom item to an existing DoD checklist.
 */
export function addCustomDoDItem(dod, item) {
    const items = [...dod.items, item];
    return computeDoD(dod.specId, items);
}
//# sourceMappingURL=dod.js.map