import { getIO } from "../libs/socket";
import Ticket from "../models/Ticket";
import sendWhatsAppMessageOfficialAPI from "./sendWhatsAppMessageOfficialAPI"

function sendWhatsAppMessageSocket(
  ticket: Ticket,
  body: string,
  quotedMsgSerializedId?: string | undefined,
  number?: string
) {
  const { phoneNumberId } = ticket;

  if (phoneNumberId) {
    sendWhatsAppMessageOfficialAPI(ticket, body, quotedMsgSerializedId);
    return;
  }

  const io = getIO();

  io.to(`session_${ticket.whatsappId.toString()}`).emit("send_message", {
    action: "create",
    msg: {
      number: number
        ? number
        : `${ticket.contact.number}@${ticket.isGroup ? "g" : "c"}.us`,
      body: body,
      quotedMessageId: quotedMsgSerializedId,
      linkPreview: false
    }
  });
}

export default sendWhatsAppMessageSocket;
