/**
 * Admin RPC adapter helpers for consumer UIs.
 *
 * Bridges a typed throwing RPC client to the four narrow admin RPC
 * interfaces the state classes consume — `AdminAccountsRpc`,
 * `AdminInvitesRpc`, `AuditLogRpc`, `AppSettingsRpc`. Two calls at the
 * admin shell layout wire everything.
 *
 * Intentionally admin-only despite the backend-side
 * `create_standard_rpc_actions` rename (admin + role-grant-offer + account).
 * Account-surface methods flow through `account_sessions_rpc_context`
 * (wired at the self-service layout), and role-grant-offer methods that
 * surface in the admin UI (`role_grant_offer_create`, `role_grant_revoke`,
 * `role_grant_offer_retract`) live inside the `AdminAccountsRpc` interface —
 * they belong to the admin UX, not a separate wire pairing. The UI side
 * and backend factory names diverge by design.
 *
 * ```ts
 * // `api` is the typed throwing Proxy from `create_frontend_rpc_client`.
 * provide_admin_rpc_contexts(create_admin_rpc_adapters(api));
 * ```
 *
 * The throwing Proxy spreads the JSON-RPC `{code, message, data?}` onto
 * the thrown `Error` so form components (e.g. `ui/RoleGrantOfferForm.svelte`)
 * can match on `error.data?.reason` via `ERROR_ROLE_GRANT_OFFER_*` constants —
 * optional chaining is required because JSON-RPC `data` is spec-level
 * optional. Consumers that need a custom unwrap strategy can construct
 * their own object satisfying `AdminRpcApi` and pass it directly.
 *
 * No `.svelte.ts` suffix — this module holds no reactive state, only
 * method-name mappings.
 *
 * @module
 */
import { admin_accounts_rpc_context } from './admin_accounts_state.svelte.js';
import { admin_invites_rpc_context } from './admin_invites_state.svelte.js';
import { audit_log_rpc_context } from './audit_log_state.svelte.js';
import { app_settings_rpc_context } from './app_settings_state.svelte.js';
import { format_scope_context } from './format_scope.js';
/**
 * Build the four admin RPC adapters from a typed throwing RPC client.
 *
 * Method-name mapping:
 *
 * | Narrow RPC method                   | Action spec method           |
 * | ----------------------------------- | ---------------------------- |
 * | `admin_accounts.list_accounts`      | `admin_account_list`         |
 * | `admin_accounts.delete_account`     | `account_delete` (soft)      |
 * | `admin_accounts.undelete_account`   | `account_undelete`           |
 * | `admin_accounts.list_sessions`      | `admin_session_list`         |
 * | `admin_accounts.create_role_grant`       | `role_grant_offer_create`        |
 * | `admin_accounts.revoke_role_grant`      | `role_grant_revoke`              |
 * | `admin_accounts.retract_offer`      | `role_grant_offer_retract`       |
 * | `admin_accounts.session_revoke_all` | `admin_session_revoke_all`   |
 * | `admin_accounts.token_revoke_all`   | `admin_token_revoke_all`     |
 * | `admin_invites.list`                | `invite_list`                |
 * | `admin_invites.create`              | `invite_create`              |
 * | `admin_invites.delete`              | `invite_delete`              |
 * | `audit_log.list`                    | `audit_log_list`             |
 * | `audit_log.role_grant_history`          | `audit_log_role_grant_history`   |
 * | `app_settings.get`                  | `app_settings_get`           |
 * | `app_settings.update`               | `app_settings_update`        |
 *
 * All four adapter factories call through the same `api` — consumers
 * pass the typed throwing Proxy from `create_frontend_rpc_client` once,
 * regardless of how many admin surfaces they mount.
 */
export const create_admin_rpc_adapters = (api) => ({
    admin_accounts: {
        list_accounts: (include_deleted) => api.admin_account_list({ include_deleted }),
        delete_account: (account_id) => api.account_delete({ account_id }),
        undelete_account: (account_id) => api.account_undelete({ account_id }),
        list_sessions: () => api.admin_session_list(),
        create_role_grant: (params) => api.role_grant_offer_create(params),
        revoke_role_grant: (params) => api.role_grant_revoke(params),
        retract_offer: (offer_id) => api.role_grant_offer_retract({ offer_id }),
        session_revoke_all: (params) => api.admin_session_revoke_all(params),
        token_revoke_all: (params) => api.admin_token_revoke_all(params),
    },
    admin_invites: {
        list: () => api.invite_list(),
        create: (params) => api.invite_create(params),
        delete: (params) => api.invite_delete(params),
    },
    audit_log: {
        list: (options) => api.audit_log_list(options ?? {}),
        role_grant_history: (params) => api.audit_log_role_grant_history(params ?? {}),
    },
    app_settings: {
        get: () => api.app_settings_get(),
        update: (params) => api.app_settings_update(params),
    },
});
/**
 * Wire all four admin RPC contexts in a single call.
 *
 * Call once at the admin shell layout (e.g. `src/routes/admin/+layout.svelte`)
 * with adapters built from `create_admin_rpc_adapters`. Every `Admin*.svelte`
 * component that reads a context below this point sees the adapters.
 *
 * Each context accessor reads `adapters.{domain}` on every invocation, so
 * mutating an adapter field on the same object propagates. Replacing the
 * whole adapter set requires calling `provide_admin_rpc_contexts` again
 * during init — in practice this is one-shot at layout mount.
 *
 * Pass `options.format_scope` to render role_grant/offer `scope_id` values as
 * human labels across `AdminAccounts`, `AdminRoleGrantHistory`,
 * `RoleGrantOfferInbox`, `RoleGrantOfferForm`, and `RoleGrantOfferHistory`.
 * Components that accept a `format_scope` prop honor the prop first; the
 * context is the fallback.
 */
export const provide_admin_rpc_contexts = (adapters, options) => {
    admin_accounts_rpc_context.set(() => adapters.admin_accounts);
    admin_invites_rpc_context.set(() => adapters.admin_invites);
    audit_log_rpc_context.set(() => adapters.audit_log);
    app_settings_rpc_context.set(() => adapters.app_settings);
    if (options?.format_scope) {
        const { format_scope } = options;
        format_scope_context.set(() => format_scope);
    }
};
