// js_polyfill.js - JavaScript 封装层，提供 Web API 兼容的接口
// 自动注入到 never_jscore 运行时环境

// ============================================
// Logging utility (必须在最前面定义)
// ============================================

const LOGGING_ENABLED = typeof globalThis.__NEVER_JSCORE_LOGGING__ !== 'undefined'
    ? globalThis.__NEVER_JSCORE_LOGGING__
    : false;

function log(...args) {
    if (LOGGING_ENABLED) {
        console.log('[never-jscore]', ...args);
    }
}

log('never-jscore polyfill loading...');

// ============================================
// Random Number Generation (Support seeded RNG)
// ============================================

// Override Math.random() to use op_crypto_random (which supports seeding)
if (typeof Math !== 'undefined' && typeof Deno !== 'undefined' && Deno.core.ops.op_crypto_random) {
    const originalMathRandom = Math.random;
    Math.random = function() {
        try {
            return Deno.core.ops.op_crypto_random();
        } catch (e) {
            // Fallback to original if op fails
            return originalMathRandom.call(Math);
        }
    };
    log('Math.random() overridden with seeded RNG support');
}

// Expose cryptoRandom as alias
globalThis.cryptoRandom = function() {
    return Deno.core.ops.op_crypto_random();
};

// ============================================
// Base64 操作 (兼容 Web API)
// ============================================

/**
 * Base64 编码（兼容浏览器 btoa）
 * @param {string} str - 要编码的字符串
 * @returns {string} Base64 编码结果
 */
function btoa(str) {
    return Deno.core.ops.op_base64_encode(String(str));
}

/**
 * Base64 解码（兼容浏览器 atob）
 * @param {string} str - Base64 编码的字符串
 * @returns {string} 解码结果
 */
function atob(str) {
    return Deno.core.ops.op_base64_decode(String(str));
}

// ============================================
// 哈希函数
// ============================================

/**
 * MD5 哈希
 * @param {string} str - 输入字符串
 * @returns {string} MD5 哈希值（hex）
 */
function md5(str) {
    return Deno.core.ops.op_md5(String(str));
}

/**
 * SHA1 哈希
 * @param {string} str - 输入字符串
 * @returns {string} SHA1 哈希值（hex）
 */
function sha1(str) {
    return Deno.core.ops.op_sha1(String(str));
}

/**
 * SHA256 哈希
 * @param {string} str - 输入字符串
 * @returns {string} SHA256 哈希值（hex）
 */
function sha256(str) {
    return Deno.core.ops.op_sha256(String(str));
}

/**
 * SHA512 哈希
 * @param {string} str - 输入字符串
 * @returns {string} SHA512 哈希值（hex）
 */
function sha512(str) {
    return Deno.core.ops.op_sha512(String(str));
}

// ============================================
// HMAC 操作
// ============================================

/**
 * HMAC-MD5
 * @param {string} key - 密钥
 * @param {string} message - 消息
 * @returns {string} HMAC 值（hex）
 */
function hmacMd5(key, message) {
    return Deno.core.ops.op_hmac_md5(String(key), String(message));
}

/**
 * HMAC-SHA1
 * @param {string} key - 密钥
 * @param {string} message - 消息
 * @returns {string} HMAC 值（hex）
 */
function hmacSha1(key, message) {
    return Deno.core.ops.op_hmac_sha1(String(key), String(message));
}

/**
 * HMAC-SHA256
 * @param {string} key - 密钥
 * @param {string} message - 消息
 * @returns {string} HMAC 值（hex）
 */
function hmacSha256(key, message) {
    return Deno.core.ops.op_hmac_sha256(String(key), String(message));
}

// ============================================
// Hex 操作
// ============================================

/**
 * Hex 编码
 * @param {string} str - 输入字符串
 * @returns {string} Hex 编码结果
 */
function hexEncode(str) {
    return Deno.core.ops.op_hex_encode(String(str));
}

/**
 * Hex 解码
 * @param {string} str - Hex 编码的字符串
 * @returns {string} 解码结果
 */
function hexDecode(str) {
    return Deno.core.ops.op_hex_decode(String(str));
}

// ============================================
// URL 编码（覆盖原生实现或提供缺失的实现）
// ============================================

/**
 * URL 编码（编码所有特殊字符）
 * @param {string} str - 输入字符串
 * @returns {string} URL 编码结果
 */
function urlEncode(str) {
    return Deno.core.ops.op_url_encode(String(str));
}

/**
 * URL 解码
 * @param {string} str - URL 编码的字符串
 * @returns {string} 解码结果
 */
function urlDecode(str) {
    return Deno.core.ops.op_url_decode(String(str));
}

/**
 * encodeURIComponent（兼容 Web API）
 * 编码 URI 组件，编码所有特殊字符
 * @param {string} str - 输入字符串
 * @returns {string} 编码结果
 */
if (typeof encodeURIComponent === 'undefined') {
    globalThis.encodeURIComponent = function(str) {
        return Deno.core.ops.op_encode_uri_component(String(str));
    };
}

/**
 * decodeURIComponent（兼容 Web API）
 * 解码 URI 组件
 * @param {string} str - 编码的字符串
 * @returns {string} 解码结果
 */
if (typeof decodeURIComponent === 'undefined') {
    globalThis.decodeURIComponent = function(str) {
        return Deno.core.ops.op_decode_uri_component(String(str));
    };
}

/**
 * encodeURI（兼容 Web API）
 * 编码 URI，保留 URI 结构字符
 * @param {string} str - 输入字符串
 * @returns {string} 编码结果
 */
if (typeof encodeURI === 'undefined') {
    globalThis.encodeURI = function(str) {
        return Deno.core.ops.op_encode_uri(String(str));
    };
}

/**
 * decodeURI（兼容 Web API）
 * 解码 URI
 * @param {string} str - 编码的字符串
 * @returns {string} 解码结果
 */
if (typeof decodeURI === 'undefined') {
    globalThis.decodeURI = function(str) {
        return Deno.core.ops.op_decode_uri_component(String(str));
    };
}

// ============================================
// Crypto 命名空间（类 Node.js crypto 风格）
// ============================================

/**
 * Crypto 工具对象
 */
var CryptoUtils = {
    // 哈希函数
    md5: md5,
    sha1: sha1,
    sha256: sha256,
    sha512: sha512,

    // HMAC
    hmacMd5: hmacMd5,
    hmacSha1: hmacSha1,
    hmacSha256: hmacSha256,

    // Base64
    base64Encode: btoa,
    base64Decode: atob,

    // Hex
    hexEncode: hexEncode,
    hexDecode: hexDecode,

    // URL
    urlEncode: urlEncode,
    urlDecode: urlDecode,

    /**
     * 创建哈希对象（链式调用风格）
     * @param {string} algorithm - 算法名称: 'md5', 'sha1', 'sha256', 'sha512'
     * @returns {Object} 哈希对象
     */
    createHash: function(algorithm) {
        var data = '';
        return {
            update: function(input) {
                data += String(input);
                return this;
            },
            digest: function(encoding) {
                var result;
                switch (algorithm.toLowerCase()) {
                    case 'md5':
                        result = md5(data);
                        break;
                    case 'sha1':
                        result = sha1(data);
                        break;
                    case 'sha256':
                        result = sha256(data);
                        break;
                    case 'sha512':
                        result = sha512(data);
                        break;
                    default:
                        throw new Error('Unsupported hash algorithm: ' + algorithm);
                }

                if (encoding === 'base64') {
                    return btoa(result);
                }
                return result; // 默认返回 hex
            }
        };
    },

    /**
     * 创建 HMAC 对象（链式调用风格）
     * @param {string} algorithm - 算法名称: 'md5', 'sha1', 'sha256'
     * @param {string} key - 密钥
     * @returns {Object} HMAC 对象
     */
    createHmac: function(algorithm, key) {
        var data = '';
        var hmacKey = String(key);
        return {
            update: function(input) {
                data += String(input);
                return this;
            },
            digest: function(encoding) {
                var result;
                switch (algorithm.toLowerCase()) {
                    case 'md5':
                        result = hmacMd5(hmacKey, data);
                        break;
                    case 'sha1':
                        result = hmacSha1(hmacKey, data);
                        break;
                    case 'sha256':
                        result = hmacSha256(hmacKey, data);
                        break;
                    default:
                        throw new Error('Unsupported HMAC algorithm: ' + algorithm);
                }

                if (encoding === 'base64') {
                    return btoa(result);
                }
                return result; // 默认返回 hex
            }
        };
    }
};

// ============================================
// 导出到全局作用域
// ============================================

// 将常用函数导出到全局
globalThis.btoa = btoa;
globalThis.atob = atob;
globalThis.CryptoUtils = CryptoUtils;

// 便捷的全局哈希函数
globalThis.md5 = md5;
globalThis.sha1 = sha1;
globalThis.sha256 = sha256;
globalThis.sha512 = sha512;

// ============================================
// 控制台提示（可选）
// ============================================

// 标记环境已加载扩展
globalThis.__NEVER_JSCORE_EXTENSIONS_LOADED__ = true;

// ============================================
// Early Return / Hook Support
// ============================================

/**
 * 提前返回函数（用于Hook拦截）
 *
 * 在逆向工程中，当你Hook了某个函数并获取到参数后，
 * 可以调用此函数立即返回结果并终止JS执行。
 *
 * @param {*} value - 要返回的值
 * @throws {Error} 抛出特殊错误来中断执行
 *
 * @example
 * // Hook XMLHttpRequest.send
 * const originalSend = XMLHttpRequest.prototype.send;
 * XMLHttpRequest.prototype.send = function(data) {
 *     // 拦截参数并立即返回
 *     __neverjscore_return__({ intercepted: data });
 * };
 *
 * 实现原理：
 * 1. 调用 Rust op (op_early_return) 抛出自定义错误
 * 2. 错误会中断 JS 执行并传播到 Rust 侧
 * 3. Rust 侧捕获错误并提取返回值
 */
