import { readChangelog } from '../engine/spec-changelog/index.js';
const VALID_ENTRY_TYPES = [
    'criteria_added',
    'criteria_removed',
    'status_changed',
    'scope_changed',
    'progress_updated',
    'reconciled',
];
function parseFilterTypes(filter) {
    return filter
        .split(',')
        .map((s) => s.trim())
        .filter((t) => VALID_ENTRY_TYPES.includes(t));
}
export async function handleSpecHistory(params) {
    const { specId, specDir, filter } = params;
    try {
        const changelog = await readChangelog({ specDir, specId });
        if (!changelog) {
            return {
                content: [
                    {
                        type: 'text',
                        text: JSON.stringify({
                            specId,
                            entries: [],
                            totalEntries: 0,
                            filtered: false,
                            message: `No hay CHANGELOG.md para ${specId}. El changelog se crea automaticamente cuando se ejecuta reconcile_spec o update_status sobre la spec.`,
                        }, null, 2),
                    },
                ],
            };
        }
        let entries = changelog.entries;
        let filtered = false;
        if (filter) {
            const filterTypes = parseFilterTypes(filter);
            if (filterTypes.length > 0) {
                entries = entries.filter((e) => filterTypes.includes(e.type));
                filtered = true;
            }
        }
        const message = entries.length === 0 && filtered
            ? 'No hay entradas del tipo solicitado'
            : `${entries.length} entrada${entries.length !== 1 ? 's' : ''} encontrada${entries.length !== 1 ? 's' : ''} en el changelog de ${specId}`;
        const result = {
            specId,
            entries,
            totalEntries: entries.length,
            filtered,
            message,
        };
        return {
            content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],
        };
    }
    catch (error) {
        const message = error instanceof Error ? error.message : String(error);
        return {
            content: [{ type: 'text', text: `Error reading spec history: ${message}` }],
            isError: true,
        };
    }
}
//# sourceMappingURL=spec-history.js.map