"use strict";
/**
 * openBrowser — best-effort cross-platform "open this URL in the default browser".
 *
 * Used by `openlife upgrade` to send the user to openlife.online to purchase.
 * Zero npm dependency (just node:child_process). It NEVER throws and NEVER
 * blocks: it spawns the OS launcher detached and returns whether the spawn was
 * issued. Callers MUST still print the URL as a fallback, because a `true`
 * return only means "a launcher was spawned", not "a browser actually opened".
 *
 * Security: only http(s) URLs are accepted. The URL is always passed as a
 * separate argv element (never interpolated into a shell string), so a
 * malicious URL cannot inject extra commands.
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.openBrowser = openBrowser;
const child_process_1 = require("child_process");
const fs = __importStar(require("fs"));
/** True when running under WSL — where `xdg-open` usually can't reach a browser. */
function isWSL() {
    if (process.env.WSL_DISTRO_NAME)
        return true;
    try {
        return /microsoft/i.test(fs.readFileSync('/proc/version', 'utf-8'));
    }
    catch {
        return false;
    }
}
/** Spawn a launcher detached; swallow any error and report success/failure. */
function trySpawn(command, args) {
    try {
        const child = (0, child_process_1.spawn)(command, args, { detached: true, stdio: 'ignore' });
        child.on('error', () => { });
        child.unref();
        return true;
    }
    catch {
        return false;
    }
}
/**
 * Open `url` in the default browser. Returns true if a launcher was spawned,
 * false if the URL was rejected or no launcher could be started.
 */
function openBrowser(url) {
    if (!/^https?:\/\//i.test(url))
        return false;
    // WSL first: prefer the Windows launcher, which actually reaches a browser.
    if (process.platform === 'linux' && isWSL()) {
        if (trySpawn('cmd.exe', ['/c', 'start', '', url]))
            return true;
        return trySpawn('xdg-open', [url]); // fallback if cmd.exe is unavailable
    }
    switch (process.platform) {
        case 'win32':
            // `start` needs an (empty) title argument before the URL.
            return trySpawn('cmd', ['/c', 'start', '', url]);
        case 'darwin':
            return trySpawn('open', [url]);
        default:
            return trySpawn('xdg-open', [url]);
    }
}
