/* ==========================================================================
   PrimeBridge Country Registry Module
   ==========================================================================
   Single source of truth for all country data. Every module (generator,
   modal, notifications) pulls from here — no more hardcoded arrays.

   Source file: public/assets/data/countries.json
   ========================================================================== */

(function () {
  'use strict';

  var _index = null;   // Map: country name → country record
  var _list  = [];     // Array of full country records (for random picks)

  /**
   * Build lookup index from the raw JSON array.
   * @param {Array} raw - Array of { name, flag, code, dial_code, currency_symbol }
   */
  function _buildIndex(raw) {
    _index = {};
    _list  = [];
    for (var i = 0; i < raw.length; i++) {
      var c = raw[i];
      if (!c || !c.name) continue;
      _index[c.name.toLowerCase()] = c;
      _list.push(c);
    }
  }

  var api = {

    /**
     * Initialize the registry. Call once at boot with the countries.json data.
     * @param {Array} data - Raw JSON array from countries.json (optional)
     */
    init: function (data) {
      if (data && data.length) {
        _buildIndex(data);
      } else if (window.__COUNTRIES_DATA__) {
        _buildIndex(window.__COUNTRIES_DATA__);
      }
    },

    /**
     * Check if the registry has been initialized.
     * @returns {boolean}
     */
    ready: function () {
      return _list.length > 0;
    },

    /**
     * Resolve a country name to its full record.
     * @param {string} name - Country name (case-insensitive)
     * @returns {Object|null} { name, flag, code, currency_symbol } or null
     */
    resolve: function (name) {
      if (!name || !_index) return null;
      return _index[name.toLowerCase()] || null;
    },

    /**
     * Get the display string (flag + name) for a country.
     * Falls back to the raw name if not found.
     * @param {string} name
     * @returns {string} e.g. "🇳🇬 Nigeria"
     */
    display: function (name) {
      if (!name) return '';
      var record = this.resolve(name);
      if (record && record.flag) {
        return record.flag + ' ' + record.name;
      }
      // Already has flag embedded
      if (/[\uD800-\uDBFF][\uDC00-\uDFFF]/.test(name)) return name;
      return name;
    },

    /**
     * Pick a random country record.
     * @returns {Object} { name, flag, code, currency_symbol }
     */
    random: function () {
      if (_list.length === 0) return { name: 'Nigeria', flag: '\uD83C\uDDF3\uD83C\uDDEC', code: 'NG', currency_symbol: '\u20A6' };
      return _list[Math.floor(Math.random() * _list.length)];
    },

    /**
     * Get all country records.
     * @returns {Array}
     */
    all: function () {
      return _list;
    }
  };

  // Register on PrimeBridge namespace
  window.PrimeBridge = window.PrimeBridge || {};
  PrimeBridge.countries = api;

})();