// 全局存储 early return 的值（用于绕过 try-catch）
globalThis.__neverjscore_early_return_value__ = null;
globalThis.__neverjscore_early_return_triggered__ = false;

globalThis.__neverjscore_return__ = function(value) {
    // 存储值到全局变量（用于绕过 Akamai try-catch）
    globalThis.__neverjscore_early_return_value__ = value;
    globalThis.__neverjscore_early_return_triggered__ = true;

    // 调用 Rust op, 存储值并标记早期返回
    try {
        const json = JSON.stringify(value);
        Deno.core.ops.op_early_return(json);
    } catch (e) {
        // 如果无法序列化，转为字符串
        Deno.core.ops.op_early_return(JSON.stringify(String(value)));
    }

    // 抛出错误来中断执行
    const error = new Error('[NEVER_JSCORE_EARLY_RETURN]');
    error.name = 'EarlyReturnSignal';
    error.__neverjscore_early_return__ = true;
    throw error;
};

// 简短别名
globalThis.$return = globalThis.__neverjscore_return__;
globalThis.$exit = globalThis.__neverjscore_return__;

log('Early return API loaded: __neverjscore_return__, $return, $exit');

// ============================================
// Timer API - Real async timers using Rust ops
// ============================================

/**
 * Real Node.js-style timers using Rust async operations
 * - setTimeout/setInterval with actual delays
 * - Non-blocking (runs in background via Rust threads)
 * - Integrates with event loop naturally
 */
// Timer tracking for early exit support
const __active_timers__ = new Set();
const __timer_callbacks__ = new Map();

// Function to clear ALL timers (for process.exit() behavior)
globalThis.__neverjscore_clear_all_timers__ = function() {
    log('[EARLY EXIT] Clearing all active timers');
    for (const id of __active_timers__) {
        Deno.core.ops.op_clear_timer(id);
    }
    __active_timers__.clear();
    __timer_callbacks__.clear();
};

if (typeof setTimeout === 'undefined') {
    globalThis.setTimeout = function(callback, delay = 0, ...args) {
        const timerId = Deno.core.ops.op_get_timer_id();
        log(`setTimeout called: id=${timerId}, delay=${delay}ms`);

        // Track active timer
        __active_timers__.add(timerId);
        __timer_callbacks__.set(timerId, callback);

        // Call async Rust op - returns a Promise that resolves after delay
        Deno.core.ops.op_set_timeout_real(timerId, Math.max(0, delay)).then(shouldExecute => {
            // Check if system has exited
            if (globalThis.__neverjscore_exited__) {
                __active_timers__.delete(timerId);
                __timer_callbacks__.delete(timerId);
                return;
            }

            if (shouldExecute && __active_timers__.has(timerId)) {
                log(`Timer executing: id=${timerId}`);
                __active_timers__.delete(timerId);
                __timer_callbacks__.delete(timerId);

                try {
                    callback(...args);
                } catch (e) {
                    // Re-throw EarlyReturnSignal to let event loop catch it
                    if (e && e.__neverjscore_early_return__ === true) {
                        log(`Timer callback triggered early return: id=${timerId}, re-throwing`);
                        throw e;  // Re-throw to propagate to event loop
                    }
                    console.error(`Error in setTimeout callback (id=${timerId}):`, e);
                }
            }
        });

        return timerId;
    };

    globalThis.setInterval = function(callback, delay = 0, ...args) {
        const timerId = Deno.core.ops.op_get_timer_id();
        log(`setInterval called: id=${timerId}, delay=${delay}ms`);

        // Track active timer
        __active_timers__.add(timerId);
        __timer_callbacks__.set(timerId, callback);

        // Recursive function to implement interval behavior
        function scheduleNext() {
            // Check if timer was cleared or system exited
            if (!__active_timers__.has(timerId) || globalThis.__neverjscore_exited__) {
                __active_timers__.delete(timerId);
                __timer_callbacks__.delete(timerId);
                return;
            }

            Deno.core.ops.op_set_interval_real(timerId, Math.max(0, delay)).then(shouldExecute => {
                // Check again after waiting
                if (!__active_timers__.has(timerId) || globalThis.__neverjscore_exited__) {
                    __active_timers__.delete(timerId);
                    __timer_callbacks__.delete(timerId);
                    return;
                }

                if (shouldExecute) {
                    log(`Timer executing: id=${timerId}`);
                    try {
                        callback(...args);
                    } catch (e) {
                        // Re-throw EarlyReturnSignal to let event loop catch it
                        if (e && e.__neverjscore_early_return__ === true) {
                            log(`Timer callback triggered early return: id=${timerId}, re-throwing`);
                            __active_timers__.delete(timerId);
                            __timer_callbacks__.delete(timerId);
                            throw e;  // Re-throw to propagate to event loop
                        }
                        console.error(`Error in setInterval callback (id=${timerId}):`, e);
                    }

                    // Re-schedule for next interval
                    scheduleNext();
                }
            });
        }

        // Start the interval
        scheduleNext();

        return timerId;
    };

    globalThis.clearTimeout = function(id) {
        log(`clearTimeout called: id=${id}`);
        if (id !== undefined && id !== null) {
            __active_timers__.delete(id);
            __timer_callbacks__.delete(id);
            Deno.core.ops.op_clear_timer(id);
        }
    };

    globalThis.clearInterval = function(id) {
        log(`clearInterval called: id=${id}`);
        if (id !== undefined && id !== null) {
            __active_timers__.delete(id);
            __timer_callbacks__.delete(id);
            Deno.core.ops.op_clear_timer(id);
        }
    };

    log('Real async timers loaded: setTimeout, setInterval (Rust-backed)');
}

// ============================================
// Worker API polyfill (fake single-threaded)
// ============================================

/**
 * Fake Worker - single-threaded, for API detection
 */
if (typeof Worker === 'undefined') {
    class Worker {
        constructor(scriptURL, options = {}) {
            this._id = Math.floor(Math.random() * 1000000);
            this._scriptURL = scriptURL;
            this._terminated = false;
            this.onmessage = null;
            this.onerror = null;
        }

        postMessage(message) {
            if (this._terminated) {
                throw new Error('Worker has been terminated');
            }

            // Fake processing - immediately trigger onmessage if set
            if (this.onmessage) {
                setTimeout(() => {
                    if (!this._terminated && this.onmessage) {
                        const event = {
                            type: 'message',
                            data: message,
                            origin: '',
                        };
                        this.onmessage(event);
                    }
                }, 0);
            }
        }

        terminate() {
            this._terminated = true;
        }

        addEventListener(type, listener) {
            if (type === 'message') {
                this.onmessage = listener;
            } else if (type === 'error') {
                this.onerror = listener;
            }
        }

        removeEventListener(type, listener) {
            if (type === 'message' && this.onmessage === listener) {
                this.onmessage = null;
            } else if (type === 'error' && this.onerror === listener) {
                this.onerror = null;
            }
        }
    }

    globalThis.Worker = Worker;
    log('Worker API loaded (single-threaded mock)');
}

// ============================================
// Node.js Compatibility APIs
// ============================================

// queueMicrotask - standard microtask API
if (typeof queueMicrotask === 'undefined') {
    globalThis.queueMicrotask = function(callback) {
        log('queueMicrotask called');
        Promise.resolve().then(callback).catch(e => {
            console.error('Error in microtask:', e);
        });
    };
    log('queueMicrotask API loaded');
}

// setImmediate/clearImmediate - Node.js macrotask API
if (typeof setImmediate === 'undefined') {
    globalThis.setImmediate = function(callback, ...args) {
        log('setImmediate called');
        return setTimeout(callback, 0, ...args);
    };

    globalThis.clearImmediate = function(id) {
        log(`clearImmediate called: id=${id}`);
        clearTimeout(id);
    };

    log('setImmediate/clearImmediate APIs loaded');
}

// ============================================
// process object - Node.js global
// ============================================

if (typeof process === 'undefined' || !process.version) {
    // 读取所有环境变量
    let envVars = {};
    try {
        if (typeof Deno !== 'undefined' && Deno.core.ops.op_getenv_all) {
            envVars = JSON.parse(Deno.core.ops.op_getenv_all());
        }
    } catch (e) {
        log('Failed to read environment variables:', e);
    }

    globalThis.process = {
        version: 'v22.0.0',  // Mimic Node.js 22
        versions: {
            node: '22.0.0',
            v8: '12.0.267.8',
            uv: '1.44.2',
            zlib: '1.2.11',
            modules: '120',
            openssl: '3.0.10'
        },
        platform: (typeof Deno !== 'undefined' && typeof Deno.build !== 'undefined')
            ? Deno.build.os
            : 'win32',
        arch: 'x64',
        env: envVars,  // 使用真实的环境变量
        argv: ['node', 'script.js'],
        execPath: '/usr/bin/node',
        execArgv: [],
        pid: Math.floor(Math.random() * 100000) + 1000,
        ppid: 1,
        title: 'node',

        cwd: function() {
            if (typeof Deno !== 'undefined' && Deno.core.ops.op_fs_cwd) {
                try {
                    return Deno.core.ops.op_fs_cwd();
                } catch (e) {
                    log('Failed to get cwd from op:', e);
                }
            }
            return '.';
        },

        chdir: function(directory) {
            log(`process.chdir called: ${directory}`);
            throw new Error('process.chdir() is not implemented');
        },

        nextTick: function(callback, ...args) {
            log('process.nextTick called');
            queueMicrotask(() => {
                try {
                    callback(...args);
                } catch (e) {
                    console.error('Error in nextTick callback:', e);
                }
            });
        },

        exit: function(code = 0) {
            log(`process.exit called: code=${code}`);
            // 使用 __neverjscore_return__ 实现真正的退出
            __neverjscore_return__({
                __process_exit__: true,
                exitCode: code,
                message: `Process exited with code ${code}`
            });
        },

        hrtime: function(previousTimestamp) {
            const now = performance.now();
            const seconds = Math.floor(now / 1000);
            const nanoseconds = Math.floor((now % 1000) * 1e6);

            if (previousTimestamp) {
                return [
                    seconds - previousTimestamp[0],
                    nanoseconds - previousTimestamp[1]
                ];
            }
            return [seconds, nanoseconds];
        },

        uptime: function() {
            return Math.floor(performance.now() / 1000);
        },

        memoryUsage: function() {
            return {
                rss: 50 * 1024 * 1024,      // 50MB
                heapTotal: 20 * 1024 * 1024, // 20MB
                heapUsed: 10 * 1024 * 1024,  // 10MB
                external: 1 * 1024 * 1024,   // 1MB
                arrayBuffers: 0
            };
        }
    };

    log('process object loaded (Node.js 22 compatible)');
}

