const fs = require('fs');
const path = require('path');

// 读取HTML文件
const htmlFilePath = path.join(__dirname, 'src/app/pages/designer/project-detail/project-detail.html');
let htmlContent = fs.readFileSync(htmlFilePath, 'utf8');

console.log('开始修复@if控制流闭合问题...');

function fixIfBlocks(content) {
    const lines = content.split('\n');
    const stack = [];
    const fixedLines = [];
    
    for (let i = 0; i < lines.length; i++) {
        const line = lines[i];
        const trimmedLine = line.trim();
        
        // 检测@if控制流开始
        if (trimmedLine.includes('@if') && trimmedLine.includes('{')) {
            stack.push({
                type: 'if',
                line: i + 1,
                indent: line.match(/^\s*/)[0] // 保存缩进
            });
            fixedLines.push(line);
            continue;
        }
        
        // 检测控制流结束
        if (trimmedLine === '}' && stack.length > 0) {
            const lastBlock = stack[stack.length - 1];
            if (lastBlock.type === 'if') {
                stack.pop();
            }
            fixedLines.push(line);
            continue;
        }
        
        fixedLines.push(line);
    }
    
    // 为未闭合的@if块添加闭合大括号
    while (stack.length > 0) {
        const unclosed = stack.pop();
        if (unclosed.type === 'if') {
            fixedLines.push(unclosed.indent + '}');
            console.log(`为第${unclosed.line}行的@if块添加闭合大括号`);
        }
    }
    
    return fixedLines.join('\n');
}

// 修复@if控制流
const fixedContent = fixIfBlocks(htmlContent);

// 写回文件
fs.writeFileSync(htmlFilePath, fixedContent, 'utf8');

console.log('@if控制流修复完成！');