import jalaali from 'jalaali-js'

export function gregorianToJalaliString(isoString, separator = '/', persianDigits = false) {
  if (!isoString) return ''

  const match = isoString.match(/^(\d{4})-(\d{2})-(\d{2})/)
  if (!match) return ''

  const gy = Number.parseInt(match[1], 10)
  const gm = Number.parseInt(match[2], 10)
  const gd = Number.parseInt(match[3], 10)

  try {
    const jalali = jalaali.toJalaali(gy, gm, gd)
    let jy = String(jalali.jy)
    let jm = String(jalali.jm).padStart(2, '0')
    let jd = String(jalali.jd).padStart(2, '0')

    if (persianDigits) {
      const enToFa = {
        0: '۰',
        1: '۱',
        2: '۲',
        3: '۳',
        4: '۴',
        5: '۵',
        6: '۶',
        7: '۷',
        8: '۸',
        9: '۹',
      }

      jy = jy.replace(/\d/g, (d) => enToFa[d])
      jm = jm.replace(/\d/g, (d) => enToFa[d])
      jd = jd.replace(/\d/g, (d) => enToFa[d])
    }
    return `${jy}${separator}${jm}${separator}${jd}`
  } catch {
    return ''
  }
}

export function gregorianToJalaliStringLong(isoString, separator = '/', persianDigits = false) {
  if (!isoString) return ''

  const match = isoString.match(/^(\d{4})-(\d{2})-(\d{2})[\sT](\d{2}):(\d{2})/)
  if (!match) return ''

  const gy = Number.parseInt(match[1], 10)
  const gm = Number.parseInt(match[2], 10)
  const gd = Number.parseInt(match[3], 10)
  const hour = match[4]
  const minute = match[5]

  try {
    const jalali = jalaali.toJalaali(gy, gm, gd)
    let jy = String(jalali.jy)
    let jm = String(jalali.jm).padStart(2, '0')
    let jd = String(jalali.jd).padStart(2, '0')
    let hh = hour
    let mm = minute

    if (persianDigits) {
      const enToFa = {
        0: '۰',
        1: '۱',
        2: '۲',
        3: '۳',
        4: '۴',
        5: '۵',
        6: '۶',
        7: '۷',
        8: '۸',
        9: '۹',
      }

      jy = jy.replace(/\d/g, (d) => enToFa[d])
      jm = jm.replace(/\d/g, (d) => enToFa[d])
      jd = jd.replace(/\d/g, (d) => enToFa[d])
      hh = hh.replace(/\d/g, (d) => enToFa[d])
      mm = mm.replace(/\d/g, (d) => enToFa[d])
    }

    return ` ${jy}${separator}${jm}${separator}${jd}، ${hh}:${mm}`
  } catch {
    return ''
  }
}

export function JalaliToGregorianString(jalaliStr) {
  try {
    if (!jalaliStr) return null
    const [jy, jm, jd] = jalaliStr.split('/').map(Number)
    const greg = jalaali.toGregorian(jy, jm, jd)
    return `${greg.gy}-${String(greg.gm).padStart(2, '0')}-${String(greg.gd).padStart(
      2,
      '0'
    )}T00:00:00.000Z`
  } catch {
    return ''
  }
}

export function JalaliToGregorianStringWithTime(jalaliDateTimeStr) {
  try {
    if (!jalaliDateTimeStr) return null

    const parts = jalaliDateTimeStr.split(' ')
    const datePart = parts[0]
    const timePart = parts[1] || '00:00'

    const [jy, jm, jd] = datePart.split('/').map(Number)
    const greg = jalaali.toGregorian(jy, jm, jd)

    return `${greg.gy}-${String(greg.gm).padStart(2, '0')}-${String(greg.gd).padStart(
      2,
      '0'
    )}T${timePart}:00.000Z`
  } catch {
    return ''
  }
}
