"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SocialNetworkSmssMethods = void 0;
const abstract_method_1 = require("../abstract.method");
const server_sdk_1 = require("@vonage/server-sdk");
const verifications = [];
class SocialNetworkSmssMethods extends abstract_method_1.AbstractMethods {
    constructor(model) {
        super(model);
        this.api = {
            apiKey: process.env.VONAGE_APIKEY,
            apiSecret: process.env.VONAGE_APISECRET,
        };
        this.vonage = new server_sdk_1.Vonage(this.api);
    }
    async sendVerification(body) {
        try {
            const code = Math.floor(Math.random() * (9999 - 1000 + 1) + 1000) + "";
            verifications[body.number] = code;
            const from = "Cupen Sys";
            const text = `Code: ${code}.`;
            return await this.sendMessages([{ number: body.number, message: text }], from);
        }
        catch (error) {
            console.log(error);
            throw error;
        }
    }
    verify(number, code) {
        return verifications[number] + "" === code + "";
    }
    async sendMessages(list, from = "No reply") {
        return new Promise((resolve, reject) => {
            try {
                const listPromises = list.map((item) => {
                    return this.vonage.sms.send({
                        to: item.number,
                        from,
                        text: item.message,
                    });
                });
                Promise.allSettled(listPromises)
                    .then((responses) => {
                    resolve(responses);
                })
                    .catch((error) => {
                    reject(error);
                });
            }
            catch (error) {
                reject(error);
            }
        });
    }
}
exports.SocialNetworkSmssMethods = SocialNetworkSmssMethods;
