import { spawn } from "node:child_process";
import { readBody, sendJson } from "./router.js";
export function registerDesktopOpenRoutes(router, opts = {}) {
    const opener = opts.opener ?? openExternalUrl;
    router.post("/api/desktop/open-url", async (req, res) => {
        let body;
        try {
            body = await readBody(req);
        }
        catch {
            sendJson(res, { ok: false, error: "Malformed JSON body" }, 400, req);
            return;
        }
        const rawUrl = body?.url;
        const url = normalizeExternalHttpsUrl(rawUrl);
        if (!url) {
            sendJson(res, { ok: false, error: "Only https URLs can be opened" }, 400, req);
            return;
        }
        try {
            await opener(url);
            sendJson(res, { ok: true }, 200, req);
        }
        catch (err) {
            const message = err instanceof Error ? err.message : String(err);
            sendJson(res, { ok: false, error: message }, 500, req);
        }
    });
}
export function normalizeExternalHttpsUrl(value) {
    if (typeof value !== "string")
        return null;
    try {
        const url = new URL(value);
        if (url.protocol !== "https:")
            return null;
        return url.toString();
    }
    catch {
        return null;
    }
}
async function openExternalUrl(url) {
    const [command, ...args] = openCommand(url);
    await new Promise((resolve, reject) => {
        const child = spawn(command, args, {
            detached: true,
            stdio: "ignore",
        });
        child.once("error", reject);
        child.once("spawn", () => {
            child.unref();
            resolve();
        });
    });
}
function openCommand(url) {
    if (process.platform === "darwin")
        return ["open", url];
    if (process.platform === "win32")
        return ["rundll32", "url.dll,FileProtocolHandler", url];
    return ["xdg-open", url];
}
//# sourceMappingURL=desktop-open-routes.js.map