/**
 * VSCodium agent-terminal backend — opens an agent terminal in a VSCodium /
 * Cursor / VS Code window via the swarmify `swarm-ext` extension's URI handler.
 *
 * Unlike iTerm/Ghostty (GUI terminal apps driven by AppleScript), the editor is
 * already running; we hand it a `<scheme>://swarmify.swarm-ext/spawn?…` URL via
 * the editor CLI's `--open-url` flag. The extension opens an editor-tab terminal
 * in `cwd`, runs `command`, and its shell-adoption promotes a resume command
 * (e.g. `claude --resume <id>`) to the matching agent chip. Driving the editor
 * CLI (not macOS `open`) means this works over `--device` SSH and on Linux, needs
 * no OS URL-scheme handler registration, and sends the command into an
 * already-interactive login shell — so no `zsh -ilc` wrap (the other backends'
 * wrapper) is applied here.
 */
import * as fs from 'fs';
/** The swarmify extension identifier that owns the `/spawn` URI verb. */
const EXTENSION_AUTHORITY = 'swarmify.swarm-ext';
/**
 * Known editors, in preference order. VSCodium first — the backend is named for
 * it and is the user's editor; Cursor and VS Code also ship the extension and
 * are wired here for `makeVscodiumAgentBackend` / future registration.
 */
export const EDITOR_VARIANTS = [
    { cli: 'codium', scheme: 'vscodium', app: '/Applications/VSCodium.app', label: 'VSCodium' },
    { cli: 'cursor', scheme: 'cursor', app: '/Applications/Cursor.app', label: 'Cursor' },
    { cli: 'code', scheme: 'vscode', app: '/Applications/Visual Studio Code.app', label: 'VS Code' },
];
function appExists(p) {
    try {
        return fs.existsSync(p);
    }
    catch {
        return false;
    }
}
/**
 * The `<scheme>://swarmify.swarm-ext/spawn?p=<payload>` URL the extension handles.
 *
 * The payload is base64url-encoded JSON in a single `p` param — NOT one param per
 * field. VS Code percent-*decodes* `uri.query` once before the extension parses
 * it, so a `command`/`cwd` containing `&` (or `=`) would be mis-split by a naive
 * multi-param query. base64url is `[A-Za-z0-9_-]` only (no `&`, `=`, `%`, `+`,
 * `/`), so it survives that decode untouched and round-trips exactly.
 *
 * Optional `meta.agent` / `meta.sessionId` / `meta.title` let the extension set
 * the tab chip and status bar without sniffing the local process tree — required
 * when `command` is a remote attach (`ssh … tmux attach`) with no agent binary
 * on this box (#2478).
 */
export function spawnUri(scheme, cwd, command, direction, meta) {
    const payload = {
        command: command.join(' '),
        cwd,
    };
    if (direction)
        payload.split = direction;
    if (meta?.agent)
        payload.agent = meta.agent;
    if (meta?.sessionId)
        payload.sessionId = meta.sessionId;
    if (meta?.title)
        payload.title = meta.title;
    const p = Buffer.from(JSON.stringify(payload), 'utf8').toString('base64url');
    return `${scheme}://${EXTENSION_AUTHORITY}/spawn?p=${p}`;
}
/** Build a backend bound to one editor variant. */
export function makeVscodiumAgentBackend(variant) {
    return {
        id: 'vscodium-agent',
        label: `${variant.label} agent`,
        isAvailable(ctx) {
            return ctx.platform === 'darwin' && appExists(variant.app);
        },
        buildTab(cwd, command, meta) {
            return { argv: [variant.cli, '--open-url', spawnUri(variant.scheme, cwd, command, undefined, meta)] };
        },
        buildSplit(cwd, command, direction, meta) {
            return { argv: [variant.cli, '--open-url', spawnUri(variant.scheme, cwd, command, direction, meta)] };
        },
    };
}
/** Default backend: VSCodium (`codium` CLI, `vscodium://` scheme). */
export const vscodiumAgentBackend = makeVscodiumAgentBackend(EDITOR_VARIANTS[0]);
