const POWER_FIELDS = "host envId subdomain owner status";
/**
 * Thin client for the vetra-housekeeping subgraph. The activator only uses the
 * open operations — studioPowerState (query) and wakeStudio (open + idempotent)
 * — so no token is required. (Manual sleepStudio is admin-gated and lives in the
 * dashboard, not here.)
 */
export function createSwitchboardClient(opts) {
    const timeout = opts.fetchTimeoutMs ?? 15_000;
    async function gql(query, variables) {
        const ac = new AbortController();
        const t = setTimeout(() => ac.abort(), timeout);
        try {
            const res = await fetch(opts.url, {
                method: "POST",
                signal: ac.signal,
                headers: { "content-type": "application/json" },
                body: JSON.stringify({ query, variables }),
            });
            const body = (await res.json());
            if (body.errors?.length)
                throw new Error(body.errors.map((e) => e.message).join("; "));
            return body.data;
        }
        finally {
            clearTimeout(t);
        }
    }
    return {
        async powerState(host) {
            const d = await gql(`query($host:String!){ VetraHousekeeping { studioPowerState(host:$host){ ${POWER_FIELDS} } } }`, { host });
            return d.VetraHousekeeping.studioPowerState;
        },
        async wakeStudio(host) {
            const d = await gql(`mutation($host:String!){ VetraHousekeeping { wakeStudio(host:$host){ ${POWER_FIELDS} } } }`, { host });
            return d.VetraHousekeeping.wakeStudio;
        },
    };
}