// Ensure process.nextTick exists even if process was already defined
if (typeof process !== 'undefined' && typeof process.nextTick === 'undefined') {
    process.nextTick = function(callback, ...args) {
        log('process.nextTick called (fallback)');
        queueMicrotask(() => {
            try {
                callback(...args);
            } catch (e) {
                console.error('Error in nextTick callback:', e);
            }
        });
    };
}

// ============================================
// Buffer class - Node.js core
// ============================================

if (typeof Buffer === 'undefined') {
    class Buffer extends Uint8Array {
        constructor(arg, encodingOrOffset, length) {
            if (typeof arg === 'string') {
                // Buffer.from(string, encoding)
                const encoding = encodingOrOffset || 'utf8';
                const bytes = Buffer._encodeString(arg, encoding);
                super(bytes);
            } else if (typeof arg === 'number') {
                // Buffer.alloc(size)
                super(arg);
            } else if (Array.isArray(arg)) {
                // Buffer.from(array)
                super(arg);
            } else if (arg instanceof ArrayBuffer || ArrayBuffer.isView(arg)) {
                // Buffer.from(arrayBuffer) or Buffer.from(typedArray)
                super(arg, encodingOrOffset, length);
            } else {
                super(0);
            }
        }

        static _encodeString(str, encoding) {
            encoding = encoding.toLowerCase();

            if (encoding === 'utf8' || encoding === 'utf-8') {
                // Don't use TextEncoder to avoid circular dependency
                // Direct UTF-8 encoding implementation
                const bytes = [];
                for (let i = 0; i < str.length; i++) {
                    let code = str.charCodeAt(i);
                    if (code < 128) {
                        bytes.push(code);
                    } else if (code < 2048) {
                        bytes.push(0xC0 | (code >> 6), 0x80 | (code & 0x3F));
                    } else if (code < 65536) {
                        bytes.push(
                            0xE0 | (code >> 12),
                            0x80 | ((code >> 6) & 0x3F),
                            0x80 | (code & 0x3F)
                        );
                    } else {
                        bytes.push(
                            0xF0 | (code >> 18),
                            0x80 | ((code >> 12) & 0x3F),
                            0x80 | ((code >> 6) & 0x3F),
                            0x80 | (code & 0x3F)
                        );
                    }
                }
                return bytes;
            } else if (encoding === 'hex') {
                const bytes = [];
                for (let i = 0; i < str.length; i += 2) {
                    bytes.push(parseInt(str.substr(i, 2), 16));
                }
                return bytes;
            } else if (encoding === 'base64') {
                const decoded = atob(str);
                const bytes = [];
                for (let i = 0; i < decoded.length; i++) {
                    bytes.push(decoded.charCodeAt(i));
                }
                return bytes;
            } else if (encoding === 'latin1' || encoding === 'binary') {
                const bytes = [];
                for (let i = 0; i < str.length; i++) {
                    bytes.push(str.charCodeAt(i) & 0xFF);
                }
                return bytes;
            } else if (encoding === 'ascii') {
                const bytes = [];
                for (let i = 0; i < str.length; i++) {
                    bytes.push(str.charCodeAt(i) & 0x7F);
                }
                return bytes;
            }

            // Default to UTF-8
            return Buffer._encodeString(str, 'utf8');
        }

        toString(encoding = 'utf8', start = 0, end = this.length) {
            encoding = encoding.toLowerCase();
            const slice = this.slice(start, end);

            log(`Buffer.toString called: encoding=${encoding}, length=${slice.length}`);

            if (encoding === 'utf8' || encoding === 'utf-8') {
                // Direct UTF-8 decoding (don't use TextDecoder to avoid circular dependency issues)
                let result = '';
                for (let i = 0; i < slice.length; i++) {
                    const byte = slice[i];
                    if (byte < 128) {
                        result += String.fromCharCode(byte);
                    } else if (byte < 224) {
                        result += String.fromCharCode(
                            ((byte & 0x1F) << 6) | (slice[++i] & 0x3F)
                        );
                    } else if (byte < 240) {
                        result += String.fromCharCode(
                            ((byte & 0x0F) << 12) |
                            ((slice[++i] & 0x3F) << 6) |
                            (slice[++i] & 0x3F)
                        );
                    } else {
                        const codePoint = ((byte & 0x07) << 18) |
                            ((slice[++i] & 0x3F) << 12) |
                            ((slice[++i] & 0x3F) << 6) |
                            (slice[++i] & 0x3F);
                        result += String.fromCodePoint(codePoint);
                    }
                }
                return result;
            } else if (encoding === 'hex') {
                return Array.from(slice)
                    .map(b => b.toString(16).padStart(2, '0'))
                    .join('');
            } else if (encoding === 'base64') {
                return btoa(String.fromCharCode(...slice));
            } else if (encoding === 'latin1' || encoding === 'binary') {
                return String.fromCharCode(...slice);
            } else if (encoding === 'ascii') {
                return String.fromCharCode(...slice.map(b => b & 0x7F));
            }

            // Default to UTF-8
            return this.toString('utf8', start, end);
        }

        write(string, offset = 0, length = this.length - offset, encoding = 'utf8') {
            const bytes = Buffer._encodeString(string, encoding);
            const bytesToWrite = Math.min(bytes.length, length);

            for (let i = 0; i < bytesToWrite; i++) {
                this[offset + i] = bytes[i];
            }

            return bytesToWrite;
        }

        static from(value, encodingOrOffset, length) {
            return new Buffer(value, encodingOrOffset, length);
        }

        static alloc(size, fill, encoding) {
            const buf = new Buffer(size);
            if (fill !== undefined) {
                buf.fill(fill, 0, size, encoding);
            }
            return buf;
        }

        static allocUnsafe(size) {
            return new Buffer(size);
        }

        static allocUnsafeSlow(size) {
            return new Buffer(size);
        }

        static isBuffer(obj) {
            return obj instanceof Buffer;
        }

        static isEncoding(encoding) {
            const validEncodings = ['utf8', 'utf-8', 'hex', 'base64', 'ascii', 'latin1', 'binary', 'ucs2', 'ucs-2', 'utf16le', 'utf-16le'];
            return validEncodings.includes(String(encoding).toLowerCase());
        }

        static concat(list, totalLength) {
            if (totalLength === undefined) {
                totalLength = list.reduce((sum, buf) => sum + buf.length, 0);
            }

            const result = Buffer.alloc(totalLength);
            let offset = 0;

            for (const buf of list) {
                result.set(buf, offset);
                offset += buf.length;
                if (offset >= totalLength) break;
            }

            return result;
        }

        static byteLength(string, encoding = 'utf8') {
            if (typeof string !== 'string') {
                return string.length;
            }
            return Buffer._encodeString(string, encoding).length;
        }
    }

    globalThis.Buffer = Buffer;
    log('Buffer class loaded (Node.js compatible)');
}

// ============================================
// TextEncoder/TextDecoder - Standard encoding APIs
// ============================================

if (typeof TextEncoder === 'undefined') {
    class TextEncoder {
        constructor() {
            this.encoding = 'utf-8';
        }

        encode(str) {
            // Use Buffer's UTF-8 encoding
            if (typeof Buffer !== 'undefined' && Buffer._encodeString) {
                return new Uint8Array(Buffer._encodeString(str, 'utf8'));
            }

            // Fallback
            const bytes = [];
            for (let i = 0; i < str.length; i++) {
                let code = str.charCodeAt(i);
                if (code < 128) {
                    bytes.push(code);
                } else if (code < 2048) {
                    bytes.push(0xC0 | (code >> 6), 0x80 | (code & 0x3F));
                } else {
                    bytes.push(
                        0xE0 | (code >> 12),
                        0x80 | ((code >> 6) & 0x3F),
                        0x80 | (code & 0x3F)
                    );
                }
            }
            return new Uint8Array(bytes);
        }
    }

    globalThis.TextEncoder = TextEncoder;
    log('TextEncoder loaded');
}

if (typeof TextDecoder === 'undefined') {
    class TextDecoder {
        constructor(encoding = 'utf-8', options = {}) {
            this.encoding = encoding.toLowerCase();
            this.fatal = options.fatal || false;
            this.ignoreBOM = options.ignoreBOM || false;
        }

        decode(input) {
            if (!input) return '';

            const bytes = new Uint8Array(input);
            let result = '';

            if (this.encoding === 'utf-8' || this.encoding === 'utf8') {
                // UTF-8 decoding
                for (let i = 0; i < bytes.length; i++) {
                    const byte = bytes[i];
                    if (byte < 128) {
                        result += String.fromCharCode(byte);
                    } else if (byte < 224) {
                        result += String.fromCharCode(
                            ((byte & 0x1F) << 6) | (bytes[++i] & 0x3F)
                        );
                    } else if (byte < 240) {
                        result += String.fromCharCode(
                            ((byte & 0x0F) << 12) |
                            ((bytes[++i] & 0x3F) << 6) |
                            (bytes[++i] & 0x3F)
                        );
                    } else {
                        // 4-byte character
                        const codePoint = ((byte & 0x07) << 18) |
                            ((bytes[++i] & 0x3F) << 12) |
                            ((bytes[++i] & 0x3F) << 6) |
                            (bytes[++i] & 0x3F);
                        result += String.fromCodePoint(codePoint);
                    }
                }
            } else {
                // Other encodings: just convert directly
                result = String.fromCharCode(...bytes);
            }

            return result;
        }
    }

    globalThis.TextDecoder = TextDecoder;
    log('TextDecoder loaded');
}

