/**
 * Phone number formatting for display.
 * Ported from webapp/js/contactHtml.js (formatPhoneDisplay).
 *
 * Russian format: 79146522209 -> +7 914 652-22-09.
 * Accepts 10 digits (9XXXXXXXXX), 11 digits (79XXXXXXXXX or 89XXXXXXXXX).
 * Other lengths are returned as-is (digits only).
 */
export function formatPhoneDisplay(phone: string | null | undefined): string {
  if (phone == null || String(phone).trim() === "") return "";
  const digits = String(phone).replace(/\D/g, "");
  if (digits.length === 10) {
    return (
      "+7 " +
      digits.slice(0, 3) +
      " " +
      digits.slice(3, 6) +
      "-" +
      digits.slice(6, 8) +
      "-" +
      digits.slice(8)
    );
  }
  if (digits.length === 11 && (digits[0] === "7" || digits[0] === "8")) {
    const rest = digits.slice(1);
    return (
      "+7 " +
      rest.slice(0, 3) +
      " " +
      rest.slice(3, 6) +
      "-" +
      rest.slice(6, 8) +
      "-" +
      rest.slice(8)
    );
  }
  return digits;
}
