"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.openBrowserUrl = openBrowserUrl;
exports.setOpenBrowserForTests = setOpenBrowserForTests;
exports.resetOpenBrowserForTests = resetOpenBrowserForTests;
const node_child_process_1 = require("node:child_process");
function getBrowserCommand(url) {
    if (process.platform === 'darwin') {
        return { command: 'open', args: [url] };
    }
    if (process.platform === 'win32') {
        return { command: 'cmd', args: ['/c', 'start', '', url] };
    }
    if (process.platform === 'linux') {
        return { command: 'xdg-open', args: [url] };
    }
    return null;
}
async function defaultOpenBrowser(url) {
    const command = getBrowserCommand(url);
    if (!command) {
        return false;
    }
    return await new Promise((resolve) => {
        const child = (0, node_child_process_1.spawn)(command.command, command.args, {
            detached: true,
            stdio: 'ignore',
        });
        child.once('error', () => resolve(false));
        child.once('spawn', () => {
            child.unref();
            resolve(true);
        });
    });
}
let openBrowser = defaultOpenBrowser;
async function openBrowserUrl(url) {
    return await openBrowser(url);
}
function setOpenBrowserForTests(fn) {
    openBrowser = fn;
}
function resetOpenBrowserForTests() {
    openBrowser = defaultOpenBrowser;
}
