/** Canonical Iran mobile: +989XXXXXXXXX (12 chars). */
export const IR_MOBILE_REGEX = /^\+989\d{9}$/;

/**
 * Normalize user-entered mobile to E.164 for Iran (+98…).
 * Accepts 09…, 9…, +98…, 0098… with optional spaces/dashes.
 */
export function normalizeMobile(input: string): string | null {
  const trimmed = input?.trim();
  if (!trimmed) {
    return null;
  }

  let digits = trimmed.replace(/[^\d+]/g, '');
  if (digits.startsWith('+')) {
    digits = digits.slice(1);
  }

  digits = digits.replace(/\D/g, '');

  if (digits.startsWith('0098')) {
    digits = digits.slice(4);
  } else if (digits.startsWith('98') && digits.length >= 12) {
    digits = digits.slice(2);
  }

  if (digits.startsWith('0') && digits.length === 11) {
    digits = digits.slice(1);
  }

  if (digits.length === 10 && digits.startsWith('9')) {
    return `+98${digits}`;
  }

  return null;
}

export function isValidMobile(normalized: string): boolean {
  return IR_MOBILE_REGEX.test(normalized);
}

/** Display-friendly local format: 09XX XXX XXXX */
export function formatMobileForDisplay(normalized: string): string {
  if (!isValidMobile(normalized)) {
    return normalized;
  }
  const local = `0${normalized.slice(3)}`;
  return `${local.slice(0, 4)} ${local.slice(4, 7)} ${local.slice(7)}`;
}

/** Strip to digits only for partial search matching. */
export function mobileSearchDigits(input: string): string {
  return input.replace(/\D/g, '');
}
