/**
 * popout-bridge — HTTP transport for mailxapi in popout windows served by the
 * loopback popout server (bin/popout-server.ts).
 *
 * The real hosts (msger Rust, MAUI) provide `window.ipc.postMessage` and call
 * `_msgapiServiceResolve/_msgapiServiceReject` with the daemon's response.
 * In a msger url-mode window that channel exists but is wired to the
 * MESSAGEBOX protocol — service RPC posted there dies silently — and
 * window.ipc is injected non-writable/non-configurable, so it cannot be
 * replaced or wrapped. mailxapi.js therefore checks `window.__rmfIpcOverride`
 * before window.ipc; this script provides it: every posted
 * `{ _action, _cbid, ...params }` payload goes to POST /rpc and the
 * `{ _cbid, result | error }` reply resolves the matching callback. The page
 * runs the REAL mailxapi.js unmodified — one API surface, zero duplication.
 *
 * Injected by serveStatic() into every popout-served HTML page, after a
 * config script that sets `window.__rmfPopoutRpc = { url }` and before
 * mailxapi.js. No imports/exports on purpose — this must compile to a plain
 * classic script (mailxapi.js loads the same way).
 */
(function () {
    const w = window;
    const cfg = w.__rmfPopoutRpc;
    if (!cfg || !cfg.url || w.__rmfIpcOverride)
        return;
    w.__rmfIpcOverride = {
        postMessage: function (msgStr) {
            let cbid = "";
            try {
                cbid = JSON.parse(msgStr)._cbid || "";
            }
            catch { /* resolve path just no-ops */ }
            fetch(cfg.url, {
                method: "POST",
                headers: { "content-type": "application/json" },
                body: msgStr,
                // Let a final draft flush survive window close. keepalive
                // caps the body at ~64KB, so only ask for it when we fit —
                // an attachment-laden saveDraft would otherwise be REJECTED
                // outright instead of merely not surviving unload.
                keepalive: msgStr.length < 60_000,
            })
                .then((r) => r.json())
                .then((res) => {
                if (res && res.error != null)
                    w._msgapiServiceReject?.(cbid, res.error);
                else
                    w._msgapiServiceResolve?.(cbid, res ? res.result : undefined);
            })
                .catch((e) => w._msgapiServiceReject?.(cbid, String((e && e.message) || e)));
        },
    };
    // Daemon events over SSE — the main window gets these on the msger
    // service channel; popout-served pages subscribe here and feed the
    // same payloads into mailxapi's event dispatch, so secondary windows
    // live-update (new mail, folder counts, sync progress).
    // EventSource auto-reconnects if the daemon restarts.
    if (cfg.events && typeof EventSource !== "undefined") {
        const es = new EventSource(cfg.events);
        es.onmessage = (ev) => {
            try {
                w._msgapiServiceEvent?.(JSON.parse(ev.data));
            }
            catch { /* malformed frame — skip */ }
        };
    }
})();
export {};
//# sourceMappingURL=popout-bridge.js.map