// AUTO-GENERATED by django_generator / ts_extras.wrapper
// Thin per-group proxy over the global `auth` store. All actual auth
// wiring lives in `helpers/auth.ts` (one interceptor, one source of
// truth). DO NOT EDIT — re-run `make gen`.

import { auth } from '../helpers/auth';
import { APILogger, type LoggerConfig } from '../helpers/logger';

import { CfgTotpBackupCodes } from '../sdk.gen';
import { CfgTotp } from '../sdk.gen';
import { CfgTotpSetup } from '../sdk.gen';
import { CfgTotpVerify } from '../sdk.gen';



export interface APIOptions {
  /** Logger config (defaults to dev-only). */
  logger?: Partial<LoggerConfig>;
  /** Locale for `Accept-Language`. If omitted, auto-detected from cookie/navigator. */
  locale?: string;
  /** API key sent as `X-API-Key`. Falls back to NEXT_PUBLIC_API_KEY. */
  apiKey?: string;
  /** Send Django session/CSRF cookies cross-origin. Defaults to true. */
  withCredentials?: boolean;
}

/**
 * Per-group ergonomic facade.
 *
 * Calling `setToken/setApiKey/setLocale/setBaseUrl` proxies to the
 * global `auth` store — the change applies to **every** group's API
 * instance because they share the same HTTP client and interceptor.
 *
 * Use the global `auth` object directly when you don't need a group
 * facade: `import { auth } from '@your/api'; auth.setToken(jwt);`
 */
export class API {
  readonly logger: APILogger;

  readonly cfgTotpBackupCodes = CfgTotpBackupCodes;
  readonly cfgTotp = CfgTotp;
  readonly cfgTotpSetup = CfgTotpSetup;
  readonly cfgTotpVerify = CfgTotpVerify;

  constructor(_baseUrl?: string, opts: APIOptions = {}) {
    this.logger = new APILogger(opts.logger);
    if (_baseUrl) auth.setBaseUrl(_baseUrl);
    if (opts.locale !== undefined) auth.setLocale(opts.locale);
    if (opts.apiKey !== undefined) auth.setApiKey(opts.apiKey);
    if (opts.withCredentials !== undefined) auth.setWithCredentials(opts.withCredentials);
  }

  // ── Base URL ────────────────────────────────────────────────────────────
  getBaseUrl(): string { return auth.getBaseUrl(); }
  setBaseUrl(url: string): void { auth.setBaseUrl(url); }

  // ── Tokens ──────────────────────────────────────────────────────────────
  getToken(): string | null { return auth.getToken(); }
  setToken(token: string | null): void { auth.setToken(token); }
  getRefreshToken(): string | null { return auth.getRefreshToken(); }
  setRefreshToken(token: string | null): void { auth.setRefreshToken(token); }
  clearToken(): void { auth.clearTokens(); }
  isAuthenticated(): boolean { return auth.isAuthenticated(); }

  // ── Locale / API key ────────────────────────────────────────────────────
  getLocale(): string | null { return auth.getLocale(); }
  setLocale(locale: string | null): void { auth.setLocale(locale); }
  getApiKey(): string | null { return auth.getApiKey(); }
  setApiKey(key: string | null): void { auth.setApiKey(key); }

  // ── 401 handling ────────────────────────────────────────────────────────
  /** Fired only on terminal 401 (after refresh+retry path is exhausted). */
  onUnauthorized(cb: ((response: Response) => void) | null): void {
    auth.onUnauthorized(cb);
  }
  /** Provide a refresh strategy. See `auth.setRefreshHandler` for the contract. */
  setRefreshHandler(
    fn: ((refreshToken: string) => Promise<{ access: string; refresh?: string } | null>) | null,
  ): void {
    auth.setRefreshHandler(fn);
  }
  /**
   * Proactively refresh now via the registered handler, sharing the single
   * 401-recovery flight (rotation + dedup + rotated-token persistence).
   * See `auth.refreshNow`.
   */
  refreshNow(): Promise<string | null> {
    return auth.refreshNow();
  }
}

export { CfgTotpBackupCodes, CfgTotp, CfgTotpSetup, CfgTotpVerify };
export { auth };
