export const CHARS_NUMERIC = '0123456789';
export const CHARS_ALPHA_LOWER = 'abcdefghijklmnopqrstuvwxyz';
export const CHARS_ALPHA_UPPER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
export const CHARS_ALPHA_NUMERIC = CHARS_NUMERIC + CHARS_ALPHA_LOWER + CHARS_ALPHA_UPPER;

/**
 * Generate a random string of a given length using a given set of characters
 * @param {number} length - the length of the string to generate
 * @param {string} [allowedChars] - the set of characters to use in the string, defaults to all alphanumeric characters in upper and lower case + numbers
 * @returns {string} - the generated string
 */
export function randomText(length: number, allowedChars = CHARS_ALPHA_NUMERIC) {
  let result = '';
  const charLength = allowedChars.length;
  let counter = 0;
  while (counter < length) {
    result += allowedChars.charAt(Math.floor(Math.random() * charLength));
    counter += 1;
  }
  return result;
}
/**
 * Compare two strings including null and undefined values
 * @param {string} a - the first string to compare
 * @param {string} b - the second string to compare
 * @returns {boolean} - true if the strings are the same or both null or undefined, false otherwise
 */
export function compareTexts(a: string | null | undefined, b: string | null | undefined) {
  if (a === undefined || a === null || a === '') {
    return b === undefined || b === null || b === '';
  }
  return a === b;
}

/**
 * Capitalize the first letter of a string
 * @param {string} s - the string to capitalize
 * @returns {string} - the capitalized string
 */
export const capitalize = (s: string): string => s.charAt(0).toUpperCase() + s.substring(1);

/**
 * Generate a random color as #RRGGBB value
 * @returns {string} - the generated color
 */
export function randomColor() {
  const color = Math.floor(Math.random() * 16777215).toString(16);
  return '#' + color;
}

const PERSIAN_DIGITS = '۰۱۲۳۴۵۶۷۸۹';
const ARABIC_DIGITS = '٠١٢٣٤٥٦٧٨٩';

/**
 * Normalizes Persian (۰-۹) and Arabic (٠-٩) digits to ASCII 0-9. Essential for any
 * numeric input (phone, OTP, national id) since Persian keyboards emit Persian digits.
 */
export function toEnglishDigits(input: string): string {
  return input.replace(/[۰-۹٠-٩]/g, (ch) => {
    const persian = PERSIAN_DIGITS.indexOf(ch);
    if (persian > -1) return String(persian);
    const arabic = ARABIC_DIGITS.indexOf(ch);
    return arabic > -1 ? String(arabic) : ch;
  });
}

/** Normalizes to ASCII digits and strips everything that isn't a digit. */
export function digitsOnly(input: string): string {
  return toEnglishDigits(input).replace(/\D/g, '');
}
