"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SocialNetworkEmailsMethods = void 0;
const email_1 = require("../../config/email");
const markers_1 = require("../../utils/markers");
const html_renders_1 = require("../../utils/renders/html.renders");
const abstract_method_1 = require("../abstract.method");
const verifications = [];
class SocialNetworkEmailsMethods extends abstract_method_1.AbstractMethods {
    constructor(model) {
        super(model);
    }
    async render(text, data) {
        return await (0, html_renders_1.htmlRenders)(text, data);
    }
    send(options, text, data = {}, email) {
        return new Promise((resolve, reject) => {
            let markers = [];
            markers = Object.keys(data).map((d) => {
                return markers_1.EmailMarkers.hasOwnProperty(d)
                    ? markers_1.EmailMarkers[d]({ email })
                    : new Promise((r) => r(data[d]));
            });
            console.log(markers);
            if (markers.length) {
                Promise.allSettled(markers)
                    .then((r) => {
                    let dataParsed = r.reduce((prev, next, i) => {
                        return {
                            ...prev,
                            [Object.keys(data)[i]]: next.value
                                ? next.value
                                : data[Object.keys(data)[i]],
                        };
                    }, {});
                    this.render(text, dataParsed)
                        .then((html) => {
                        const mailOptions = {
                            from: "Cupen systems <no-reply@cupensystems.com>",
                            to: email,
                            subject: options.subject,
                            html,
                        };
                        email_1.transporterSMTP.sendMail(mailOptions, (error, info) => {
                            if (error) {
                                reject("ERROR: " + JSON.stringify(error));
                                console.log(error);
                            }
                            else {
                                resolve(info);
                            }
                        });
                    })
                        .catch((error) => {
                        reject("ERROR: " + JSON.stringify(error));
                    });
                })
                    .catch((error) => {
                    reject("ERROR: " + JSON.stringify(error));
                });
            }
            else {
                console.log(text);
                this.render(text, {})
                    .then((html) => {
                    const mailOptions = {
                        from: "Cupen systems <no-reply@cupensystems.com>",
                        to: email,
                        subject: options.subject,
                        html,
                    };
                    email_1.transporterSMTP.sendMail(mailOptions, (err, info) => {
                        if (err) {
                            reject("ERROR: " + err);
                            console.log(err);
                        }
                        else {
                            resolve(info);
                        }
                    });
                })
                    .catch((error) => {
                    reject("ERROR: " + JSON.stringify(error));
                });
            }
        });
    }
    async sendVerification(body) {
        var _a;
        try {
            const code = Math.floor(Math.random() * (9999 - 1000 + 1) + 1000) + "";
            verifications[body.email] = code;
            const from = "Cupen Sys";
            const html = `Code: ${code}.`;
            return await this.send({ subject: "Verify" }, html, {}, (_a = body.email.trim()) !== null && _a !== void 0 ? _a : "");
        }
        catch (error) {
            console.log(error);
            throw error;
        }
    }
    verify(number, code) {
        return verifications[number] + "" === code + "";
    }
}
exports.SocialNetworkEmailsMethods = SocialNetworkEmailsMethods;
