"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SocialNetworkWhatsappsMethods = void 0;
const mime = __importStar(require("mime-types"));
const social_network_connect_model_1 = require("../../models/social-network/social-network-connect.model");
const wa_1 = require("../../utils/plugins/bot-whatsapp/wa");
const whatsapp_plugin_1 = require("../../utils/plugins/whatsapp.plugin");
const abstract_method_1 = require("../abstract.method");
class SocialNetworkWhatsappsMethods extends abstract_method_1.AbstractMethods {
    constructor(model) {
        super(model);
    }
    createSession(sessionId, res) {
        (0, wa_1.createSession)({ sessionId, res });
        return;
    }
    removeSession(sessionId) {
        (0, wa_1.deleteSession)(sessionId);
    }
    async loadSession(sessionId) {
        return (0, wa_1.getSession)(sessionId);
    }
    sendMessages(sessionId, list) {
        return new Promise(async (resolve, reject) => {
            try {
                const listPromises = list.map((item) => {
                    const provider = new whatsapp_plugin_1.WebWhatsappProvider(sessionId);
                    return provider.sendMessage(item.number, { text: item.message }, item.options);
                });
                const responses = await Promise.allSettled(listPromises);
                resolve(responses);
            }
            catch (error) {
                reject(error);
            }
        });
    }
    sendPhotos(sessionId, list) {
        return new Promise(async (resolve, reject) => {
            try {
                const listPromises = list.map((item) => {
                    var _a, _b;
                    const provider = new whatsapp_plugin_1.WebWhatsappProvider(sessionId);
                    if ((_a = item.options) === null || _a === void 0 ? void 0 : _a.media) {
                        const mimeType = mime.lookup((_b = item.options) === null || _b === void 0 ? void 0 : _b.media);
                        if (mimeType.includes("image")) {
                            return provider.sendMessage(item.number, { text: item.message }, item.options);
                        }
                        else {
                            return new Promise((rs, rj) => {
                                rs(false);
                            });
                        }
                    }
                    else {
                        return new Promise((rs, rj) => {
                            rs(false);
                        });
                    }
                });
                const responses = await Promise.allSettled(listPromises);
                resolve(responses);
            }
            catch (error) {
                reject(error);
            }
        });
    }
    sendVideos(sessionId, list) {
        return new Promise(async (resolve, reject) => {
            try {
                const listPromises = list.map((item) => {
                    var _a, _b;
                    const provider = new whatsapp_plugin_1.WebWhatsappProvider(sessionId);
                    if ((_a = item.options) === null || _a === void 0 ? void 0 : _a.media) {
                        const mimeType = mime.lookup((_b = item.options) === null || _b === void 0 ? void 0 : _b.media);
                        if (mimeType.includes("video")) {
                            return provider.sendMessage(item.number, { text: item.message }, item.options);
                        }
                        else {
                            return new Promise((rs, rj) => {
                                rs(false);
                            });
                        }
                    }
                    else {
                        return new Promise((rs, rj) => {
                            rs(false);
                        });
                    }
                });
                const responses = await Promise.allSettled(listPromises);
                resolve(responses);
            }
            catch (error) {
                reject(error);
            }
        });
    }
    sendAudios(sessionId, list) {
        return new Promise(async (resolve, reject) => {
            try {
                const listPromises = list.map((item) => {
                    var _a, _b;
                    const provider = new whatsapp_plugin_1.WebWhatsappProvider(sessionId);
                    if ((_a = item.options) === null || _a === void 0 ? void 0 : _a.media) {
                        const mimeType = mime.lookup((_b = item.options) === null || _b === void 0 ? void 0 : _b.media);
                        if (mimeType.includes("audio")) {
                            return provider.sendMessage(item.number, { text: item.message }, item.options);
                        }
                        else {
                            return new Promise((rs, rj) => {
                                rs(false);
                            });
                        }
                    }
                    else {
                        return new Promise((rs, rj) => {
                            rs(false);
                        });
                    }
                });
                const responses = await Promise.allSettled(listPromises);
                resolve(responses);
            }
            catch (error) {
                reject(error);
            }
        });
    }
    refreshSession(status, req, matchedData) {
        try {
            const query = {
                store_id: req.headers.store,
                user_id: req.user_id,
                username: matchedData.username,
                provider_id: matchedData.provider_id,
            };
            const update = {
                $set: {
                    store_id: req.headers.store,
                    user_id: req.user_id,
                    username: matchedData.username,
                    provider_id: matchedData.provider_id,
                    status,
                },
            };
            const options = { upsert: true };
            return social_network_connect_model_1.SocialNetworkConnectsModel.updateOne(query, update, options);
        }
        catch (error) {
            throw error;
        }
    }
}
exports.SocialNetworkWhatsappsMethods = SocialNetworkWhatsappsMethods;
