import React from "react";
import useTickets from "./useTickets";

const useTicketsListener = (userId?: string) => {
  const { tickets, reload } = useTickets();

  React.useEffect(() => {
    const intervalId = setInterval(() => {
      reload();
    }, 60 * 1000);

    return () => clearInterval(intervalId);
  }, [reload]);

  if (userId) {
    const assignedTickets = tickets.filter(
      (ticket) => ticket.assignedTo === userId && ticket.status === "submitted"
    );

    return {
      assignedTickets,
      totalAssignedTickets: assignedTickets.length,
    };
  }

  return {};
};

export default useTicketsListener;
