/**
 * 批量修复剩余变量名
 */
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');

console.log('批量修复剩余变量名...');

// 基于上下文分析的替换表
const replacements = {
    // settings/config 相关
    'var243': 'settingsObj',
    'var315': 'config',
    'var316': 'configPath',

    // 路径相关
    'var451': 'workbenchSubPath',
    'var184': 'macPath',
    'var502': 'linuxPath',
    'var602': 'platformPath',
    'var603': 'appPath',
    'var605': 'resourcesPath',
    'var674': 'backupPath',

    // 内容/代码
    'var78': 'htmlContent',
    'var99': 'lines',
    'var114': 'uri',
    'var701': 'injectionCode',
    'var762': 'seamlessCode',

    // 模块辅助
    'var2': 'descriptor',
    'arg36': 'mod',

    // stdout (解构变量)
    'v319': 'wmicOut',
    'v324': 'psOut',
    'v339': 'regOut',
    'v342': 'regOut2',
    'v354': 'lnkOut',
    'v362': 'whereOut',
    'v508': 'lsofOut',
    'v532': 'psOut2',
    'v718': 'psOut3',

    // 错误变量
    'v313': 'e1',
    'v323': 'e2',
    'v325': 'e3',
    'v341': 'e4',
    'v350': 'e5',
    'v356': 'e6',
    'v359': 'e7',
    'v365': 'e8',
    'v373': 'e9',
    'v378': 'e10',
    'v402': 'e11',
    'v412': 'e12',
    'v423': 'e13',
    'v436': 'e14',
    'v446': 'e15',
    'v457': 'e16',
    'v466': 'e17',
    'v482': 'e18',
    'v490': 'e19',
    'v502': 'e20',
    'v520': 'e21',
    'v527': 'e22',
    'v540': 'e23',
    'v555': 'e24',
    'v569': 'e25',
    'v579': 'e26',
    'v590': 'e27',
    'v604': 'e28',
    'v617': 'e29',
    'v629': 'e30',
    'v665': 'e31',
    'v668': 'e32',
    'v680': 'e33',
    'v692': 'e34',
    'v697': 'e35',
    'v707': 'e36',
    'v714': 'e37',
    'v726': 'e38',
    'v742': 'e39',
    'v749': 'e40',
    'v756': 'e41',
    'v766': 'e42',
    'v770': 'e43',
    'v772': 'e44',

    // 循环变量
    'var432': 'entry',

    // 其他
    'v14': 'result',
};

let totalReplaced = 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}处)`);
        totalReplaced++;
    }
}

fs.writeFileSync(inputPath, code);
console.log(`\n共替换 ${totalReplaced} 个变量名`);

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

if (remaining.length > 0 && remaining.length <= 50) {
    console.log('\n剩余变量:');
    remaining.forEach(v => {
        const count = (code.match(new RegExp('\\b' + v + '\\b', 'g')) || []).length;
        console.log(`  ${v}: ${count}次`);
    });
}

console.log('\n✅ 完成');
