/**
 * 修复 catch 块中不合理的变量名
 */
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('修复 catch 变量名...');

// 定义需要修复的映射 (旧名 -> 新名)
// 需要结合上下文分析
const catchReplacements = [
    // _handleActivate 中的错误
    { old: 'catch (stdOut)', new: 'catch (activateErr)', context: '连接服务器失败' },

    // _handleSwitch 中的错误
    { old: 'catch (outStr)', new: 'catch (switchErr)', context: '连接服务器失败' },

    // _writeAccountToLocal 中的错误
    { old: 'catch (strIdx)', new: 'catch (writeErr)', context: '写入本地失败' },

    // 注册表写入错误
    { old: 'catch (procStdout)', new: 'catch (regWriteErr)', context: '注册表更新失败' },

    // 读取 hosts 文件错误
    { old: 'catch (cmdOut)', new: 'catch (readErr)', context: 'Read hosts error' },

    // 写入 hosts 文件错误
    { old: 'catch (procOut)', new: 'catch (writeErr1)', context: 'Direct write failed' },
    { old: 'catch (psOut) {', new: 'catch (writeErr2) {', context: 'Write still failed' },

    // where 命令错误
    { old: 'catch (regOut)', new: 'catch (whereErr)', context: 'where 命令获取路径失败' },

    // 空 catch 块 - 使用简单的 e
    { old: 'catch (lnkOut) {}', new: 'catch (e) {}', context: null },
    { old: 'catch (whereOut) {}', new: 'catch (e) {}', context: null },
    { old: 'catch (findOut) {}', new: 'catch (e) {}', context: null },

    // _handleInjectSeamless 中写入错误
    { old: 'catch (lsofOut) {\n        console.error("[CursorPro] 写入文件失败:',
      new: 'catch (writeErr) {\n        console.error("[CursorPro] 写入文件失败:', context: null },

    // _handleRestoreSeamless 中的错误
    { old: 'catch (subPath) {\n        if (subPath.code',
      new: 'catch (writeErr) {\n        if (writeErr.code', context: null },
    { old: 'throw subPath;', new: 'throw writeErr;', context: null },

    { old: 'catch (psOut2) {\n      console.error("[CursorPro] Restore error:", psOut2);',
      new: 'catch (restoreErr) {\n      console.error("[CursorPro] Restore error:", restoreErr);', context: null },
    { old: 'if (psOut2.code === "EPERM"', new: 'if (restoreErr.code === "EPERM"', context: null },
    { old: 'psOut2.message ||', new: 'restoreErr.message ||', context: null },

    // _handleToggleSeamless 中的错误
    { old: 'catch (lsofOut) {\n      this._postMessage({\n        \'type\': "seamlessConfigUpdated"',
      new: 'catch (configErr) {\n      this._postMessage({\n        \'type\': "seamlessConfigUpdated"', context: null },
];

let replaced = 0;
catchReplacements.forEach(r => {
    if (code.includes(r.old)) {
        code = code.replace(r.old, r.new);
        replaced++;
        console.log(`  ✓ ${r.old.substring(0, 30)}... -> ${r.new.substring(0, 30)}...`);
    }
});

console.log(`\n修复了 ${replaced} 处`);

// 还需要修复 lsofOut 的引用
code = code.replace(/lsofOut\.code/g, 'writeErr.code');
code = code.replace(/lsofOut\.message/g, 'writeErr.message');
code = code.replace(/throw lsofOut;/g, 'throw writeErr;');

fs.writeFileSync(inputPath, code);

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