const fs = require('fs');
const path = 'D:\\project\\zhizhu\\components\\PhotoUploadForm.vue';
const content = fs.readFileSync(path, 'utf8');
const lines = content.split('\n');

// 找到 script 区域
let scriptStart = -1;
let scriptEnd = -1;
for (let i = 0; i < lines.length; i++) {
  if (lines[i].includes('<script>') || lines[i].includes('<script ')) {
    scriptStart = i;
  }
  if (lines[i].includes('</script>')) {
    scriptEnd = i;
    break;
  }
}

// 逐行精确计数
let braceCount = 0;
let sections = [];

for (let i = scriptStart; i <= scriptEnd; i++) {
  const line = lines[i];
  // 去掉注释
  let code = line.replace(/\/\/.*$/, '').replace(/\/\*.*?\*\//g, '');
  
  // 去掉字符串（简单处理）
  code = code.replace(/'[^']*'/g, '""').replace(/"[^"]*"/g, '""').replace(/`[^`]*`/g, '""');
  
  const opens = (code.match(/{/g) || []).length;
  const closes = (code.match(/}/g) || []).length;
  const prevCount = braceCount;
  braceCount += opens - closes;
  
  // 记录关键位置
  if (line.includes('export default') || 
      line.includes('data()') ||
      line.includes('data ()') ||
      line.includes('computed:') ||
      line.includes('watch:') ||
      line.includes('methods:') ||
      line.includes('components:') ||
      line.includes('props:') ||
      line.includes('</script>')) {
    sections.push({ line: i + 1, count: braceCount, content: line.trim() });
  }
}

console.log('=== Key sections ===');
sections.forEach(s => {
  console.log(`Line ${s.line}: count=${s.count}, ${s.content}`);
});

// 逐行追踪，找到 count 第一次变成 1 的位置
console.log('\n=== Finding where braceCount becomes 1 ===');
braceCount = 0;

for (let i = scriptStart; i <= scriptEnd; i++) {
  const line = lines[i];
  let code = line.replace(/\/\/.*$/, '').replace(/\/\*.*?\*\//g, '');
  code = code.replace(/'[^']*'/g, '""').replace(/"[^"]*"/g, '""').replace(/`[^`]*`/g, '""');
  
  const opens = (code.match(/{/g) || []).length;
  const closes = (code.match(/}/g) || []).length;
  const prevCount = braceCount;
  braceCount += opens - closes;
  
  // 检查是否出现异常
  if (braceCount < 0) {
    console.log(`Line ${i + 1}: NEGATIVE count ${braceCount}!`);
    console.log(`  Content: ${line}`);
  }
  
  // 找到 count 变成 1 且之后一直保持 >= 1 的起点
  if (braceCount === 1 && prevCount === 0) {
    console.log(`\nBrace count becomes 1 at line ${i + 1}:`);
    console.log(`  ${line}`);
    console.log(`\nContext (lines ${i-4} to ${i+10}):`);
    for (let j = Math.max(scriptStart, i - 5); j <= Math.min(scriptEnd, i + 10); j++) {
      const marker = j === i ? '>>>' : '   ';
      console.log(`${marker} Line ${j + 1}: ${lines[j]}`);
    }
  }
}
