import { execFile as cpExecFile } from 'node:child_process';
const defaultDeps = {
    platform: process.platform,
    execFile: cpExecFile,
    // eslint-disable-next-line no-console
    warn: (msg) => console.warn(msg),
    isTTY: !!process.stdout.isTTY,
};
const PLATFORM_COMMANDS = {
    darwin: { cmd: 'open', args: (url) => [url] },
    linux: { cmd: 'xdg-open', args: (url) => [url] },
    win32: { cmd: 'cmd', args: (url) => ['/c', 'start', '', url] },
};
export class BrowserOpenerService {
    deps;
    constructor(deps = {}) {
        this.deps = { ...defaultDeps, ...deps };
    }
    open(url) {
        // Skip browser opening in non-interactive environments (tests, CI, piped output).
        if (!this.deps.isTTY)
            return;
        const entry = PLATFORM_COMMANDS[this.deps.platform];
        if (!entry)
            return;
        const child = this.deps.execFile(entry.cmd, entry.args(url), (error) => {
            if (error) {
                this.deps.warn(`Failed to open browser: ${error.message}`);
            }
        });
        child.unref();
    }
}
