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].trim() === '<script>') {
    scriptStart = i;
  }
  if (lines[i].trim() === '</script>') {
    scriptEnd = i;
    break;
  }
}

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

// 精确的括号计数 - 跳过 import 语句中的括号
let braceCount = 0;
let inImport = false;
let importBraceCount = 0;

for (let i = scriptStart; i <= scriptEnd; i++) {
  const line = lines[i];
  
  // 检测多行 import
  if (line.includes('import ') && line.includes('{') && !line.includes('}')) {
    inImport = true;
    importBraceCount = 1;
    continue;
  }
  
  if (inImport) {
    importBraceCount += (line.match(/{/g) || []).length;
    importBraceCount -= (line.match(/}/g) || []).length;
    if (importBraceCount === 0) {
      inImport = false;
    }
    continue;
  }
  
  // 跳过单行 import
  if (line.includes('import ') && line.includes('from ')) {
    continue;
  }
  
  // 正常代码计数
  const codeOnly = line.replace(/\/\/.*$/, '').replace(/\/\*.*?\*\//g, '');
  const noStrings = codeOnly.replace(/'[^']*'/g, '""').replace(/"[^"]*"/g, '""').replace(/`[^`]*`/g, '""');
  
  const opens = (noStrings.match(/{/g) || []).length;
  const closes = (noStrings.match(/}/g) || []).length;
  braceCount += opens - closes;
  
  // 检查异常
  if (braceCount < 0) {
    console.log(`ERROR: Negative brace count at line ${i + 1}`);
    console.log(`  Content: ${line}`);
  }
}

console.log(`\nFinal brace count: ${braceCount}`);
console.log(braceCount === 0 ? '✓ Balanced!' : '✗ Unbalanced!');

// 检查文件末尾
console.log('\n=== File ending (lines 1990-2000) ===');
for (let i = 1989; i < 2000; i++) {
  console.log(`Line ${i + 1}: ${lines[i]}`);
}
