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

// 更精确的括号计数 - 考虑字符串和注释
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;
  }
}

console.log(`Script section: line ${scriptStart + 1} to ${scriptEnd + 1}`);

// 简单统计 script 区域的括号
let braceCount = 0;
let parenCount = 0;
let bracketCount = 0;
let problematicLines = [];

for (let i = scriptStart; i <= scriptEnd; i++) {
  const line = lines[i];
  // 跳过单行注释
  const codeOnly = line.replace(/\/\/.*$/, '').replace(/\/\*.*?\*\//g, '');
  
  const opens = (codeOnly.match(/{/g) || []).length;
  const closes = (codeOnly.match(/}/g) || []).length;
  const diff = opens - closes;
  
  braceCount += diff;
  
  // 记录大幅度变化的行
  if (Math.abs(diff) >= 2) {
    problematicLines.push({ line: i + 1, diff, count: braceCount, content: line.trim().substring(0, 80) });
  }
}

console.log('\nFinal brace count:', braceCount);

// 逐行追踪括号变化，找到不平衡的位置
console.log('\n=== Tracking brace count at key points ===');
braceCount = 0;
let keyPoints = [];

for (let i = scriptStart; i <= scriptEnd; i++) {
  const line = lines[i];
  const codeOnly = line.replace(/\/\/.*$/, '').replace(/\/\*.*?\*\//g, '');
  
  const opens = (codeOnly.match(/{/g) || []).length;
  const closes = (codeOnly.match(/}/g) || []).length;
  braceCount += opens - closes;
  
  // 记录方法声明和结束
  if (line.match(/\s+(async\s+)?\w+\([^)]*\)\s*{/) || 
      line.match(/^\s+\},\s*$/) ||
      line.match(/^\s+\};\s*$/)) {
    keyPoints.push({ line: i + 1, count: braceCount, content: line.trim().substring(0, 60) });
  }
}

// 找出 count=0 的位置（可能是 methods 对象结束）
console.log('\nPoints where braceCount = 0:');
keyPoints.filter(p => p.count === 0).forEach(p => {
  console.log(`  Line ${p.line}: ${p.content}`);
});

// 找出 count 变化最大的区域
console.log('\nProblematic lines (large brace count changes):');
problematicLines.forEach(p => {
  console.log(`  Line ${p.line}: diff=${p.diff}, count=${p.count}, content: ${p.content}`);
});

// 特别检查 methods 区域
console.log('\n=== Methods area key structure ===');
keyPoints.filter(p => p.line >= 398 && p.line <= 2000).forEach(p => {
  console.log(`  Line ${p.line}: count=${p.count}, ${p.content}`);
});
