export function isValidURL(string: string) {
  let url;
  try {
    url = new URL(string);
  } catch (_) {
    return false;
  }

  return url.protocol === "http:" || url.protocol === "https:";
}

export function isValidHex(hex: string) {
  return /^(#)?([a-fA-F0-9]{3}|[a-fA-F0-9]{6})$/.test(hex);
}

// Plain-text star glyphs for email templates, which can't rely on icon
// fonts rendering consistently across mail clients.
export const starsDisplay = (rating: number) => {
  const filled = Math.max(0, Math.min(5, Math.round(rating)));
  return "★".repeat(filled) + "☆".repeat(5 - filled);
};

// Parses an optional date field from a request body.
//   undefined        -> field not sent, leave the stored value alone
//   null / ""        -> explicitly cleared
//   valid date string -> Date
//   anything else    -> invalid
export const parseOptionalDate = (
  value: unknown,
): { valid: boolean; date?: Date | null } => {
  if (value === undefined) return { valid: true, date: undefined };
  if (value === null || value === "") return { valid: true, date: null };
  if (typeof value !== "string") return { valid: false };

  const date = new Date(value);
  if (isNaN(date.getTime())) return { valid: false };
  return { valid: true, date };
};

export const isValidEmail =(email: string) => {
  return String(email)
    .toLowerCase()
    .match(
      /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|.(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    );
};