// ============================================
// Crypto random functions
// ============================================

/**
 * Crypto namespace with random functions
 */
if (typeof crypto === 'undefined') {
    globalThis.crypto = {};
}

if (!crypto.randomUUID) {
    crypto.randomUUID = function() {
        return Deno.core.ops.op_crypto_random_uuid();
    };
}

if (!crypto.getRandomValues) {
    crypto.getRandomValues = function(typedArray) {
        // Generate random hex string and fill typed array
        const length = typedArray.length;
        const hexString = Deno.core.ops.op_crypto_get_random_values(length);

        for (let i = 0; i < length; i++) {
            const hex = hexString.substr(i * 2, 2);
            typedArray[i] = parseInt(hex, 16);
        }

        return typedArray;
    };
}

// ============================================
// Fetch API polyfill
// ============================================

/**
 * Response class (simplified for Node.js compatibility)
 */
class Response {
    constructor(body, init = {}) {
        this._body = body;
        this._bodyBinary = init.bodyBinary || null;  // Base64 encoded binary data
        this.status = init.status || 200;
        this.statusText = init.statusText || 'OK';
        this.ok = this.status >= 200 && this.status < 300;
        this.headers = new Headers(init.headers || {});
        this.url = init.url || '';
    }

    async text() {
        return Promise.resolve(this._body);
    }

    async json() {
        try {
            return Promise.resolve(JSON.parse(this._body));
        } catch(e) {
            throw new Error('Invalid JSON: ' + e.message);
        }
    }

    async blob() {
        // 简化实现：返回文本
        return Promise.resolve(this._body);
    }

    async arrayBuffer() {
        // 如果有二进制数据（Base64 编码），解码并返回 ArrayBuffer
        if (this._bodyBinary) {
            try {
                // 解码 Base64 到字节数组
                const buffer = Buffer.from(this._bodyBinary, 'base64');
                // 返回底层 ArrayBuffer
                return Promise.resolve(buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength));
            } catch (e) {
                throw new Error('Failed to decode binary response: ' + e.message);
            }
        }

        // 否则将文本编码为 ArrayBuffer
        const encoder = new TextEncoder();
        return Promise.resolve(encoder.encode(this._body).buffer);
    }

    clone() {
        return new Response(this._body, {
            bodyBinary: this._bodyBinary,
            status: this.status,
            statusText: this.statusText,
            headers: this.headers,
            url: this.url
        });
    }
}

/**
 * Headers class (simplified)
 */
class Headers {
    constructor(init = {}) {
        this._headers = {};
        if (init) {
            for (let key in init) {
                this.set(key, init[key]);
            }
        }
    }

    get(name) {
        return this._headers[name.toLowerCase()] || null;
    }

    set(name, value) {
        this._headers[name.toLowerCase()] = String(value);
    }

    has(name) {
        return name.toLowerCase() in this._headers;
    }

    delete(name) {
        delete this._headers[name.toLowerCase()];
    }

    forEach(callback, thisArg) {
        for (let name in this._headers) {
            callback.call(thisArg, this._headers[name], name, this);
        }
    }

    keys() {
        return Object.keys(this._headers);
    }

    values() {
        return Object.values(this._headers);
    }

    entries() {
        return Object.entries(this._headers);
    }
}

/**
 * Fetch API (Node.js/Browser compatible)
 */
if (typeof fetch === 'undefined') {
    globalThis.fetch = async function(url, options = {}) {
        // 构建请求选项
        const method = (options.method || 'GET').toUpperCase();
        const headers = {};

        // 处理 headers
        if (options.headers) {
            if (options.headers instanceof Headers) {
                options.headers.forEach((value, key) => {
                    headers[key] = value;
                });
            } else if (typeof options.headers === 'object') {
                for (let key in options.headers) {
                    headers[key] = options.headers[key];
                }
            }
        }

        // 处理 body
        let body = '';
        if (options.body !== undefined) {
            if (typeof options.body === 'string') {
                body = options.body;
            } else if (typeof options.body === 'object') {
                // 自动序列化 JSON
                body = JSON.stringify(options.body);
                if (!headers['content-type'] && !headers['Content-Type']) {
                    headers['Content-Type'] = 'application/json';
                }
            }
        }

        // 构建 Rust 端的请求选项
        const fetchOptions = {
            method: method,
            headers: headers,
            body: body,
            timeout: options.timeout || 30000
        };

        // 调用 Rust op
        const responseJson = Deno.core.ops.op_fetch(url, JSON.stringify(fetchOptions));
        const responseData = JSON.parse(responseJson);

        // 检查错误
        if (responseData.error) {
            throw new Error('Fetch failed: ' + responseData.error);
        }

        // 构建 Response 对象
        const response = new Response(responseData.body, {
            bodyBinary: responseData.bodyBinary,  // 添加二进制数据支持
            status: responseData.status,
            statusText: responseData.statusText,
            headers: responseData.headers,
            url: url
        });

        return response;
    };

    globalThis.Response = Response;
    globalThis.Headers = Headers;
}

// ============================================
// File System API for require()
// ============================================

/**
 * 简化的 fs 模块（Node.js风格）
 */
const fs = {
    readFileSync: function(path, encoding = 'utf8') {
        const content = Deno.core.ops.op_read_file_sync(path);
        if (content.startsWith('Error:')) {
            throw new Error(content);
        }
        return content;
    },

    writeFileSync: function(path, content) {
        const result = Deno.core.ops.op_write_file_sync(path, String(content));
        if (result !== 'OK') {
            throw new Error(result);
        }
    },

    existsSync: function(path) {
        return Deno.core.ops.op_file_exists(path);
    },

    statSync: function(path) {
        return {
            isFile: () => Deno.core.ops.op_is_file(path),
            isDirectory: () => Deno.core.ops.op_is_directory(path)
        };
    },

    readdirSync: function(path) {
        const result = Deno.core.ops.op_readdir(path);
        if (result.startsWith('Error:')) {
            throw new Error(result);
        }
        return JSON.parse(result);
    }
};

/**
 * 简化的 path 模块（Node.js风格）
 */
const path = {
    resolve: function(...paths) {
        if (paths.length === 0) {
            return Deno.core.ops.op_getcwd();
        }
        let resolved = paths[0];
        for (let i = 1; i < paths.length; i++) {
            resolved = Deno.core.ops.op_join_path(resolved, paths[i]);
        }
        return Deno.core.ops.op_resolve_path(resolved);
    },

    join: function(...paths) {
        let result = paths[0] || '';
        for (let i = 1; i < paths.length; i++) {
            result = Deno.core.ops.op_join_path(result, paths[i]);
        }
        return result;
    },

    dirname: function(p) {
        return Deno.core.ops.op_dirname(p);
    },

    basename: function(p) {
        return Deno.core.ops.op_basename(p);
    },

    extname: function(p) {
        const base = Deno.core.ops.op_basename(p);
        const lastDot = base.lastIndexOf('.');
        if (lastDot === -1 || lastDot === 0) return '';
        return base.substring(lastDot);
    },

    isAbsolute: function(p) {
        // Windows: C:\ or \\
        // Unix: /
        return /^([a-zA-Z]:\\|\\\\|\\|\/)/.test(p);
    },

    parse: function(p) {
        // 简化的 path.parse 实现
        const isAbsolute = this.isAbsolute(p);
        const dir = this.dirname(p);
        const base = this.basename(p);
        const ext = this.extname(p);
        const name = ext ? base.substring(0, base.length - ext.length) : base;

        // 确定根路径
        let root = '';
        if (isAbsolute) {
            if (p.match(/^[a-zA-Z]:\\/)) {
                // Windows: C:\
                root = p.substring(0, 3);
            } else if (p.startsWith('\\\\') || p.startsWith('//')) {
                // UNC path: \\server\share
                root = '\\\\';
            } else if (p.startsWith('\\') || p.startsWith('/')) {
                // Unix: /
                root = p.substring(0, 1);
            }
        }

        return { root, dir, base, ext, name };
    },

    sep: typeof process !== 'undefined' && process.platform === 'win32' ? '\\' : '/'
};

// ============================================
// CommonJS require() implementation
// ============================================

/**
 * Module cache
 */
const Module = {
    _cache: {},
    _extensions: {}
};

/**
 * require() 实现（完整的 Node.js 模块解析）
 */
