import { TicketDetailsInterface } from '../interface/ticketDetails';
import { ticketStatuses } from '../config/ticketStatus';
import { safeDivision } from './safeDivision';

//Format response for bot researcher landing page and compliance landing page open and closed section
export function formatMrDashboardResponse(
  tickets: TicketDetailsInterface[],
  filterStatus: string,
) {
  for (const ticket of tickets) {
    const completionPercentage = safeDivision(
      ticket.completedTaskPerTicket,
      ticket.totalTaskPerTicket,
    );
    ticket.completionPercentage = completionPercentage;
  }
  const openTicketsFilter = [ticketStatuses.open, ticketStatuses.assigned];
  const closedTicketsFilter = [ticketStatuses.completed];

  const openTickets = tickets.filter((ticket) =>
    openTicketsFilter.includes(ticket.ticketStatus),
  );

  const closedTickets = tickets.filter((ticket) =>
    closedTicketsFilter.includes(ticket.ticketStatus),
  );

  return filterStatus === 'Open'
    ? {
        closedTicketCounts: closedTickets.length,
        openTicketCounts: openTickets.length,
        tickets: openTickets,
      }
    : {
        openTicketCounts: openTickets.length,
        closedTicketCounts: closedTickets.length,
        tickets: closedTickets,
      };
}
