(function () {
  window.PrimeBridge = window.PrimeBridge || {};

  /**
   * PrimeBridge.random — Random utility functions.
   *
   * Provides helpers for picking random elements, generating random delays,
   * formatting amounts, shuffling arrays, and selecting unique subsets.
   *
   * @namespace PrimeBridge.random
   */
  PrimeBridge.random = {
    /**
     * pickRandom — Returns a random element from an array.
     *
     * @param {Array} array — The source array.
     * @returns {*} A randomly selected element, or undefined if empty.
     */
    pickRandom: function (array) {
      if (!Array.isArray(array) || array.length === 0) return undefined;
      return array[Math.floor(Math.random() * array.length)];
    },

    /**
     * randomInt — Returns a random integer between min and max (inclusive).
     *
     * @param {number} min — Minimum value.
     * @param {number} max — Maximum value.
     * @returns {number} A random integer in [min, max].
     */
    randomInt: function (min, max) {
      min = Math.ceil(min);
      max = Math.floor(max);
      return Math.floor(Math.random() * (max - min + 1)) + min;
    },

    /**
     * randomDelay — Returns a random millisecond delay between min and max.
     *
     * @param {number} [min=3000] — Minimum delay in ms.
     * @param {number} [max=8000] — Maximum delay in ms.
     * @returns {number} A random delay value.
     */
    randomDelay: function (min, max) {
      if (min === undefined) min = 3000;
      if (max === undefined) max = 8000;
      return Math.floor(Math.random() * (max - min + 1)) + min;
    },

    /**
     * randomAmount — Formats a random amount as a comma-separated string with a prefix.
     *
     * @param {number} min — Minimum amount.
     * @param {number} max — Maximum amount.
     * @param {string} [prefix='$'] — Currency prefix (e.g. '$', '€', '₦').
     * @returns {string} Formatted amount, e.g. " $5,234 ".
     */
    randomAmount: function (min, max, prefix) {
      if (prefix === undefined) prefix = '$';
      var amount = Math.floor(Math.random() * (max - min + 1)) + min;
      return ' ' + prefix + amount.toLocaleString() + ' ';
    },

    /**
     * pickRandomN — Picks n unique random items from an array.
     *
     * Uses Fisher-Yates partial shuffle. If n >= array length, returns a shuffled copy.
     *
     * @param {Array} array — The source array.
     * @param {number} n — Number of items to pick.
     * @returns {Array} Array of n unique random items.
     */
    pickRandomN: function (array, n) {
      if (!Array.isArray(array) || array.length === 0) return [];
      var result = array.slice();
      var len = result.length;
      var count = Math.min(n, len);

      for (var i = 0; i < count; i++) {
        var rand = Math.floor(Math.random() * (len - i)) + i;
        var temp = result[i];
        result[i] = result[rand];
        result[rand] = temp;
      }

      return result.slice(0, count);
    },

    /**
     * shuffle — Fisher-Yates shuffle. Returns a new shuffled array.
     *
     * @param {Array} array — The array to shuffle.
     * @returns {Array} A new array with elements in random order.
     */
    shuffle: function (array) {
      if (!Array.isArray(array)) return [];
      var result = array.slice();
      var len = result.length;

      for (var i = len - 1; i > 0; i--) {
        var j = Math.floor(Math.random() * (i + 1));
        var temp = result[i];
        result[i] = result[j];
        result[j] = temp;
      }

      return result;
    },

    /**
     * pickWeighted — Picks a random element from a weighted array.
     *
     * Elements repeated more frequently in the array have higher
     * probability of selection. Pass a flat array where duplicates
     * represent weight, e.g.:
     *   ['deposit','deposit','deposit','withdrawal','bonus']
     * gives deposit ~60% chance, withdrawal ~20%, bonus ~20%.
     *
     * @param {Array} weightedArray — Array with weighted duplicates.
     * @returns {*} A randomly selected element, or undefined if empty.
     *
     * @example
     *   var type = PrimeBridge.random.pickWeighted([
     *     'deposit','deposit','deposit',
     *     'withdrawal',
     *     'bonus'
     *   ]);
     *   // 'deposit' is 3× more likely than 'withdrawal' or 'bonus'
     */
    pickWeighted: function (weightedArray) {
      if (!Array.isArray(weightedArray) || weightedArray.length === 0) return undefined;
      return weightedArray[Math.floor(Math.random() * weightedArray.length)];
    }
  };
})();