function createRequire(baseDir) {
    const require = function(modulePath) {
        // 0. 检查内置模块（fs, path等），直接从缓存返回
        if (Module._cache[modulePath] && Module._cache[modulePath].loaded) {
            return Module._cache[modulePath].exports;
        }

        // 解析模块路径
        const resolved = require.resolve(modulePath);

        // 检查缓存
        if (Module._cache[resolved]) {
            return Module._cache[resolved].exports;
        }

        // 创建新模块对象
        const module = {
            exports: {},
            id: resolved,
            filename: resolved,
            loaded: false,
            parent: null,
            children: [],
            paths: []
        };

        // 添加到缓存
        Module._cache[resolved] = module;

        // 加载模块
        try {
            const ext = path.extname(resolved) || '.js';
            const loader = Module._extensions[ext] || Module._extensions['.js'];
            loader(module, resolved);
            module.loaded = true;
        } catch (e) {
            // 加载失败，从缓存中移除
            delete Module._cache[resolved];
            throw e;
        }

        return module.exports;
    };

    /**
     * 模块路径解析（完整的 Node.js 解析算法）
     */
    require.resolve = function(modulePath) {
        // 1. 核心模块（Node.js 内置模块不支持，直接跳过）

        // 2. 相对路径或绝对路径
        if (modulePath.startsWith('./') || modulePath.startsWith('../') || path.isAbsolute(modulePath)) {
            return resolveAsFile(path.resolve(baseDir, modulePath)) ||
                   resolveAsDirectory(path.resolve(baseDir, modulePath)) ||
                   throwNotFound(modulePath);
        }

        // 3. 从 node_modules 查找
        return resolveNodeModules(modulePath, baseDir) || throwNotFound(modulePath);
    };

    /**
     * 作为文件解析
     */
    function resolveAsFile(p) {
        // 尝试精确路径
        if (fs.existsSync(p) && fs.statSync(p).isFile()) {
            return p;
        }

        // 尝试添加扩展名
        const extensions = ['.js', '.json', '.node'];
        for (const ext of extensions) {
            const withExt = p + ext;
            if (fs.existsSync(withExt) && fs.statSync(withExt).isFile()) {
                return withExt;
            }
        }

        return null;
    }

    /**
     * 作为目录解析
     */
    function resolveAsDirectory(p) {
        if (!fs.existsSync(p) || !fs.statSync(p).isDirectory()) {
            return null;
        }

        // 1. 检查 package.json
        const packageJson = path.join(p, 'package.json');
        if (fs.existsSync(packageJson)) {
            try {
                const pkg = JSON.parse(fs.readFileSync(packageJson));
                if (pkg.main) {
                    const mainPath = path.resolve(p, pkg.main);
                    const resolved = resolveAsFile(mainPath) || resolveAsFile(path.join(mainPath, 'index'));
                    if (resolved) return resolved;
                }
            } catch (e) {
                // package.json 解析失败，继续
            }
        }

        // 2. 查找 index.js, index.json, index.node
        return resolveAsFile(path.join(p, 'index'));
    }

    /**
     * 从 node_modules 查找
     */
    function resolveNodeModules(moduleName, startDir) {
        const dirs = getNodeModulesPaths(startDir);
        for (const dir of dirs) {
            const modulePath = path.join(dir, moduleName);
            const resolved = resolveAsFile(modulePath) || resolveAsDirectory(modulePath);
            if (resolved) return resolved;
        }
        return null;
    }

    /**
     * 获取所有可能的 node_modules 路径（递归向上）
     */
    function getNodeModulesPaths(startDir) {
        const paths = [];
        let currentDir = path.resolve(startDir);
        const root = path.parse(currentDir).root;
        const visited = new Set(); // 防止无限循环
        let maxIterations = 100; // 最大迭代次数保护

        while (maxIterations-- > 0) {
            // 检查是否访问过此目录（防止循环）
            if (visited.has(currentDir)) {
                break;
            }
            visited.add(currentDir);

            // 不在 node_modules 目录内
            if (!currentDir.endsWith(path.sep + 'node_modules')) {
                paths.push(path.join(currentDir, 'node_modules'));
            }

            // 到达根目录
            if (currentDir === root) break;

            // 向上一级
            const parentDir = path.dirname(currentDir);

            // 如果 dirname 返回相同路径，说明已经到根目录
            if (parentDir === currentDir) break;

            // 空字符串也是到达根目录的标志
            if (!parentDir || parentDir === '.') break;

            currentDir = parentDir;
        }

        // 添加 NODE_PATH 环境变量指定的路径
        if (typeof process !== 'undefined' && process.env && process.env.NODE_PATH) {
            const nodePath = process.env.NODE_PATH;
            // 根据平台选择分隔符 (Windows: ';', Unix: ':')
            const delimiter = process.platform === 'win32' ? ';' : ':';
            const nodePathDirs = nodePath.split(delimiter)
                .map(p => p.trim())
                .filter(p => p.length > 0);

            // 将 NODE_PATH 目录添加到搜索路径（在递归路径之后）
            for (const dir of nodePathDirs) {
                // NODE_PATH 可以直接指向包含模块的目录
                paths.push(dir);
            }
        }

        return paths;
    }

    /**
     * 抛出模块未找到错误
     */
    function throwNotFound(modulePath) {
        throw new Error("Cannot find module '" + modulePath + "'");
    }

    /**
     * require.cache - 模块缓存
     */
    require.cache = Module._cache;

    /**
     * require.main - 主模块
     */
    require.main = null;

    return require;
}

/**
 * JavaScript 模块加载器
 */
Module._extensions['.js'] = function(module, filename) {
    const content = fs.readFileSync(filename, 'utf8');

    // 创建模块作用域
    const dirname = path.dirname(filename);
    const require = createRequire(dirname);

    // 包装模块代码
    const wrapper = '(function(exports, require, module, __filename, __dirname) {\n' +
                   content + '\n' +
                   '})';

    // 编译并执行
    const compiledWrapper = eval(wrapper);
    compiledWrapper(module.exports, require, module, filename, dirname);
};

/**
 * JSON 模块加载器
 */
Module._extensions['.json'] = function(module, filename) {
    const content = fs.readFileSync(filename, 'utf8');
    module.exports = JSON.parse(content);
};

/**
 * 全局 require（从当前工作目录）
 */
if (typeof require === 'undefined') {
    const cwd = Deno.core.ops.op_getcwd();
    globalThis.require = createRequire(cwd);
    globalThis.module = { exports: {} };
    globalThis.exports = globalThis.module.exports;
    globalThis.__dirname = cwd;
    globalThis.__filename = '';
}

// 导出 fs 和 path 模块，可以通过 require 使用
Module._cache['fs'] = { exports: fs, loaded: true };
Module._cache['path'] = { exports: path, loaded: true };

// ============================================
// crypto 模块 - Node.js compatible
// ============================================

const nodeCrypto = {
    /**
     * Create a hash object
     * @param {string} algorithm - Hash algorithm (md5, sha1, sha256, sha512, sm3, etc.)
     * @returns {Hash} Hash object
     */
    createHash: function(algorithm) {
        return new Hash(algorithm);
    },

    /**
     * Create an HMAC object
     * @param {string} algorithm - Hash algorithm
     * @param {string|Buffer} key - HMAC key
     * @returns {Hmac} HMAC object
     */
    createHmac: function(algorithm, key) {
        return new Hmac(algorithm, key);
    },

    /**
     * Generate random bytes
     * @param {number} size - Number of bytes
     * @param {Function} callback - Optional callback (if provided, async)
     * @returns {Buffer|undefined} Buffer of random bytes (sync) or undefined (async)
     */
    randomBytes: function(size, callback) {
        // op_crypto_get_random_values returns hex string
        const hexString = Deno.core.ops.op_crypto_get_random_values(size);

        // Convert hex string to Buffer
        const buf = Buffer.from(hexString, 'hex');

        if (callback) {
            // Async mode
            setTimeout(() => callback(null, buf), 0);
            return undefined;
        }

        // Sync mode
        return buf;
    },

    /**
     * Generate random UUID
     * @returns {string} UUID string
     */
    randomUUID: function() {
        return Deno.core.ops.op_crypto_random_uuid();
    },

    /**
     * Crypto constants (partial implementation)
     */
    constants: {
        // OpenSSL constants placeholder
    }
};

/**
 * Hash class - Node.js compatible
 */
class Hash {
    constructor(algorithm) {
        this._algorithm = algorithm.toLowerCase().replace(/-/g, '');
        this._data = '';
    }

    /**
     * Update hash with data
     * @param {string|Buffer} data - Data to hash
     * @param {string} inputEncoding - Input encoding (optional)
     * @returns {Hash} this for chaining
     */
    update(data, inputEncoding) {
        if (Buffer.isBuffer(data)) {
            this._data += data.toString('utf8');
        } else if (typeof data === 'string') {
            this._data += data;
        } else {
            this._data += String(data);
        }
        return this;
    }

    /**
     * Calculate final digest
     * @param {string} encoding - Output encoding ('hex', 'base64', 'buffer', etc.)
     * @returns {string|Buffer} Hash digest
     */
    digest(encoding) {
        let result;

        // Map algorithms to available ops
        switch (this._algorithm) {
            case 'md5':
                result = Deno.core.ops.op_md5(this._data);
                break;
            case 'sha1':
                result = Deno.core.ops.op_sha1(this._data);
                break;
            case 'sha256':
                result = Deno.core.ops.op_sha256(this._data);
                break;
            case 'sha512':
                result = Deno.core.ops.op_sha512(this._data);
                break;
            case 'sm3':
                // SM3 not directly supported, fallback to SHA256
                console.warn('[crypto] SM3 not supported, using SHA256 as fallback');
                result = Deno.core.ops.op_sha256(this._data);
                break;
            default:
                throw new Error(`Digest method ${this._algorithm} not supported`);
        }

        // result is hex string by default
        if (!encoding || encoding === 'hex') {
            return result;
        } else if (encoding === 'base64') {
            // Convert hex to buffer then to base64
            const buf = Buffer.from(result, 'hex');
            return buf.toString('base64');
        } else if (encoding === 'buffer' || encoding === 'binary') {
            // Return Buffer
            return Buffer.from(result, 'hex');
        } else if (encoding === 'latin1') {
            const buf = Buffer.from(result, 'hex');
            return buf.toString('latin1');
        } else {
            // Default to hex
            return result;
        }
    }

    /**
     * Copy the hash object
     * @returns {Hash} New hash object with same state
     */
    copy() {
        const newHash = new Hash(this._algorithm);
        newHash._data = this._data;
        return newHash;
    }
}

/**
 * Hmac class - Node.js compatible
 */
class Hmac {
    constructor(algorithm, key) {
        this._algorithm = algorithm.toLowerCase().replace(/-/g, '');
        this._key = Buffer.isBuffer(key) ? key.toString('utf8') : String(key);
        this._data = '';
    }

    /**
     * Update HMAC with data
     * @param {string|Buffer} data - Data to hash
     * @param {string} inputEncoding - Input encoding (optional)
     * @returns {Hmac} this for chaining
     */
    update(data, inputEncoding) {
        if (Buffer.isBuffer(data)) {
            this._data += data.toString('utf8');
        } else if (typeof data === 'string') {
            this._data += data;
        } else {
            this._data += String(data);
        }
        return this;
    }

