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

// 提取 script 内容
const scriptMatch = content.match(/<script[^>]*>([\s\S]*?)<\/script>/);
if (!scriptMatch) {
  console.log('No script section found');
  process.exit(1);
}

const scriptContent = scriptMatch[1];
const lines = scriptContent.split('\n');

// 更智能的括号计数 - 跳过 import 语句中的 {}
console.log('=== Smart brace counting ===\n');

let braceCount = 0;
let inImport = false;
let importBraceCount = 0;

for (let i = 0; i < lines.length; i++) {
  const line = lines[i];
  
  // 检测是否在 import 语句中
  if (line.includes('import ') && line.includes('from ')) {
    // 单行 import，跳过其中的括号计���
    if (line.includes('{') && line.includes('}')) {
      // 单行 import，不计入
      continue;
    }
  }
  
  // 多行 import 开始
  if (line.includes('import ') && line.includes('{') && !line.includes('}')) {
    inImport = true;
    importBraceCount = 1;
    continue;
  }
  
  // 在 import 中
  if (inImport) {
    importBraceCount += (line.match(/{/g) || []).length;
    importBraceCount -= (line.match(/}/g) || []).length;
    if (importBraceCount === 0) {
      inImport = false;
    }
    continue;
  }
  
  // 正常代码中的括号计数
  const opens = (line.match(/{/g) || []).length;
  const closes = (line.match(/}/g) || []).length;
  braceCount += opens - closes;
  
  // 检查异常变化
  if (braceCount < 0) {
    console.log(`Line ${i + 167}: NEGATIVE brace count!`);
    console.log(`  ${line}`);
  }
  
  // 检查 export default 开始
  if (line.includes('export default {')) {
    console.log(`Line ${i + 167}: export default starts, braceCount = ${braceCount}`);
  }
  
  // 检查 }; 结束
  if (line.trim() === '};') {
    console.log(`Line ${i + 167}: Potential export default end, braceCount = ${braceCount}`);
  }
}

console.log(`\nFinal brace count (excluding imports): ${braceCount}`);

if (braceCount === 0) {
  console.log('Braces balanced!');
} else {
  console.log(`Braces UNBALANCED! Difference: ${braceCount}`);
}

// 检查其他常见语法问题
console.log('\n=== Checking for common syntax errors ===\n');

// 1. 检查是否有缺少逗号的方法声明
for (let i = 0; i < lines.length - 1; i++) {
  const line = lines[i];
  const nextLine = lines[i + 1];
  
  // 方法声明后如果是另一个方法声明，需要逗号
  if (line.trim().endsWith('}') && !line.trim().endsWith('},') && !line.trim().endsWith('};')) {
    // 检查下一行是否是新的方法声明（不是 if/for/while 等）
    if (nextLine.match(/^\s+(async\s+)?\w+\([^)]*\)\s*{/)) {
      if (!nextLine.includes('if (') && !nextLine.includes('for (') && !nextLine.includes('while (') && !nextLine.includes('switch (') && !nextLine.includes('catch (')) {
        console.log(`Line ${i + 167}: Missing comma after method?`);
        console.log(`  ${line}`);
        console.log(`  Next: ${nextLine}`);
      }
    }
  }
}

// 2. 检查是否有未定义的函数调用
const definedFunctions = new Set();
for (let i = 0; i < lines.length; i++) {
  const match = lines[i].match(/^\s+(async\s+)?(\w+)\s*\([^)]*\)\s*{/);
  if (match && !['if', 'for', 'while', 'switch', 'catch'].includes(match[2])) {
    definedFunctions.add(match[2]);
  }
}

console.log('\n=== Checking for calls to undefined methods ===');
for (let i = 0; i < lines.length; i++) {
  const line = lines[i];
  // 检查 this.xxx() 调用
  const calls = line.match(/this\.(\w+)\(/g);
  if (calls) {
    for (const call of calls) {
      const methodName = call.replace('this.', '').replace('(', '');
      if (!definedFunctions.has(methodName)) {
        // 某些是内置方法或外部方法，不报错
        const builtIn = ['$refs', '$emit', '$nextTick', '$set', '$delete', '$forceUpdate', '$mount', '$watch', '$on', '$once', '$off'];
        if (!builtIn.some(b => methodName.includes(b))) {
          console.log(`Line ${i + 167}: Possible undefined method call: this.${methodName}()`);
        }
      }
    }
  }
}