/**
 * 原版 CursorPro 反编译脚本
 * 完整解码混淆代码
 */
const fs = require('fs');

// 原版代码字符串数组 (从原版代码中提取)
const stringArray = ['cursor', 'XenNcturgdsoIeKrHRBCjEhRpemLPbyB==', 'W7mXWPBdNJZdL1O', 'yXhcLSkQWPa', 'EubBWPfh', 'WRNcKNekna', 'sd/cJCkmWOpdIW', 'W7qLW7bCkW', 'bmkbESkVWPK', 'WRBcVmkNWOldNfpcM0FdTsi', 'W6OCW4hcNCoNCua', 'acLXq0C', 'WROrW7NcS1Gw', 'W6aWW6H5pHiGW6v/WQ5Tb8k2WQmwga', 'nJrJW6ddUmogvCkeaHHyWOu', 'W6ehWRGiW48yfINcRahcOmkZnI8NmuO', 'WRZdOmoxFCky', 'Cmk8w8okW4DEcb7cQSk3WRpdVCk4WQC2WR0kC8oXCmkAW57dV8op', 'uNDGWPHyWRLXnmk1qCkNWQOne8odW7jZW5CcW5SZWOWOW4a', 'W78KAJNdLG', 'eCkJFqBcTCk0W53dGtvUlYWRWQ3dJCkBW6pdJSo3eSozWP7cSItdLd1nW5VdTdxdMejrW6C', 'dCoWWRbuW4KE', 'W7qUWO3dJI/dMa', 'WOORW5pcPLC', 'WOuWW5NcNLa', 'c8owW6m/', 'WQddQrRcHSkW', 'c8opWPpcTaO', 'W4tcNw8cs8koi281W4/dMX0TW5VdU1HKWRrfWPJdHmkHWQWbnSkp', 'DCkzW68eeW', 'WOe6W6ZcPve', 'iCoZWRftW5W3amovWQSF', 'jCosWPfVW6yGn8oHWPe+W6VdHq', 'k391WRVdKq', 'fCoHWOXIFCooWOzTWRxcL3i', 'nGH1A2jK', 'W4OautxdLq', 'F0zDWQbx', 'eMeBzSoYDLRdOSkLoxKx', 'W5xdOCotW7/dHW', 'umohahq2dthcJLj8FxlcV8kD', 'ddDyWOhcRa', 'W5LoWRbMt8klWRj0wa', 'r8kgxb3dLG', 'W5bwWPFcVNW', 'W60MW7nB', 'W6yNWORdUa7dKvpcI34rW64MW6ZcR8oNW5Vc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Nhz1BW', 'W7msWQG6W54CkZFcPGRcQSkXoIiUlupdLai', 'uCoiefC0bWtcJW', 'iLlcSWRcJetcUSooECkonX/cLCkyzCkveq', 'WQ/dH8oMtG', 'Cb1PtSo6WPi8WQGhW5bpDa', 'kCo9u8orvCoCW6WRWRhcVIhdMspdLCoOWPJcU0VdGa', 'pCoSWOZcNdu', 'EIddICkOWOS', 'qvCHetpdOSo3ufrX', 'W6yPWO3dRZ7dNhpcHxeBW6G7W5K', 'WQShW67cK1iFWOD7iSogW4WHW4v2va'];

// Base64 解码
function base64Decode(str) {
    const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/=';
    let output = '';
    let buffer = 0;
    let bits = 0;

    for (let i = 0; i < str.length; i++) {
        const charIndex = chars.indexOf(str[i]);
        if (charIndex === -1) continue;

        buffer = (buffer << 6) | charIndex;
        bits += 6;

        if (bits >= 8) {
            bits -= 8;
            output += String.fromCharCode((buffer >> bits) & 0xFF);
        }
    }

    try {
        return decodeURIComponent(escape(output));
    } catch (e) {
        return output;
    }
}

// RC4 解密
function rc4Decrypt(str, key) {
    let s = [];
    let j = 0;
    let result = '';

    // 初始化 S 盒
    for (let i = 0; i < 256; i++) {
        s[i] = i;
    }

    // 密钥调度算法 (KSA)
    for (let i = 0; i < 256; i++) {
        j = (j + s[i] + key.charCodeAt(i % key.length)) % 256;
        [s[i], s[j]] = [s[j], s[i]];
    }

    // 伪随机生成算法 (PRGA)
    let i = 0;
    j = 0;
    for (let k = 0; k < str.length; k++) {
        i = (i + 1) % 256;
        j = (j + s[i]) % 256;
        [s[i], s[j]] = [s[j], s[i]];
        result += String.fromCharCode(str.charCodeAt(k) ^ s[(s[i] + s[j]) % 256]);
    }

    return result;
}

// 解码函数 - 模拟 _0x3fbb
function decode(index, key) {
    const baseIndex = 0x12e; // 302
    const arrayIndex = index - baseIndex;

    if (arrayIndex < 0 || arrayIndex >= stringArray.length) {
        return `[UNKNOWN:${index}]`;
    }

    const encoded = stringArray[arrayIndex];
    const decoded = base64Decode(encoded);
    const result = rc4Decrypt(decoded, key);

    return result;
}

// 测试解码 - 从原版代码中提取的调用
const testCases = [
    [0x18a, 'yC20', 'DEFAULT_API_URL'],
    [0x151, 'pIfk', 'vscode require'],
    [0x177, ']8Ci', 'workspace'],
    [0x15b, 'j#7G', 'apiUrl config'],
    [0x197, 'Kfdi', '/api/verify'],
    [0x186, 'dpeu', 'POST'],
    [0x141, 'z&uV', '/api/switch'],
    [0x144, '*1hl', '__esModule'],
    [0x1b6, 'mBo%', 'getApiUrl export'],
    [0x146, 'pIfk', 'getOnlineStatus export'],
    [0x145, 'j#7G', 'switchAccount export'],
    [0x154, 'fjkd', 'updateProxyConfig export'],
    [0x13e, '3nFX', 'getSeamlessStatus export'],
    [0x193, 'HShS', 'getUserSwitchStatus export'],
    [0x17f, 'fjkd', 'getSeamlessConfig export'],
    [0x1c2, '0hHe', 'updateSeamlessConfig export'],
    [0x1b7, 'csY!', 'injectSeamless export'],
    [0x13c, 'fjkd', 'getSeamlessAccounts export'],
    [0x173, 'j#7G', 'syncSeamlessAccounts export'],
    [0x175, 'pIfk', 'getSeamlessToken export'],
    [0x152, ')0X3', 'getLatestVersion export'],
];

console.log('=== CursorPro 原版代码解码结果 ===\n');

const results = {};
for (const [index, key, desc] of testCases) {
    const decoded = decode(index, key);
    results[desc] = decoded;
    console.log(`${desc}: "${decoded}"`);
}

console.log('\n=== 关键信息 ===');
console.log('原版 API 地址:', results['DEFAULT_API_URL']);
