import { hasChildren } from './types.js';
// ─── Internal Helpers ────────────────────────────────────────────────
let issueCounter = 0;
function nextIssueId() {
    return `a11y_${++issueCounter}`;
}
function collectBlocks(node, blocks) {
    if (node.type === 'block') {
        blocks.push(node);
        return;
    }
    if (hasChildren(node)) {
        for (const child of node.children) {
            collectBlocks(child, blocks);
        }
    }
}
function getHeadingLevel(block) {
    if (block.settings.blockType !== 'text')
        return null;
    const level = block.data.headingLevel;
    if (!level)
        return null;
    const match = /^h(\d)$/.exec(level);
    return match ? parseInt(match[1], 10) : null;
}
// ─── Audit ───────────────────────────────────────────────────────────
export function auditAccessibility(tree) {
    issueCounter = 0;
    const blocks = [];
    collectBlocks(tree, blocks);
    const issues = [];
    let lastHeadingLevel = 1;
    for (const block of blocks) {
        const blockType = block.settings.blockType;
        if (blockType === 'image') {
            if (!block.data.alt) {
                issues.push({
                    id: nextIssueId(),
                    blockId: block.id,
                    type: 'missing-alt',
                    severity: 'error',
                    message: 'Image block is missing alt text',
                    autoFixable: false,
                });
            }
        }
        if (blockType === 'hero') {
            if (block.data.image && !block.data.imageAlt && !block.data.alt) {
                issues.push({
                    id: nextIssueId(),
                    blockId: block.id,
                    type: 'missing-alt',
                    severity: 'error',
                    message: 'Hero block image is missing alt text',
                    autoFixable: false,
                });
            }
        }
        if (blockType === 'gallery') {
            const images = block.data.images;
            if (images) {
                for (const img of images) {
                    if (!img.alt) {
                        issues.push({
                            id: nextIssueId(),
                            blockId: block.id,
                            type: 'missing-alt',
                            severity: 'error',
                            message: 'Gallery image is missing alt text',
                            autoFixable: false,
                        });
                    }
                }
            }
        }
        if (blockType === 'text') {
            const level = getHeadingLevel(block);
            if (level !== null) {
                if (level > lastHeadingLevel + 1) {
                    issues.push({
                        id: nextIssueId(),
                        blockId: block.id,
                        type: 'heading-skip',
                        severity: 'warning',
                        message: `Heading level skips from h${lastHeadingLevel} to h${level}`,
                        autoFixable: true,
                    });
                }
                lastHeadingLevel = level;
            }
        }
        if (blockType === 'form') {
            if (!block.data.formId) {
                issues.push({
                    id: nextIssueId(),
                    blockId: block.id,
                    type: 'missing-label',
                    severity: 'warning',
                    message: 'Form block is missing a linked form with field labels',
                    autoFixable: false,
                });
            }
        }
        if (blockType === 'cta') {
            const buttonText = block.data.buttonText;
            if (!buttonText) {
                issues.push({
                    id: nextIssueId(),
                    blockId: block.id,
                    type: 'missing-aria',
                    severity: 'warning',
                    message: 'CTA block is missing button text for aria-label',
                    autoFixable: true,
                });
            }
        }
    }
    return issues;
}
// ─── Fix ─────────────────────────────────────────────────────────────
function deepClone(obj) {
    return JSON.parse(JSON.stringify(obj));
}
export function fixAccessibility(tree) {
    const fixedTree = deepClone(tree);
    const blocks = [];
    collectBlocks(fixedTree, blocks);
    const issues = auditAccessibility(tree);
    let fixedCount = 0;
    let lastHeadingLevel = 1;
    for (const block of blocks) {
        const blockType = block.settings.blockType;
        if (blockType === 'image' && !block.data.alt) {
            block.data.alt = `Image in ${blockType} section`;
            fixedCount++;
        }
        if (blockType === 'hero' && block.data.image && !block.data.imageAlt && !block.data.alt) {
            block.data.alt = 'Image in hero section';
            fixedCount++;
        }
        if (blockType === 'gallery') {
            const images = block.data.images;
            if (images) {
                for (const img of images) {
                    if (!img.alt) {
                        img.alt = 'Image in gallery section';
                        fixedCount++;
                    }
                }
            }
        }
        if (blockType === 'text') {
            const level = getHeadingLevel(block);
            if (level !== null) {
                if (level > lastHeadingLevel + 1) {
                    const corrected = lastHeadingLevel + 1;
                    block.data.headingLevel = `h${corrected}`;
                    lastHeadingLevel = corrected;
                    fixedCount++;
                }
                else {
                    lastHeadingLevel = level;
                }
            }
        }
        if (blockType === 'cta' && !block.data.buttonText) {
            block.data.ariaLabel = block.data.heading || 'Call to action';
            fixedCount++;
        }
    }
    const remainingCount = issues.length - fixedCount;
    return {
        tree: fixedTree,
        report: {
            issues,
            fixedCount,
            remainingCount: remainingCount < 0 ? 0 : remainingCount,
        },
    };
}
//# sourceMappingURL=a11y-fix.js.map