// --- Internal builders ---
function computeDoR(specId, items) {
    const requiredItems = items.filter((i) => i.required);
    const passedCount = items.filter((i) => i.status === 'passed').length;
    const isReady = requiredItems.every((i) => i.status === 'passed');
    return {
        specId,
        items,
        passedCount,
        requiredCount: requiredItems.length,
        isReady,
        generatedAt: new Date().toISOString(),
    };
}
function buildScopeAwareDoRItems(type, scope) {
    const items = [];
    if (scope === 'architectural' || scope === 'cross-module') {
        items.push({
            id: 'dor-arch-1',
            description: 'Architecture diagram or ADR included for architectural scope',
            category: 'design',
            required: scope === 'architectural',
            autoCheck: false,
            status: 'pending',
        });
    }
    if (type === 'feature' || type === 'refactor') {
        items.push({
            id: 'dor-type-1',
            description: 'Acceptance criteria reviewed and approved by team',
            category: 'team',
            required: false,
            autoCheck: false,
            status: 'pending',
        });
    }
    return items;
}
function buildCoreDoRItems(spec) {
    return [
        {
            id: 'dor-1',
            description: 'Spec has a clear title and description',
            category: 'spec',
            required: true,
            autoCheck: true,
            status: spec.title.length > 3 ? 'passed' : 'failed',
        },
        {
            id: 'dor-2',
            description: 'Spec type and scope are defined',
            category: 'spec',
            required: true,
            autoCheck: true,
            status: spec.type.length > 0 && spec.scope.length > 0 ? 'passed' : 'failed',
        },
        {
            id: 'dor-3',
            description: 'Difficulty is assessed (1-5)',
            category: 'spec',
            required: true,
            autoCheck: true,
            status: spec.difficulty >= 1 && spec.difficulty <= 5 ? 'passed' : 'failed',
        },
        {
            id: 'dor-4',
            description: 'Estimation is complete (dev hours > 0)',
            category: 'spec',
            required: true,
            autoCheck: true,
            status: spec.estimation.devHours > 0 ? 'passed' : 'failed',
        },
        {
            id: 'dor-5',
            description: 'HU.md file exists and is complete',
            category: 'spec',
            required: true,
            autoCheck: true,
            status: spec.huPath ? 'passed' : 'failed',
        },
    ];
}
function buildTechDoRItems(spec) {
    return [
        {
            id: 'dor-6',
            description: 'FICHA-TECNICA.md file exists and is complete',
            category: 'design',
            required: true,
            autoCheck: true,
            status: spec.fichaTecnicaPath ? 'passed' : 'failed',
        },
        {
            id: 'dor-7',
            description: 'No blocking dependencies (blockedBy is empty)',
            category: 'dependencies',
            required: true,
            autoCheck: true,
            status: spec.blockedBy.length === 0 ? 'passed' : 'failed',
        },
        {
            id: 'dor-8',
            description: 'Risk level is assessed',
            category: 'technical',
            required: true,
            autoCheck: true,
            status: spec.risk.length > 0 ? 'passed' : 'failed',
        },
        {
            id: 'dor-9',
            description: 'Target is defined (frontend/backend/fullstack)',
            category: 'technical',
            required: true,
            autoCheck: true,
            status: spec.target.length > 0 ? 'passed' : 'failed',
        },
        {
            id: 'dor-10',
            description: 'Spec is in approved status',
            category: 'spec',
            required: true,
            autoCheck: true,
            status: spec.status === 'approved' || spec.status === 'implementing' ? 'passed' : 'failed',
        },
        {
            id: 'dor-11',
            description: 'Git branch is configured for this spec',
            category: 'technical',
            required: false,
            autoCheck: true,
            status: spec.gitBranch.length > 0 ? 'passed' : 'failed',
        },
    ];
}
function buildDoRItems(spec) {
    return [
        ...buildCoreDoRItems(spec),
        ...buildTechDoRItems(spec),
        ...buildScopeAwareDoRItems(spec.type, spec.scope),
    ];
}
function buildDoRHumanMessage(title, blockingItems) {
    if (blockingItems.length === 0) {
        return `Spec "${title}" is ready to implement. All required DoR items passed.`;
    }
    const count = blockingItems.length;
    const listed = blockingItems.slice(0, 3).join('; ');
    const extra = count > 3 ? ` (and ${count - 3} more)` : '';
    return `Spec "${title}" is NOT ready. Missing ${count} required item(s): ${listed}${extra}`;
}
// --- Public API ---
/**
 * Generate a Definition of Ready checklist for a spec.
 * Items are type/scope-aware and include auto-checkable conditions.
 */
export function generateDoR(spec) {
    const items = buildDoRItems(spec);
    return computeDoR(spec.id, items);
}
/**
 * Validate a spec's DoR and return a structured result.
 * Blocking = required items that failed; warnings = optional items that failed.
 */
export function validateDoR(spec) {
    const dor = generateDoR(spec);
    const blockingItems = dor.items
        .filter((i) => i.required && i.status === 'failed')
        .map((i) => i.description);
    const warningItems = dor.items
        .filter((i) => !i.required && i.status === 'failed')
        .map((i) => i.description);
    const isValid = blockingItems.length === 0;
    const humanMessage = buildDoRHumanMessage(spec.title, blockingItems);
    const expertSummary = `DoR: ${dor.passedCount}/${dor.items.length} passed | Blocking: ${blockingItems.length} | Warnings: ${warningItems.length}`;
    return { isValid, blockingItems, warningItems, humanMessage, expertSummary };
}
/**
 * Add a custom item to an existing DoR checklist.
 */
export function addCustomDoRItem(dor, item) {
    const items = [...dor.items, item];
    return computeDoR(dor.specId, items);
}
/**
 * Mark DoR items as HIGH PRIORITY when they match known failure patterns.
 */
export function enrichDoRWithLearning(dor, failurePatterns) {
    if (failurePatterns.length === 0) {
        return dor;
    }
    const items = dor.items.map((item) => {
        const isFrequentFailure = failurePatterns.some((p) => item.description.toLowerCase().includes(p.toLowerCase()));
        return isFrequentFailure && !item.description.startsWith('[HIGH PRIORITY]')
            ? { ...item, description: `[HIGH PRIORITY] ${item.description}` }
            : item;
    });
    return computeDoR(dor.specId, items);
}
//# sourceMappingURL=dor.js.map