import { format } from "date-fns-jalali";
import { enUS } from "date-fns/locale";
import PersianDate from "persian-date";

const persianLocale = {
  ...enUS,
  localize: {
    ...enUS.localize,
    month: (n) =>
      [
        "فروردین",
        "اردیبهشت",
        "خرداد",
        "تیر",
        "مرداد",
        "شهریور",
        "مهر",
        "آبان",
        "آذر",
        "دی",
        "بهمن",
        "اسفند",
      ][n],
  },
  formatLong: {
    ...enUS.formatLong,
    date: () => "d MMMM yyyy",
  },
};

export const formatTime = (time) => {
  const date = new Date(time);
  const hours = date.getHours().toString().padStart(2, "0");
  const minutes = date.getMinutes().toString().padStart(2, "0");

  return format(new Date(time), "yyyy/MM/dd  ") + `(${hours}:${minutes})`;
};

export const formatTimeFull = (time) => {
  const date = new Date(time);
  const hours = date.getHours().toString().padStart(2, "0");
  const minutes = date.getMinutes().toString().padStart(2, "0");
  const seconds = date.getSeconds().toString().padStart(2, "0");

  return (
    format(new Date(time), "yyyy/MM/dd  ") + `(${hours}:${minutes}:${seconds})`
  );
};

export const formatJustDate = (time) => {
  if (time) {
    return format(new Date(time), "yyyy/MM/dd");
  } else {
    return null;
  }
};

export const formatJustTime = (time) => {
  return format(new Date(time), "HH:MM");
};

export const formatJustDateGregorian = (time) => {
  const timePortion = time.getTime() % (3600 * 1000 * 24);
  return new Date(time - timePortion);
};

export function formatTimeStampDate(timestamp) {
  const date = new Date(timestamp);
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, "0");
  const day = String(date.getDate()).padStart(2, "0");
  return `${year}/${month}/${day}`;
}

export function convertToIranianTime(dateString) {
  const gregorianDate = new Date(dateString);
  const iranianDate = new PersianDate(gregorianDate);
  const iranianDateString = iranianDate.format("YYYY/MM/DD");

  const asciiIranianDateString = iranianDateString.replace(
    /[۰-۹]/g,
    function (digit) {
      return String.fromCharCode(digit.charCodeAt(0) - 1728);
    }
  );

  return asciiIranianDateString;
}

export function getPersianMonth(dateString) {
  const date = new Date(dateString);
  const persianMonth = format(date, "MMMM", { locale: persianLocale });
  return persianMonth + " " + format(new Date(date), "yyyy");
}

export function convertDaysToYMD(days) {
  const daysInYear = 365;
  const daysInMonth = 30;

  const years = Math.floor(days / daysInYear);
  days = days % daysInYear;

  const months = Math.floor(days / daysInMonth);
  days = days % daysInMonth;

  const remainingDays = days;

  const result = [];
  if (years > 0) result.push(`${years} سال`);
  if (months > 0) result.push(`${months} ماه`);
  if (remainingDays > 0) result.push(`${remainingDays} روز`);

  return result.join(" و ");
}

export function convertPersianToEnglishNumerals(text) {
  if (!text || typeof text !== "string") {
    return text;
  }

  const persianToEnglish = {
    "۰": "0",
    "۱": "1",
    "۲": "2",
    "۳": "3",
    "۴": "4",
    "۵": "5",
    "۶": "6",
    "۷": "7",
    "۸": "8",
    "۹": "9",
  };

  return text.replace(/[۰-۹]/g, (char) => persianToEnglish[char] || char);
}
