export const formatCurrency = (amount: number) => {
  return new Intl.NumberFormat('id-ID', {
    style: 'currency',
    currency: 'IDR',
    minimumFractionDigits: 0
  }).format(amount)
}

export const formatShortCurrency = (num: number): string => {
  const formatNumber = (value: number, suffix: string) => {
    const str = value.toFixed(2).replace(/\.00$/, '')
    return str + suffix
  }

  const absNum = Math.abs(num)
  let result: string

  if (absNum >= 1_000_000) {
    result = formatNumber(absNum / 1_000_000, 'M')
  } else if (absNum >= 1_000) {
    result = formatNumber(absNum / 1_000, 'k')
  } else {
    result = absNum.toString()
  }

  return num < 0 ? '-' + result : result
}

export const formatDate = (dateString: any) => {
  return new Date(dateString).toLocaleDateString('id-ID', {
    day: 'numeric',
    month: 'short',
    year: 'numeric'
  })
}

export const formatDateDDMMYYYY = (dateString: Date | string) => {
  const date = new Date(dateString)
  date.setHours(0, 0, 0, 0)
  const day = String(date.getDate()).padStart(2, '0')
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const year = date.getFullYear()
  return `${day}-${month}-${year}`
}

export const formatForInputDate = (dateString: Date | string) => {
  const date = new Date(dateString)
  const day = String(date.getDate()).padStart(2, '0')
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const year = date.getFullYear()
  return `${year}-${month}-${day}`
}


export const formatDatetime = (dateString: string | number | Date) => {
  const date = new Date(dateString)

  const day = String(date.getDate()).padStart(2, '0')
  const month = date.toLocaleString('id-ID', { month: 'long' }) // Aug
  const year = date.getFullYear()

  const hours = String(date.getHours()).padStart(2, '0')
  const minutes = String(date.getMinutes()).padStart(2, '0')

  return `${day} ${month} ${year} ${hours}:${minutes}`
}
