import { APIClient } from "./ApiClient";

export interface ServerConfig {
  auth_disabled: boolean;
  oidc_provider_name: string;
  apk_download_url: string;
  accounts_types: AccountType[];
  constraints: ServerConstraints;
}

export interface AccountType {
  label: string;
  code: string;
  icon: string;
}

export interface ServerConstraints {
  token_name: LenConstraint;
  token_ip_net: LenConstraint;
  token_max_inactivity: LenConstraint;
  account_name: LenConstraint;
  movement_label: LenConstraint;
  inbox_entry_label: LenConstraint;
  file_allowed_types: string[];
}

export interface LenConstraint {
  min: number;
  max: number;
}

let config: ServerConfig | null = null;

export class ServerApi {
  /**
   * Get server configuration
   */
  static async LoadConfig(): Promise<void> {
    config = (
      await APIClient.exec({
        uri: "/server/config",
        method: "GET",
      })
    ).data;
  }

  /**
   * Get cached configuration
   */
  static get Config(): ServerConfig {
    if (config === null) throw new Error("Missing configuration!");
    return config;
  }
}
