/**
 * 最终清理 - 处理所有剩余变量
 */
const fs = require('fs');
const babel = require('@babel/core');
const traverse = require('@babel/traverse').default;
const generate = require('@babel/generator').default;
const t = require('@babel/types');

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

console.log('最终清理剩余变量...\n');

// 第一步：文本替换已知模式
const textReplacements = {
    // catch 错误变量
    'v231': 'cleanErr',
    'v254': 'proxyErr',
    'v115': 'readErr',
    'v129': 'writeErr',
    'v145': 'grantErr',
    'v163': 'hostsErr',
    'v176': 'toggleErr',
    'v186': 'statusErr',
    'v198': 'pathErr',
    'v216': 'seamlessErr',
    'v269': 'switchErr',
    'v285': 'resetErr',
    'v288': 'disableErr',
    'v305': 'updateErr',
    'v308': 'envErr',
    'v29': 'modErr',
    'v406': 'findErr',
    'v426': 'checkErr',
    'v429': 'injectErr',
    'v473': 'restoreErr',
    'v496': 'usageErr',
    'v566': 'announceErr',
    'v578': 'versionErr',
    'v584': 'runningErr',
    'v609': 'manualErr',
    'v612': 'beforeErr',
    'v635': 'backupErr',
    'v676': 'patchErr',
    'v690': 'restoreErr2',
    'v695': 'toggleErr2',
    'v699': 'seamlessErr2',
    'v750': 'execErr',
    'v779': 'fsErr',
    'v782': 'finalErr',
    'v80': 'parseErr',
    'v91': 'jsonErr',

    // stdout 解构
    'v262': 'cmdOut',
    'v276': 'procOut',
    'v278': 'psOut',
    'v366': 'regOut',
    'v398': 'lnkOut',
    'v410': 'whereOut',
    'v418': 'findOut',
    'v543': 'lsofOut',
    'v554': 'psOut2',
    'v659': 'execOut',
    'v679': 'shellOut',
    'v781': 'cmdOut2',
    'v49': 'outStr',
    'v42': 'stdOut',
    'v152': 'procStdout',
    'v0': 'initOut',

    // 参数
    'arg4': 'aliasKey',
    'arg12': 'fallbackKey',
    'arg258': 'msgData',
    'arg314': 'toggleArg',
    'arg315': 'silentArg',

    // 本地变量
    'var102': 'proxyLine',
    'var131': 'proxyEntry',
    'var132': 'newHostsContent',
    'var150': 'hostsLines',
    'var263': 'switchResponse',
    'var272': 'remainingCount',
    'var277': 'lockedInfo',
    'var292': 'resetResponse',
    'var301': 'disableResponse',
    'var438': 'usageResponse',
    'var497': 'announcementData',
    'var498': 'announceList',
    'var499': 'latestAnnounce',
    'var504': 'versionResponse',
    'var505': 'latestVersion',
    'var510': 'currentVersion',
    'var549': 'runningPathData',
    'var561': 'cursorRunning',
    'var571': 'switchCheck',
    'var592': 'beforeSwitch',
    'var629': 'seamlessDir',
    'var642': 'seamlessBackup',
    'var717': 'patchedContent',
    'var784': 'finalContent',
};

let count = 0;
for (const [oldName, newName] of Object.entries(textReplacements)) {
    const regex = new RegExp('\\b' + oldName + '\\b', 'g');
    const matches = code.match(regex);
    if (matches) {
        code = code.replace(regex, newName);
        count++;
    }
}
console.log(`文本替换: ${count} 个变量`);

fs.writeFileSync(inputPath, code);

// 验证语法
console.log('\n验证语法...');
try {
    babel.parseSync(code, { sourceType: 'script' });
    console.log('✅ 语法正确');
} catch (e) {
    console.error('❌ 语法错误:', e.message);
}

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

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

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