/**
 * Reactive state for admin invite management.
 *
 * Flows every operation through an injected `AdminInvitesRpc` adapter — the
 * class stays decoupled from the concrete RPC client so tests can inject
 * plain-function stubs. Mirrors `AdminAccountsRpc` / `AuditLogRpc`.
 *
 * Holds two `AsyncSlot`s — `list` (fetch) and `create` (singular write) —
 * plus one `KeyedAsyncSlot<Uuid>` (`remove`) for the per-row delete with
 * correct per-row supersession and per-row error surfacing. Method names
 * use the `submit_*` prefix to avoid slot-name collisions (`delete` is
 * reserved at top-level positions; renamed for symmetry).
 *
 * @module
 */
import { create_context } from '@fuzdev/fuz_ui/context_helpers.js';
import { AsyncSlot } from './async_slot.svelte.js';
import { KeyedAsyncSlot } from './keyed_async_slot.svelte.js';
/**
 * Svelte context carrying the reactive `AdminInvitesRpc` accessor. Mirrors
 * `admin_accounts_rpc_context`. `get()` throws when no provisioner ran above
 * the component — the adapter is required.
 */
export const admin_invites_rpc_context = create_context();
export class AdminInvitesState {
    #get_rpc;
    list = new AsyncSlot();
    create = new AsyncSlot();
    remove = new KeyedAsyncSlot();
    invites = $state.raw([]);
    invite_count = $derived(this.invites.length);
    unclaimed_count = $derived(this.invites.filter((i) => !i.claimed_at).length);
    constructor(options) {
        this.#get_rpc = options.get_rpc;
    }
    async fetch() {
        await this.list.run(async () => {
            const { invites } = await this.#get_rpc().list();
            this.invites = invites;
        });
    }
    async submit_create(email, username) {
        await this.create.run(async () => {
            await this.#get_rpc().create({ email: email ?? null, username: username ?? null });
        });
        if (!this.create.succeeded)
            return false;
        await this.fetch();
        return true;
    }
    async submit_delete(id) {
        await this.remove.run(id, async () => {
            await this.#get_rpc().delete({ invite_id: id });
        });
        if (this.remove.succeeded(id))
            await this.fetch();
    }
}
