import Whatsapp from "../models/Whatsapp";
import ShowQueuesByUser from "../services/UserServices/ShowQueuesByUser";
import ShowWhatsAppService from "../services/WhatsappService/ShowWhatsAppService";

async function whatsappQueueMatchingUserQueue(
  userId: number,
  whatsapp: Whatsapp,
  userProfile: string = "user"
) {
  const userQueues = await ShowQueuesByUser({
    profile: userProfile,
    userId: userId
  });

  if (!userQueues || (userQueues && userQueues.length == 0)) return;

  // console.log('-----> userQueues: ', userQueues);

  let whats: any = await ShowWhatsAppService(whatsapp.id);

  if (!whats.queues || (whats.queues && whats.queues.length == 0)) return;

  const whatsappQueues = whats.queues.map((e: any) => e.dataValues.name);

  // console.log('-----> whatsappQueues: ', whatsappQueues);

  const matchingQueue = userQueues.find(queue =>
    whatsappQueues.includes(queue.name)
  );

  return matchingQueue;
}

export default whatsappQueueMatchingUserQueue;
