export const delay = (ms: number) => new Promise(res => setTimeout(res, ms));

export const formatDate = (dateString?: string | Date): string => {
    if(!dateString) return '';
    const date = new Date(dateString);
    const yyyy = date.getFullYear();
    const mm = String(date.getMonth() + 1).padStart(2, '0');
    const dd = String(date.getDate()).padStart(2, '0');
    return `${yyyy}-${mm}-${dd}`;
};

export const formatDateTime = (dateString?: string | Date): string => {
    if(!dateString) return '';
    const date = new Date(dateString);
    const yyyy = date.getFullYear();
    const mm = String(date.getMonth() + 1).padStart(2, '0');
    const dd = String(date.getDate()).padStart(2, '0');
    const hh = String(date.getHours()).padStart(2, '0');
    const ii = String(date.getMinutes()).padStart(2, '0');
    const ss = String(date.getSeconds()).padStart(2, '0');
    return `${yyyy}-${mm}-${dd} ${hh}:${ii}:${ss}`;
};

export const formatTanggal = (dateString?: string): string => {
    if(!dateString) return '';
    const date = new Date(dateString);
    const dd = String(date.getDate()).padStart(2, '0');
    const bulanIndonesia = [
        'Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni',
        'Juli', 'Agustus', 'September', 'Oktober', 'November', 'Desember'
    ];
    const mm = bulanIndonesia[date.getMonth()];
    const yyyy = date.getFullYear();
    return `${dd} ${mm} ${yyyy}`;
}

export function hitungJatuhTempo(strDate: string, periode: number): string {
  const date = new Date(strDate);
  const tahun = date.getFullYear();
  const bulan = date.getMonth() + 1; // getMonth() dimulai dari 0
  const hari = date.getDate();

  const blnJarak = bulan + periode;

  let blnAkhir = blnJarak % 12;
  let thnAkhir = tahun + Math.floor(blnJarak / 12);

  if (blnAkhir === 0) {
    blnAkhir = 12;
    thnAkhir--;
  }

  let tglAkhir = hari;

  // Atur batas maksimal hari sesuai bulan
  const maxHari = getMaxHari(blnAkhir, thnAkhir);
  if (tglAkhir > maxHari) {
    tglAkhir = maxHari;
  }

  const hasil = new Date(thnAkhir, blnAkhir - 1, tglAkhir);

  // Format ke YYYY-MM-DD
  const yyyy = hasil.getFullYear();
  const mm = String(hasil.getMonth() + 1).padStart(2, '0');
  const dd = String(hasil.getDate()).padStart(2, '0');

  return `${yyyy}-${mm}-${dd}`;
}

function getMaxHari(bulan: number, tahun: number): number {
  // Februari
  if (bulan === 2) {
    const isKabisat = (tahun % 4 === 0 && tahun % 100 !== 0) || tahun % 400 === 0;
    return isKabisat ? 29 : 28;
  }

  // April, Juni, September, November = 30 hari
  if ([4, 6, 9, 11].includes(bulan)) {
    return 30;
  }

  // Sisanya 31 hari
  return 31;
}

export function formatNumber(val: number | string | undefined): string {
  if (val === undefined || val === null) {
    return "";
  }

  let number = typeof val === "string" ? parseFloat(val) : val;

  if (isNaN(number)) return "";

  // remove sign if negative
  const sign = number < 0 ? -1 : 1;
  number = Math.abs(number);

  // split integer and decimal parts
  const [integerPart, decimalPart] = number.toString().split(".");

  // format integer part with dot separator
  let formatted = "";
  let count = 0;

  for (let i = integerPart.length - 1; i >= 0; i--) {
    formatted = integerPart[i] + formatted;
    count++;
    if (count % 3 === 0 && i !== 0) {
      formatted = "." + formatted;
    }
  }

  // combine decimal part
  if (decimalPart !== undefined) {
    formatted += "," + decimalPart;
  }

  return sign < 0 ? "-" + formatted : formatted;
}

export function yesterday(tgl : string){
    const date = new Date(tgl);
    date.setDate(date.getDate() - 1);
    const yyyy = date.getFullYear();
    const mm = String(date.getMonth() + 1).padStart(2, '0');
    const dd = String(date.getDate()).padStart(2, '0');
    return `${yyyy}-${mm}-${dd}`;
}
