// String-literal `import("node:child_process")` is statically resolved by
// esbuild and ends up as `import('child_process')` in dist/index.mjs, which
// then fails when downstream consumers bundle the SDK with platform: "browser".
// Wrapping the specifier in a template literal makes esbuild (and every other
// modern bundler) treat the call as a truly dynamic import and leave it alone.
// At runtime the catch below handles the failure when the host has neither
// window.open nor node built-ins.
const nodePrefix = "node:";
async function loadChildProcess() {
    return import(`${nodePrefix}child_process`);
}
async function loadProcess() {
    return import(`${nodePrefix}process`);
}
const openUrl = async (url) => {
    if (typeof url !== "string") {
        throw new TypeError("Expected `url` to be a string");
    }
    let parsed;
    try {
        parsed = new URL(url);
    }
    catch {
        throw new Error(`Invalid URL: ${url}`);
    }
    if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
        throw new Error(`Refusing to open non-http(s) URL: ${parsed.protocol}`);
    }
    const target = parsed.toString();
    // Browser: `noopener` prevents the opened page from touching window.opener.
    if (typeof globalThis.window !== "undefined" &&
        typeof globalThis.window.open === "function") {
        globalThis.window.open(target, "_blank", "noopener");
        return;
    }
    let spawn;
    let platform;
    try {
        const [cp, proc] = await Promise.all([loadChildProcess(), loadProcess()]);
        spawn = cp.spawn;
        platform = proc.platform;
    }
    catch {
        throw new Error("openUrl: no supported runtime. window.open is unavailable and node:child_process could not be loaded.");
    }
    if (typeof spawn !== "function") {
        throw new Error("openUrl: child_process.spawn is not available in this runtime");
    }
    let command;
    let args;
    if (platform === "darwin") {
        command = "open";
        args = [target];
    }
    else if (platform === "win32") {
        // rundll32 dispatches to the default URL handler without going through
        // cmd.exe, avoiding `&`/`^`/`|` metacharacter pitfalls.
        command = "rundll32";
        args = ["url.dll,FileProtocolHandler", target];
    }
    else {
        throw new Error(`Unsupported platform: ${platform}`);
    }
    const child = spawn(command, args, {
        stdio: "ignore",
        detached: true,
        windowsHide: true,
    });
    child.once("error", () => { });
    child.unref();
};
export default openUrl;
