export const persianToEnglish = (str: string | number): string => {
  if (typeof str === "number") return str.toString();
  if (!str) return "";

  const persianNumbers = ["۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"];
  const arabicNumbers = ["٠", "١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩"];
  const englishNumbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];

  let result = str.toString();

  for (let i = 0; i < 10; i++) {
    result = result.replace(
      new RegExp(persianNumbers[i], "g"),
      englishNumbers[i]
    );
    result = result.replace(
      new RegExp(arabicNumbers[i], "g"),
      englishNumbers[i]
    );
  }

  return result;
};

export const convertPersianNumbersInObject = (obj: any): any => {
  if (obj === null || obj === undefined) return obj;

  if (typeof obj === "string") {
    return persianToEnglish(obj);
  }

  if (typeof obj === "number") {
    return obj;
  }

  if (Array.isArray(obj)) {
    return obj.map((item) => convertPersianNumbersInObject(item));
  }

  if (typeof obj === "object") {
    const converted: any = {};
    for (const key in obj) {
      if (obj.hasOwnProperty(key)) {
        converted[key] = convertPersianNumbersInObject(obj[key]);
      }
    }
    return converted;
  }

  return obj;
};

export const createNumberTransform = () => {
  return (value: any, originalValue: any) => {
    if (
      originalValue === "" ||
      originalValue === null ||
      originalValue === undefined
    ) {
      return undefined;
    }

    const converted = persianToEnglish(originalValue);
    const num = Number(converted);

    return isNaN(num) ? undefined : num;
  };
};

export const createOptionalNumberTransform = () => {
  return (value: any, originalValue: any) => {
    if (
      originalValue === "" ||
      originalValue === null ||
      originalValue === undefined
    ) {
      return null;
    }

    const converted = persianToEnglish(originalValue);
    const num = Number(converted);

    return isNaN(num) ? null : num;
  };
};

export const formatWithThousands = (value: string | number): string => {
  if (value === null || value === undefined) return "";
  const raw = persianToEnglish(value.toString());
  if (raw === "") return "";
  const hasTrailingDot = /\.$/.test(raw);
  const sanitized = raw.replace(/[^\d.]/g, "");
  const parts = sanitized.split(".");
  const integerPart = parts[0];
  const decimalPart = parts.length > 1 ? parts[1] : "";
  const withCommas = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  if (decimalPart) return `${withCommas}.${decimalPart}`;
  return hasTrailingDot ? `${withCommas}.` : withCommas;
};

export const parseFormattedNumber = (value: any): number | undefined => {
  if (value === null || value === undefined) return undefined;
  const cleaned = persianToEnglish(String(value)).replace(/,/g, "").trim();
  if (cleaned === "") return undefined;
  const num = Number(cleaned);
  return isNaN(num) ? undefined : num;
};

export const englishToPersian = (str: string | number): string => {
  if (str === null || str === undefined) return "";
  const en = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
  const fa = ["۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"];
  let s = String(str);
  for (let i = 0; i < 10; i++) {
    s = s.replace(new RegExp(en[i], "g"), fa[i]);
  }
  return s;
};
