/**
 * WHMCS Parsing Utilities (domain-internal)
 */

/**
 * Parse amount from WHMCS API response.
 * WHMCS returns amounts as strings or numbers.
 */
export function parseAmount(amount: string | number | undefined): number {
  if (typeof amount === "number") return amount;
  if (!amount) return 0;

  const cleaned = String(amount).replace(/[^\d.-]/g, "");
  const parsed = Number.parseFloat(cleaned);
  return Number.isNaN(parsed) ? 0 : parsed;
}

/**
 * Format date from WHMCS API to ISO string.
 * Returns undefined if input is invalid.
 */
export function formatDate(input?: string | null): string | undefined {
  if (!input) return undefined;

  const date = new Date(input);
  if (Number.isNaN(date.getTime())) return undefined;

  return date.toISOString();
}
