import { type PayrollReceipt } from './types';

export const getReceiptKey = (r: PayrollReceipt) =>
  `${r.idEmpresa}-${r.idTrabajador}-${r.ejercicio}-${r.idMes}-${r.idNomina}`;

export const formatMonth = (receipt: PayrollReceipt) =>
  `${receipt.dMes.charAt(0)}${receipt.dMes.slice(1).toLowerCase()} ${receipt.ejercicio}`;

export const formatDevengo = (receipt: PayrollReceipt) =>
  receipt.fDevengoInicio
    ? `${receipt.fDevengoInicio} - ${receipt.fDevengo}`
    : receipt.fDevengo;

export const buildTitle = (r: PayrollReceipt) =>
  `${r.dNomina} — ${formatMonth(r)}`;

export const triggerDownload = (url: string, title: string) => {
  const a = document.createElement('a');
  a.href = url;
  a.download = `${title}.pdf`;
  document.body.appendChild(a);
  a.click();
  document.body.removeChild(a);
};
