/**
 * 修复剩余的未初始化变量
 */
const fs = require('fs');

let code = fs.readFileSync('deobfuscated_full/extension/out/webview/provider.js', 'utf8');

// 基于使用上下文的替换
const replacements = {
    // settings path 相关
    'var235': 'settingsPath',
    'var243': 'settingsObj',
    
    // config 相关  
    'var315': 'config',
    'var316': 'cursorPath',
    
    // 循环/临时变量
    'var2': 'descriptor',
    'var451': 'workbenchPath',
    
    // stdout 相关 (v开头通常是解构)
    'v319': 'wmicStdout',
    'v324': 'psStdout', 
    'v339': 'regStdout',
    'v342': 'regStdout2',
    'v354': 'lnkTarget',
    'v362': 'whereStdout',
    'v508': 'lsofStdout',
    'v532': 'psStdout2',
    'v718': 'psStdout3',
    
    // error catch 变量
    'v313': 'err',
    'v323': 'err',
    'v325': 'err',
    'v341': 'err',
    'v350': 'err',
    'v356': 'err',
    
    // 其他常见
    'var99': 'lines',
    'var78': 'htmlContent',
    'var114': 'uri',
    'var184': 'macPath',
    'var502': 'linuxPath',
    'var602': 'platformPath',
    'var603': 'appPath',
    'var605': 'resourcesPath',
    'var674': 'backupPath',
    'var701': 'injectionCode',
    'var762': 'seamlessCode',
    
    // 函数参数
    'arg36': 'mod',
};

let count = 0;
for (const [old, newName] of Object.entries(replacements)) {
    const regex = new RegExp('\b' + old + '\b', 'g');
    const before = code;
    code = code.replace(regex, newName);
    if (code !== before) count++;
}

fs.writeFileSync('deobfuscated_full/extension/out/webview/provider.js', code);
console.log(`替换了 ${count} 个变量名`);

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