/* ── the weather ────────────────────────────────────────────────────────
   Thirty rounds of walk drove the happy path and nothing else: not one step
   ever made a command reject, and not one ever saw the client signed out.
   This mock IS the whole backend behind the browser walk, so it is the only
   place the unhappy paths can be driven from.

   Four doors, all of them checked in mockInvoke BEFORE any dispatch, so a
   `plugin:` command and a catalog command arm exactly the same way:

     __ssMock.fail(cmd, message)   the next and every call to `cmd` rejects
                                   with that exact string; "" disarms it.
     __ssMock.answer(cmd, value)   `cmd` resolves with `value` — an empty
                                   list, a null page — instead of its
                                   fixture, so the walk names what "empty"
                                   means rather than the mock guessing.
     __ssMock.forget(cmd)          takes one armed answer away.
     __ssMock.offline(on)          the client goes down: everything the
                                   backend funnels through with_client()
                                   answers "not connected to Soulseek",
                                   connect_stored / connect_and_login answer
                                   the server string, and soulseek:state
                                   goes out. off restores and re-connects.
     __ssMock.clear()              all of it away, for the walk's last step.

   They reject with a PLAIN STRING, because that is what Tauri rejects with:
   an Error would arrive as "Error: …" and hide half of what say() has to
   cope with. All of it sits behind `enabled`, so a real build never sees it. */
const armedFail = new Map();
const armedAnswer = new Map();
let offlineNow = false;
/* what src-tauri/src/commands.rs runs through with_client(), plus the two
   searches and a download, which check the connection themselves */
const needsClient = (cmd) => /^(slsk_|buddy_)/.test(cmd) || cmd === "start_search" || cmd === "stop_search" || cmd === "download_file";
function weather(cmd) {
    const f = armedFail.get(cmd);
    if (f != null) return Promise.reject(f);
    if (offlineNow) {
        if (cmd === "connect_stored" || cmd === "connect_and_login") {
            return Promise.reject("could not reach server: io: failed to lookup address information: no address associated with hostname");
        }
        if (needsClient(cmd)) return Promise.reject("not connected to Soulseek");
    }
    if (armedAnswer.has(cmd)) return Promise.resolve(armedAnswer.get(cmd));
    return undefined;
}
if (enabled && typeof window !== "undefined") {
    window.__ssMock = {
        emit: mockEmit,
        fail(cmd, message) { if (message) armedFail.set(cmd, String(message)); else armedFail.delete(cmd); return true; },
        answer(cmd, value) { armedAnswer.set(cmd, value); return true; },
        forget(cmd) { armedAnswer.delete(cmd); return true; },
        offline(on) {
            offlineNow = !!on;
            mockEmit("soulseek:state", offlineNow ? { kind: "disconnected" } : { kind: "connected", username: MOCK_USER });
            return offlineNow;
        },
        clear() {
            armedFail.clear();
            armedAnswer.clear();
            if (offlineNow) { offlineNow = false; mockEmit("soulseek:state", { kind: "connected", username: MOCK_USER }); }
            return true;
        },
    };
}
