export const convertDate = (date: string | Date, region: "fa-IR" | "en-US" = "fa-IR") => {
  const datetime = new Date(date);
  const result = datetime.toLocaleDateString(region, { year: 'numeric', month: 'long', day: 'numeric' });
  return result;
}

export function getTimeUntilStart(startDate: string | Date): string {
  const start = new Date(startDate).getTime()
  const now = Date.now()

  const diff = start - now

  if (diff <= 0) return ""

  const hours = Math.floor(diff / (1000 * 60 * 60))
  const days = Math.floor(hours / 24)

  if (days >= 1) {
    return `${days.toLocaleString("fa-IR")} روز تا شروع دوره`
  }

  return `${hours.toLocaleString("fa-IR")} ساعت تا شروع دوره`
}


export function getRemainingTime(endDate: string | Date): string {
  const end = new Date(endDate).getTime()
  const now = Date.now()

  const diff = end - now

  if (diff <= 0) return "به پایان رسیده"

  const hours = Math.floor(diff / (1000 * 60 * 60))
  const days = Math.floor(hours / 24)

  if (days >= 1) {
    return `${days.toLocaleString("fa-IR")} روز باقی مانده`
  }

  return `${hours.toLocaleString("fa-IR")} ساعت باقی مانده`
}

export function getPersianDateTime() {
  const now = new Date()

  const date = new Intl.DateTimeFormat("fa-IR-u-ca-persian", {
    year: "numeric",
    month: "2-digit",
    day: "2-digit",
  }).format(now)

  const time = now.toTimeString().split(" ")[0]

  return `${date} ${time}`
} 
export function jalaliDay(date: string | Date = new Date()): string {
  const value = typeof date === "string" && /^\d{4}-\d{2}-\d{2}$/.test(date) ? new Date(`${date}T12:00:00`) : new Date(date)
  try {
    return new Intl.DateTimeFormat("fa-IR-u-ca-persian", { weekday: "long", day: "numeric", month: "long" }).format(value)
  } catch {
    return typeof date === "string" ? date : ""
  }
}