    /**
     * Calculate final HMAC digest
     * @param {string} encoding - Output encoding ('hex', 'base64', 'buffer', etc.)
     * @returns {string|Buffer} HMAC digest
     */
    digest(encoding) {
        let result;

        // Map algorithms to available ops
        switch (this._algorithm) {
            case 'md5':
                result = Deno.core.ops.op_hmac_md5(this._key, this._data);
                break;
            case 'sha1':
                result = Deno.core.ops.op_hmac_sha1(this._key, this._data);
                break;
            case 'sha256':
                result = Deno.core.ops.op_hmac_sha256(this._key, this._data);
                break;
            case 'sha512':
                // HMAC-SHA512 not directly supported, use SHA256 as fallback
                console.warn('[crypto] HMAC-SHA512 not supported, using HMAC-SHA256 as fallback');
                result = Deno.core.ops.op_hmac_sha256(this._key, this._data);
                break;
            case 'sm3':
                // SM3 not supported, fallback to SHA256
                console.warn('[crypto] HMAC-SM3 not supported, using HMAC-SHA256 as fallback');
                result = Deno.core.ops.op_hmac_sha256(this._key, this._data);
                break;
            default:
                throw new Error(`HMAC digest method ${this._algorithm} not supported`);
        }

        // result is hex string by default
        if (!encoding || encoding === 'hex') {
            return result;
        } else if (encoding === 'base64') {
            const buf = Buffer.from(result, 'hex');
            return buf.toString('base64');
        } else if (encoding === 'buffer' || encoding === 'binary') {
            return Buffer.from(result, 'hex');
        } else if (encoding === 'latin1') {
            const buf = Buffer.from(result, 'hex');
            return buf.toString('latin1');
        } else {
            return result;
        }
    }
}

// Export crypto as a built-in module (use nodeCrypto to avoid conflict with global crypto)
Module._cache['crypto'] = { exports: nodeCrypto, loaded: true };

log('crypto module loaded (Node.js compatible)');


// ============================================
// localStorage / sessionStorage
// ============================================

if (typeof localStorage === 'undefined') {
    class Storage {
        setItem(key, value) {
            const result = Deno.core.ops.op_local_storage_set_item(String(key), String(value));
            if (result !== 'OK') throw new Error(result);
        }
        getItem(key) {
            const result = Deno.core.ops.op_local_storage_get_item(String(key));
            return result === 'null' ? null : result;
        }
        removeItem(key) {
            Deno.core.ops.op_local_storage_remove_item(String(key));
        }
        clear() {
            Deno.core.ops.op_local_storage_clear();
        }
        key(index) {
            const keys = JSON.parse(Deno.core.ops.op_local_storage_keys());
            return keys[index] || null;
        }
        get length() {
            return Deno.core.ops.op_local_storage_length();
        }
    }

    class SessionStorage {
        setItem(key, value) {
            const result = Deno.core.ops.op_session_storage_set_item(String(key), String(value));
            if (result !== 'OK') throw new Error(result);
        }
        getItem(key) {
            const result = Deno.core.ops.op_session_storage_get_item(String(key));
            return result === 'null' ? null : result;
        }
        removeItem(key) {
            Deno.core.ops.op_session_storage_remove_item(String(key));
        }
        clear() {
            Deno.core.ops.op_session_storage_clear();
        }
        key(index) {
            const keys = JSON.parse(Deno.core.ops.op_session_storage_keys());
            return keys[index] || null;
        }
        get length() {
            return Deno.core.ops.op_session_storage_length();
        }
    }

    globalThis.localStorage = new Storage();
    globalThis.sessionStorage = new SessionStorage();
}

// ============================================
// Browser Environment (navigator, location, document, window, screen)
// ============================================

// Helper function: 创建可配置、可写的对象（支持 hook 和重写）
function createConfigurableObject(data) {
    const obj = {};
    for (const key in data) {
        if (data.hasOwnProperty(key)) {
            const value = data[key];
            // 使用 defineProperty 设置为可写、可配置
            Object.defineProperty(obj, key, {
                value: value,
                writable: true,        // 可写：支持 navigator.userAgent = '...'
                configurable: true,    // 可配置：支持 Object.defineProperty(navigator, 'userAgent', {...})
                enumerable: true       // 可枚举：for...in 可遍历
            });
        }
    }
    return obj;
}

if (typeof navigator === 'undefined') {
    const navigatorData = JSON.parse(Deno.core.ops.op_get_navigator());
    // 创建可配置对象，支持修改和 hook
    globalThis.navigator = createConfigurableObject(navigatorData);
}

if (typeof location === 'undefined') {
    const locationData = JSON.parse(Deno.core.ops.op_get_location('https://example.com/'));
    globalThis.location = createConfigurableObject(locationData);
}

if (typeof document === 'undefined') {
    const docProps = JSON.parse(Deno.core.ops.op_get_document_props());
    // document 需要包含方法
    const document = createConfigurableObject(docProps);

    // 添加 DOM 方法（也设置为可配置，支持 hook）
    Object.defineProperty(document, 'getElementById', {
        value: () => null,
        writable: true,
        configurable: true,
        enumerable: false
    });
    Object.defineProperty(document, 'getElementsByClassName', {
        value: () => [],
        writable: true,
        configurable: true,
        enumerable: false
    });
    Object.defineProperty(document, 'getElementsByTagName', {
        value: () => [],
        writable: true,
        configurable: true,
        enumerable: false
    });
    Object.defineProperty(document, 'querySelector', {
        value: () => null,
        writable: true,
        configurable: true,
        enumerable: false
    });
    Object.defineProperty(document, 'querySelectorAll', {
        value: () => [],
        writable: true,
        configurable: true,
        enumerable: false
    });
    Object.defineProperty(document, 'createElement', {
        value: (tag) => ({ tagName: tag.toUpperCase(), nodeName: tag.toUpperCase() }),
        writable: true,
        configurable: true,
        enumerable: false
    });
    Object.defineProperty(document, 'addEventListener', {
        value: () => {},
        writable: true,
        configurable: true,
        enumerable: false
    });
    Object.defineProperty(document, 'removeEventListener', {
        value: () => {},
        writable: true,
        configurable: true,
        enumerable: false
    });

    globalThis.document = document;
}

if (typeof window === 'undefined') {
    globalThis.window = globalThis;
}

// window 属性也设置为可配置
const windowProps = JSON.parse(Deno.core.ops.op_get_window_props());
for (const key in windowProps) {
    if (windowProps.hasOwnProperty(key)) {
        Object.defineProperty(globalThis, key, {
            value: windowProps[key],
            writable: true,
            configurable: true,
            enumerable: true
        });
    }
}

if (typeof screen === 'undefined') {
    const screenData = JSON.parse(Deno.core.ops.op_get_screen());
    globalThis.screen = createConfigurableObject(screenData);
}

// Enhanced Console (if needed)
if (typeof console.table === 'undefined') {
    console.table = function(data) {
        console.log(JSON.stringify(data, null, 2));
    };
}

// Mark browser environment loaded
globalThis.__NEVER_JSCORE_BROWSER_ENV_LOADED__ = true;


// ============================================
// Blob API
// ============================================

if (typeof Blob === 'undefined') {
    class Blob {
        constructor(blobParts = [], options = {}) {
            this.type = options.type || '';
            this.endings = options.endings || 'transparent';

            // 将所有部分转换为字符串并拼接
            this._parts = [];
            this._size = 0;

            if (Array.isArray(blobParts)) {
                for (const part of blobParts) {
                    if (part instanceof Blob) {
                        // 如果是 Blob，复制其内容
                        this._parts.push(...part._parts);
                        this._size += part.size;
                    } else if (part instanceof ArrayBuffer || ArrayBuffer.isView(part)) {
                        // ArrayBuffer 或 TypedArray
                        const bytes = new Uint8Array(
                            part instanceof ArrayBuffer ? part : part.buffer
                        );
                        this._parts.push(bytes);
                        this._size += bytes.byteLength;
                    } else if (typeof part === 'string') {
                        // 字符串：转换为 UTF-8 字节
                        const encoder = new TextEncoder();
                        const bytes = encoder.encode(part);
                        this._parts.push(bytes);
                        this._size += bytes.byteLength;
                    } else {
                        // 其他类型：转为字符串
                        const str = String(part);
                        const encoder = new TextEncoder();
                        const bytes = encoder.encode(str);
                        this._parts.push(bytes);
                        this._size += bytes.byteLength;
                    }
                }
            }
        }

        get size() {
            return this._size;
        }

        /**
         * 返回 Blob 的一部分作为新 Blob
         * @param {number} start - 起始字节位置
         * @param {number} end - 结束字节位置（不包含）
         * @param {string} contentType - 新 Blob 的 MIME 类型
         * @returns {Blob} 新的 Blob 对象
         */
        slice(start = 0, end = this._size, contentType = '') {
            // 标准化参数
            const normalizedStart = start < 0 ? Math.max(this._size + start, 0) : Math.min(start, this._size);
            const normalizedEnd = end < 0 ? Math.max(this._size + end, 0) : Math.min(end, this._size);
            const span = Math.max(normalizedEnd - normalizedStart, 0);

            // 获取所有字节
            const allBytes = this._getAllBytes();

            // 切片
            const slicedBytes = allBytes.slice(normalizedStart, normalizedStart + span);

            // 创建新 Blob
            const newBlob = new Blob([slicedBytes], { type: contentType || this.type });
            return newBlob;
        }

        /**
         * 读取 Blob 内容为文本
         * @returns {Promise<string>} Blob 内容
         */
        async text() {
            const bytes = this._getAllBytes();
            const decoder = new TextDecoder();
            return decoder.decode(bytes);
        }

        /**
         * 读取 Blob 内容为 ArrayBuffer
         * @returns {Promise<ArrayBuffer>} Blob 内容
         */
        async arrayBuffer() {
            const bytes = this._getAllBytes();
            return bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
        }

        /**
         * 创建可读流（简化实现）
         * @returns {ReadableStream} 可读流
         */
        stream() {
            // 简化实现：返回一个 mock 对象
            const bytes = this._getAllBytes();
            return {
                getReader: () => ({
                    read: async () => ({
                        value: bytes,
                        done: false
                    }),
                    releaseLock: () => {}
                })
            };
        }

        /**
         * 获取所有字节（内部方法）
         * @private
         * @returns {Uint8Array} 所有字节
         */
        _getAllBytes() {
            if (this._parts.length === 0) {
                return new Uint8Array(0);
            }

            // 合并所有部分
            const result = new Uint8Array(this._size);
            let offset = 0;
            for (const part of this._parts) {
                result.set(part, offset);
                offset += part.byteLength;
            }
            return result;
        }

        /**
         * 返回 Blob 类型字符串
         * @returns {string} "[object Blob]"
         */
        toString() {
            return '[object Blob]';
        }
    }

    globalThis.Blob = Blob;
    log('Blob API loaded');
}

