"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SocialNetworkEmailsController = void 0;
const social_network_email_method_1 = require("../../methods/social-network/social-network-email.method");
const social_network_email_model_1 = require("../../models/social-network/social-network-email.model");
const abstract_controller_1 = require("../abstract.controller");
class SocialNetworkEmailsController extends abstract_controller_1.AbstractController {
    constructor() {
        super(social_network_email_model_1.SocialNetworkEmailsModel);
        this.methods = new social_network_email_method_1.SocialNetworkEmailsMethods(social_network_email_model_1.SocialNetworkEmailsModel);
    }
    async sendEmail(req, res) {
        try {
            const matchedData = this.params.matchedData(req);
            if (Object.keys(matchedData).length > 0) {
                const senders = [];
                matchedData.list.forEach((item) => {
                    const emails = item.email.split(",");
                    senders.push(...emails.map((email) => {
                        var _a, _b, _c;
                        return this.methods.send({ subject: item.subject }, (_a = item.html) !== null && _a !== void 0 ? _a : "", (_b = item.data) !== null && _b !== void 0 ? _b : {}, (_c = email.trim()) !== null && _c !== void 0 ? _c : "");
                    }));
                });
                Promise.allSettled(senders)
                    .then((response) => {
                    res.send({ message: "SUCCESS", response });
                })
                    .catch((error) => {
                    this.params.handleHttpError(res, error);
                });
            }
            else {
                this.params.handleHttpError(res, "MISSING_DATA");
            }
        }
        catch (e) {
            this.params.handleHttpError(res, e);
        }
    }
    async sendVerification(req, res) {
        try {
            const matchedData = this.params.matchedData(req);
            this.methods
                .sendVerification(matchedData)
                .then((response) => {
                res.send(response);
            })
                .catch((error) => {
                this.params.handleHttpError(res, error);
            });
        }
        catch (e) {
            console.log(e);
            this.params.handleHttpError(res, e);
        }
    }
    async verify(req, res) {
        try {
            const matchedData = this.params.matchedData(req);
            res.send({
                verify: this.methods.verify(matchedData.email, matchedData.code),
            });
        }
        catch (e) {
            console.log(e);
            this.params.handleHttpError(res, e);
        }
    }
}
exports.SocialNetworkEmailsController = SocialNetworkEmailsController;
