/**
 * 最后一轮清理
 */
const fs = require('fs');

const inputPath = 'D:/temp/破解/cursorpro-0.4.5/deobfuscated_full/extension/out/webview/provider.js';
let code = fs.readFileSync(inputPath, 'utf8');

const replacements = {
    // 常见模式
    'var295': 'usageData',
    'var261': 'accountInfo',
    'var249': 'tokenData',
    'var443': 'switchInfo',
    'var428': 'statusInfo',
    'var521': 'checkResult',
    'var581': 'versionInfo',

    // 循环变量
    'v98': 'lineIdx',
    'v96': 'charIdx',
    'v82': 'strIdx',
    'v414': 'pathItem',
    'v636': 'codeItem',
    'v684': 'lineContent',

    // 参数
    'arg395': 'lineStr',
    'arg259': 'dataArg',
    'arg257': 'cbArg',

    // 其他
    'var95': 'hostLine',
    'var90': 'hostEntry',
    'var733': 'restoreCode',
    'var724': 'patchContent',

    // 所有剩余的 v 开头的（通常是解构或catch变量）
    'v600': 'tmpErr',
    'v625': 'tmpErr2',
    'v645': 'tmpErr3',
    'v653': 'tmpErr4',
    'v660': 'tmpErr5',
    'v670': 'tmpErr6',
    'v702': 'tmpErr7',
    'v720': 'tmpErr8',
    'v735': 'tmpErr9',
    'v745': 'tmpErr10',
    'v752': 'tmpErr11',
    'v761': 'tmpErr12',
    'v767': 'tmpErr13',
};

let count = 0;
for (const [oldName, newName] of Object.entries(replacements)) {
    const regex = new RegExp('\\b' + oldName + '\\b', 'g');
    const matches = code.match(regex);
    if (matches) {
        code = code.replace(regex, newName);
        console.log(`${oldName} -> ${newName} (${matches.length})`);
        count++;
    }
}

fs.writeFileSync(inputPath, code);
console.log(`\n替换了 ${count} 个`);

const remaining = [...new Set((code.match(/\b(var|arg|v)\d+\b/g) || []))];
console.log(`剩余: ${remaining.length} 个`);

// 显示剩余的
if (remaining.length <= 80) {
    console.log('\n剩余变量详情:');
    remaining.sort().forEach(v => {
        const cnt = (code.match(new RegExp('\\b' + v + '\\b', 'g')) || []).length;
        console.log(`  ${v} (${cnt}次)`);
    });
}