// Object URL 存储（用于 createObjectURL / revokeObjectURL）
const _objectURLStore = new Map();
let _objectURLCounter = 0;

// ============================================
// URL and URLSearchParams API
// ============================================

if (typeof URL === 'undefined') {
    class URL {
        constructor(url, base) {
            const urlString = base ? this._resolveUrl(url, base) : url;
            const parsed = JSON.parse(Deno.core.ops.op_parse_url(urlString));

            this.href = parsed.href;
            this.protocol = parsed.protocol;
            this.hostname = parsed.hostname;
            this.port = parsed.port;
            this.host = parsed.host;
            this.pathname = parsed.pathname;
            this.search = parsed.search;
            this.hash = parsed.hash;
            this.origin = parsed.origin;

            this._searchParams = null;
        }

        _resolveUrl(url, base) {
            if (url.startsWith('http://') || url.startsWith('https://')) {
                return url;
            }
            // 简单的相对 URL 解析
            if (url.startsWith('/')) {
                const baseParsed = JSON.parse(Deno.core.ops.op_parse_url(base));
                return `${baseParsed.origin}${url}`;
            }
            return `${base}/${url}`;
        }

        get searchParams() {
            if (!this._searchParams) {
                this._searchParams = new URLSearchParams(this.search);
            }
            return this._searchParams;
        }

        toString() {
            return this.href;
        }

        toJSON() {
            return this.href;
        }

        /**
         * 创建 Blob 或 MediaSource 的对象 URL
         * @param {Blob|MediaSource} obj - Blob 或 MediaSource 对象
         * @returns {string} 对象 URL
         */
        static createObjectURL(obj) {
            if (!obj) {
                throw new TypeError("Failed to execute 'createObjectURL' on 'URL': 1 argument required, but only 0 present.");
            }

            // 检查是否为 Blob 或类 Blob 对象
            if (!(obj instanceof Blob) && !(obj._parts !== undefined)) {
                throw new TypeError("Failed to execute 'createObjectURL' on 'URL': Overload resolution failed.");
            }

            // 生成唯一 URL
            const id = `blob-${_objectURLCounter++}-${Math.random().toString(36).substr(2, 9)}`;
            const blobUrl = `blob:neverjscore/${id}`;

            // 存储 Blob 对象
            _objectURLStore.set(blobUrl, obj);

            log(`URL.createObjectURL: ${blobUrl}, size=${obj.size || 0}, type=${obj.type || ''}`);

            return blobUrl;
        }

        /**
         * 撤销对象 URL
         * @param {string} url - 要撤销的对象 URL
         */
        static revokeObjectURL(url) {
            if (typeof url !== 'string') {
                return;
            }

            if (_objectURLStore.has(url)) {
                _objectURLStore.delete(url);
                log(`URL.revokeObjectURL: ${url}`);
            }
        }

        /**
         * 获取对象 URL 对应的 Blob（内部方法，供其他 API 使用）
         * @private
         * @param {string} url - 对象 URL
         * @returns {Blob|null} Blob 对象或 null
         */
        static _getObjectURL(url) {
            return _objectURLStore.get(url) || null;
        }
    }

    globalThis.URL = URL;
}

if (typeof URLSearchParams === 'undefined') {
    class URLSearchParams {
        constructor(init) {
            this._params = new Map();

            if (typeof init === 'string') {
                // 移除开头的 ?
                const str = init.startsWith('?') ? init.substring(1) : init;
                if (str) {
                    str.split('&').forEach(pair => {
                        const [key, value = ''] = pair.split('=');
                        this.append(
                            decodeURIComponent(key),
                            decodeURIComponent(value)
                        );
                    });
                }
            } else if (init instanceof URLSearchParams) {
                init.forEach((value, key) => {
                    this.append(key, value);
                });
            } else if (typeof init === 'object' && init !== null) {
                Object.keys(init).forEach(key => {
                    this.append(key, init[key]);
                });
            }
        }

        append(name, value) {
            const key = String(name);
            const val = String(value);
            if (!this._params.has(key)) {
                this._params.set(key, []);
            }
            this._params.get(key).push(val);
        }

        delete(name) {
            this._params.delete(String(name));
        }

        get(name) {
            const values = this._params.get(String(name));
            return values ? values[0] : null;
        }

        getAll(name) {
            return this._params.get(String(name)) || [];
        }

        has(name) {
            return this._params.has(String(name));
        }

        set(name, value) {
            this._params.set(String(name), [String(value)]);
        }

        forEach(callback, thisArg) {
            this._params.forEach((values, key) => {
                values.forEach(value => {
                    callback.call(thisArg, value, key, this);
                });
            });
        }

        keys() {
            return this._params.keys();
        }

        values() {
            const result = [];
            this._params.forEach(values => {
                result.push(...values);
            });
            return result[Symbol.iterator]();
        }

        entries() {
            const result = [];
            this._params.forEach((values, key) => {
                values.forEach(value => {
                    result.push([key, value]);
                });
            });
            return result[Symbol.iterator]();
        }

        toString() {
            const parts = [];
            this._params.forEach((values, key) => {
                values.forEach(value => {
                    parts.push(
                        encodeURIComponent(key) + '=' + encodeURIComponent(value)
                    );
                });
            });
            return parts.join('&');
        }

        [Symbol.iterator]() {
            return this.entries();
        }
    }

    globalThis.URLSearchParams = URLSearchParams;
}

// ============================================
// FormData API
// ============================================

if (typeof FormData === 'undefined') {
    class FormData {
        constructor() {
            this._data = new Map();
        }

        append(name, value, filename) {
            const key = String(name);
            if (!this._data.has(key)) {
                this._data.set(key, []);
            }

            const entry = { value: String(value) };
            if (filename !== undefined) {
                entry.filename = String(filename);
            }

            this._data.get(key).push(entry);
        }

        delete(name) {
            this._data.delete(String(name));
        }

        get(name) {
            const entries = this._data.get(String(name));
            return entries ? entries[0].value : null;
        }

        getAll(name) {
            const entries = this._data.get(String(name));
            return entries ? entries.map(e => e.value) : [];
        }

        has(name) {
            return this._data.has(String(name));
        }

        set(name, value, filename) {
            const key = String(name);
            const entry = { value: String(value) };
            if (filename !== undefined) {
                entry.filename = String(filename);
            }
            this._data.set(key, [entry]);
        }

        forEach(callback, thisArg) {
            this._data.forEach((entries, key) => {
                entries.forEach(entry => {
                    callback.call(thisArg, entry.value, key, this);
                });
            });
        }

        keys() {
            return this._data.keys();
        }

        values() {
            const result = [];
            this._data.forEach(entries => {
                entries.forEach(entry => result.push(entry.value));
            });
            return result[Symbol.iterator]();
        }

        entries() {
            const result = [];
            this._data.forEach((entries, key) => {
                entries.forEach(entry => result.push([key, entry.value]));
            });
            return result[Symbol.iterator]();
        }

        [Symbol.iterator]() {
            return this.entries();
        }
    }

    globalThis.FormData = FormData;
}

// ============================================
// Event and EventTarget API
// ============================================

if (typeof Event === 'undefined') {
    class Event {
        constructor(type, eventInitDict = {}) {
            this.type = String(type);
            this.bubbles = eventInitDict.bubbles || false;
            this.cancelable = eventInitDict.cancelable || false;
            this.composed = eventInitDict.composed || false;

            this.target = null;
            this.currentTarget = null;
            this.eventPhase = 0; // Event.NONE
            this.defaultPrevented = false;
            this.timeStamp = Date.now();

            this._stopPropagation = false;
            this._stopImmediatePropagation = false;
        }

        preventDefault() {
            if (this.cancelable) {
                this.defaultPrevented = true;
            }
        }

        stopPropagation() {
            this._stopPropagation = true;
        }

        stopImmediatePropagation() {
            this._stopImmediatePropagation = true;
            this._stopPropagation = true;
        }

        // Event phase constants
        static get NONE() { return 0; }
        static get CAPTURING_PHASE() { return 1; }
        static get AT_TARGET() { return 2; }
        static get BUBBLING_PHASE() { return 3; }
    }

    globalThis.Event = Event;
}

