/**
 * Currency registry — type definitions & runtime accessor.
 *
 * The SINGLE SOURCE OF TRUTH lives in PHP:
 *   class-crtoday-plugin-settings.php → CURRENCIES constant
 *
 * This file intentionally contains NO hardcoded data.
 * The currency list is injected at runtime via wp_localize_script()
 * as `window.crtodayCurrencies`.
 *
 * @see docs/contracts/currency-registry.md
 */

export type CurrencyEntry = {
  /** ISO 4217 code (e.g. "USD", "BTC"). */
  code: string;
  /** Translated human-readable name (e.g. "US Dollar"). */
  name: string;
  /** Flag file stem inside assets/flags/webp/128/ (e.g. "us", "gold"). */
  flag: string;
  /** Regional group key (e.g. "north_america", "europe", "cryptocurrency", "virtual"). */
  group: string;
};

export type EditorCurrenciesUi = {
  flagBaseUrl?: string;
  groupOrder?: string[];
  groupLabels?: Record<string, string>;
  labels?: {
    searchCurrency?: string;
    noResults?: string;
    noCurrenciesSelected?: string;
    selectedCurrenciesPattern?: string;
    sourceGroupBuiltIn?: string;
    sourceGroupLocal?: string;
    exchangeUrlPlaceholder?: string;
    converterUrlPlaceholder?: string;
  };
};

declare global {
  interface Window {
    crtodayCurrencies?: CurrencyEntry[];
    crtodayEditorCurrenciesUi?: EditorCurrenciesUi;
  }
}

/**
 * Return the currency list provided by the server.
 * Falls back to an empty array when called outside WordPress admin.
 */
export function getCurrencies(): CurrencyEntry[] {
  return window.crtodayCurrencies ?? [];
}