if (typeof EventTarget === 'undefined') {
    class EventTarget {
        constructor() {
            this._listeners = new Map();
        }

        addEventListener(type, callback, options = {}) {
            if (typeof callback !== 'function') return;

            const typeStr = String(type);
            if (!this._listeners.has(typeStr)) {
                this._listeners.set(typeStr, []);
            }

            const listeners = this._listeners.get(typeStr);

            // 检查是否已经存在
            const exists = listeners.some(l =>
                l.callback === callback &&
                l.capture === (options.capture || false)
            );

            if (!exists) {
                listeners.push({
                    callback,
                    capture: options.capture || false,
                    once: options.once || false,
                    passive: options.passive || false
                });
            }
        }

        removeEventListener(type, callback, options = {}) {
            const typeStr = String(type);
            const listeners = this._listeners.get(typeStr);
            if (!listeners) return;

            const capture = options.capture || false;
            const index = listeners.findIndex(l =>
                l.callback === callback && l.capture === capture
            );

            if (index !== -1) {
                listeners.splice(index, 1);
            }

            if (listeners.length === 0) {
                this._listeners.delete(typeStr);
            }
        }

        dispatchEvent(event) {
            const typeStr = String(event.type);
            const listeners = this._listeners.get(typeStr);
            if (!listeners || listeners.length === 0) {
                return !event.defaultPrevented;
            }

            event.target = this;
            event.currentTarget = this;
            event.eventPhase = Event.AT_TARGET;

            // 复制监听器数组以防在回调中被修改
            const listenersCopy = [...listeners];

            for (const listener of listenersCopy) {
                if (event._stopImmediatePropagation) break;

                try {
                    listener.callback.call(this, event);
                } catch (err) {
                    console.error('Error in event listener:', err);
                }

                // 如果是 once，移除监听器
                if (listener.once) {
                    this.removeEventListener(
                        typeStr,
                        listener.callback,
                        { capture: listener.capture }
                    );
                }
            }

            return !event.defaultPrevented;
        }
    }

    globalThis.EventTarget = EventTarget;
}

// ============================================
// XMLHttpRequest API (基于 fetch 实现)
// ============================================

if (typeof XMLHttpRequest === 'undefined') {
    class XMLHttpRequest extends EventTarget {
        constructor() {
            super();

            // Ready states
            this.UNSENT = 0;
            this.OPENED = 1;
            this.HEADERS_RECEIVED = 2;
            this.LOADING = 3;
            this.DONE = 4;

            // State
            this.readyState = this.UNSENT;
            this.response = null;
            this.responseText = '';
            this.responseType = '';
            this.responseURL = '';
            this.responseXML = null;
            this.status = 0;
            this.statusText = '';
            this.timeout = 0;
            this.withCredentials = false;

            // Event handlers
            this.onreadystatechange = null;
            this.onload = null;
            this.onerror = null;
            this.onprogress = null;
            this.onloadstart = null;
            this.onloadend = null;
            this.ontimeout = null;
            this.onabort = null;

            // Internal
            this._method = null;
            this._url = null;
            this._headers = {};
            this._async = true;
            this._aborted = false;
        }

        open(method, url, async = true, username, password) {
            this._method = String(method).toUpperCase();
            this._url = String(url);
            this._async = Boolean(async);

            this.readyState = this.OPENED;
            this._triggerEvent('readystatechange');
        }

        setRequestHeader(name, value) {
            if (this.readyState !== this.OPENED) {
                throw new Error('InvalidStateError');
            }
            this._headers[String(name)] = String(value);
        }

        send(body = null) {
            if (this.readyState !== this.OPENED) {
                throw new Error('InvalidStateError');
            }

            if (this._aborted) return;

            this._triggerEvent('loadstart');

            // 使用 fetch 发送请求
            const options = {
                method: this._method,
                headers: this._headers
            };

            if (body !== null && this._method !== 'GET' && this._method !== 'HEAD') {
                options.body = body;
            }

            if (this.timeout > 0) {
                options.timeout = this.timeout;
            }

            const sendRequest = async () => {
                try {
                    const response = await fetch(this._url, options);

                    this.status = response.status;
                    this.statusText = response.statusText || '';
                    this.responseURL = this._url;

                    // 提取响应头
                    if (response.headers && response.headers.forEach) {
                        response.headers.forEach((value, name) => {
                            this._headers[name.toLowerCase()] = value;
                        });
                    }

                    this.readyState = this.HEADERS_RECEIVED;
                    this._triggerEvent('readystatechange');

                    this.readyState = this.LOADING;
                    this._triggerEvent('readystatechange');

                    // 读取响应体（根据 responseType）
                    if (this.responseType === 'arraybuffer') {
                        const arrayBuffer = await response.arrayBuffer();
                        this.response = arrayBuffer;
                        this.responseText = '';  // arraybuffer 类型时 responseText 为空
                    } else if (this.responseType === 'json') {
                        const json = await response.json();
                        this.response = json;
                        this.responseText = JSON.stringify(json);
                    } else if (this.responseType === 'blob') {
                        const blob = await response.blob();
                        this.response = blob;
                        this.responseText = '';
                    } else {
                        // 默认为 'text' 或 ''
                        const text = await response.text();
                        this.responseText = text;
                        this.response = text;
                    }

                    this.readyState = this.DONE;
                    this._triggerEvent('readystatechange');
                    this._triggerEvent('load');
                    this._triggerEvent('loadend');

                } catch (error) {
                    this.readyState = this.DONE;
                    this._triggerEvent('error', error);
                    this._triggerEvent('loadend');
                }
            };

            if (this._async) {
                sendRequest();
            } else {
                // 同步请求（不推荐，但为了兼容性）
                console.warn('Synchronous XMLHttpRequest is deprecated');
                sendRequest();
            }
        }

        abort() {
            if (this.readyState === this.UNSENT || this.readyState === this.DONE) {
                return;
            }

            this._aborted = true;
            this.readyState = this.DONE;
            this._triggerEvent('abort');
            this._triggerEvent('loadend');
        }

        getAllResponseHeaders() {
            if (this.readyState < this.HEADERS_RECEIVED) {
                return '';
            }
            // 简化实现
            return Object.keys(this._headers)
                .map(key => `${key}: ${this._headers[key]}`)
                .join('\r\n');
        }

        getResponseHeader(name) {
            if (this.readyState < this.HEADERS_RECEIVED) {
                return null;
            }
            return this._headers[String(name).toLowerCase()] || null;
        }

        overrideMimeType(mime) {
            // 简化实现
        }

        _triggerEvent(eventName, detail) {
            // 触发 on* 处理器
            const handler = this['on' + eventName];
            if (typeof handler === 'function') {
                const event = new Event(eventName);
                if (detail) event.detail = detail;
                handler.call(this, event);
            }

            // 触发 EventTarget 监听器
            const event = new Event(eventName);
            if (detail) event.detail = detail;
            this.dispatchEvent(event);
        }
    }

    // 添加静态常量
    XMLHttpRequest.UNSENT = 0;
    XMLHttpRequest.OPENED = 1;
    XMLHttpRequest.HEADERS_RECEIVED = 2;
    XMLHttpRequest.LOADING = 3;
    XMLHttpRequest.DONE = 4;

    globalThis.XMLHttpRequest = XMLHttpRequest;
}

// Mark high-priority APIs loaded
globalThis.__NEVER_JSCORE_HIGH_PRIORITY_APIS_LOADED__ = true;

// ============================================
// Performance API (Web Performance Timing)
// ============================================

if (typeof performance === 'undefined') {
    class Performance {
        /**
         * Get high-precision timestamp in milliseconds
         * @returns {number} Time in milliseconds since timeOrigin
         */
        now() {
            return Deno.core.ops.op_performance_now();
        }

        /**
         * Get the time origin (Unix timestamp when runtime started)
         * @returns {number} Unix timestamp in milliseconds
         */
        get timeOrigin() {
            return Deno.core.ops.op_performance_time_origin();
        }

        /**
         * Create a performance mark
         * @param {string} name - Mark name
         * @returns {PerformanceMark} The created mark entry
         */
        mark(name) {
            Deno.core.ops.op_performance_mark(String(name));
            const timestamp = Deno.core.ops.op_performance_now();
            return {
                name: String(name),
                entryType: 'mark',
                startTime: timestamp,
                duration: 0
            };
        }

        /**
         * Create a performance measure between two marks
         * @param {string} name - Measure name
         * @param {string} startMark - Start mark name (optional)
         * @param {string} endMark - End mark name (optional)
         * @returns {PerformanceMeasure} The created measure entry
         */
        measure(name, startMark = '', endMark = '') {
            const result = Deno.core.ops.op_performance_measure(
                String(name),
                startMark ? String(startMark) : '',
                endMark ? String(endMark) : ''
            );

            if (result.startsWith('Error:')) {
                throw new Error(result);
            }

            return JSON.parse(result);
        }

        /**
         * Clear marks
         * @param {string} name - Mark name to clear (optional, clears all if not specified)
         */
        clearMarks(name = '') {
            Deno.core.ops.op_performance_clear_marks(name ? String(name) : '');
        }

        /**
         * Clear measures
         * @param {string} name - Measure name to clear (optional, clears all if not specified)
         */
        clearMeasures(name = '') {
            Deno.core.ops.op_performance_clear_measures(name ? String(name) : '');
        }

        /**
         * Get all performance entries
         * @returns {Array<PerformanceEntry>} Array of all entries
         */
        getEntries() {
            const json = Deno.core.ops.op_performance_get_entries();
            return JSON.parse(json);
        }

        /**
         * Get performance entries by type
         * @param {string} type - Entry type ("mark" or "measure")
         * @returns {Array<PerformanceEntry>} Array of matching entries
         */
        getEntriesByType(type) {
            const json = Deno.core.ops.op_performance_get_entries_by_type(String(type));
            return JSON.parse(json);
        }

        /**
         * Get performance entries by name
         * @param {string} name - Entry name
         * @param {string} type - Optional type filter ("mark" or "measure")
         * @returns {Array<PerformanceEntry>} Array of matching entries
         */
        getEntriesByName(name, type = '') {
            const json = Deno.core.ops.op_performance_get_entries_by_name(String(name));
            const entries = JSON.parse(json);

            if (type) {
                return entries.filter(e => e.entryType === String(type));
            }

            return entries;
        }

        /**
         * Convert to JSON (for debugging)
         * @returns {Object} Performance timing info
         */
        toJSON() {
            return {
                timeOrigin: this.timeOrigin,
                timing: {},
                navigation: {}
            };
        }
    }

    // Create and export global performance object
    globalThis.performance = new Performance();
}

// Mark Performance API loaded
globalThis.__NEVER_JSCORE_PERFORMANCE_API_LOADED__ = true;

