"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var Msg91WhatsappService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Msg91WhatsappService = void 0;
const axios_1 = require("@nestjs/axios");
const common_1 = require("@nestjs/common");
const publisher_factory_service_1 = require("../queues/publisher-factory.service");
const whatsapp_provider_decorator_1 = require("../../decorators/whatsapp-provider.decorator");
const setting_service_1 = require("../setting.service");
var Msg91WhatsappParameterHeaderType;
(function (Msg91WhatsappParameterHeaderType) {
    Msg91WhatsappParameterHeaderType[Msg91WhatsappParameterHeaderType["image"] = 0] = "image";
    Msg91WhatsappParameterHeaderType[Msg91WhatsappParameterHeaderType["text"] = 1] = "text";
})(Msg91WhatsappParameterHeaderType || (Msg91WhatsappParameterHeaderType = {}));
let Msg91WhatsappService = Msg91WhatsappService_1 = class Msg91WhatsappService {
    constructor(publisherFactory, httpService, settingService) {
        this.publisherFactory = publisherFactory;
        this.httpService = httpService;
        this.settingService = settingService;
        this.logger = new common_1.Logger(Msg91WhatsappService_1.name);
    }
    async sendWhatsAppMessage(to, templateId, parameters, parentEntity, parentEntityId) {
        const message = {
            payload: {
                to,
                templateId,
                ...parameters,
            },
            parentEntity,
            parentEntityId,
        };
        return this.sendWhatsAppMessageAsynchronously(message);
    }
    async sendWhatsAppMessageAsynchronously(message) {
        const { to, templateId } = message.payload;
        this.logger.debug(`Queueing WhatsApp message to ${to} with template ${templateId}`);
        return this.publisherFactory.publish(message, 'Msg91WhatsappQueuePublisher');
    }
    async sendWhatsAppMessageSynchronously(message) {
        const body = await this.createWhatsappRequest(message);
        const headers = { authkey: this.settingService.getConfigValue("msg91WhatsappApiKey") };
        await this.httpService.axiosRef.post(`${this.settingService.getConfigValue("msg91WhatsappUrl")}`, body, { headers });
        this.logger.debug(`Sending Whatsapp message for CP registration with body ${JSON.stringify(body)} and url ${this.settingService.getConfigValue("msg91WhatsappUrl")}`);
    }
    async createWhatsappRequest(message) {
        const { to, templateId, ...parameters } = message.payload;
        const whatsappToAndComponents = this.createWhatsappToAndComponents(to, parameters);
        const whatsappLanguage = {
            code: 'en',
            policy: 'deterministic',
        };
        const whatsappTemplate = this.createWhatsappTemplate(templateId, whatsappToAndComponents, whatsappLanguage);
        const whatsappPayload = {
            messaging_product: 'whatsapp',
            type: 'template',
            template: whatsappTemplate,
        };
        return {
            integrated_number: this.settingService.getConfigValue("msg91WhatsappIntegratedNumber"),
            content_type: 'template',
            payload: whatsappPayload,
        };
    }
    createWhatsappToAndComponents(to, parameters) {
        return {
            to: [to],
            components: this.createWhatsappComponents(parameters),
        };
    }
    createWhatsappComponents(parameters) {
        const components = {};
        if (parameters.header) {
            components['header_1'] = {
                type: parameters.header.type,
                value: parameters.header.value,
            };
        }
        if (parameters.body && parameters.body.length > 0) {
            parameters.body.forEach((elem, index) => {
                components[`body_${index + 1}`] = {
                    type: 'text',
                    value: elem,
                };
            });
        }
        return components;
    }
    createWhatsappTemplate(templateName, toAndComponents, language) {
        return {
            name: templateName,
            language: language,
            namespace: null,
            to_and_components: [toAndComponents],
        };
    }
};
exports.Msg91WhatsappService = Msg91WhatsappService;
exports.Msg91WhatsappService = Msg91WhatsappService = Msg91WhatsappService_1 = __decorate([
    (0, common_1.Injectable)(),
    (0, whatsapp_provider_decorator_1.WhatsAppProvider)(),
    __metadata("design:paramtypes", [publisher_factory_service_1.PublisherFactory,
        axios_1.HttpService,
        setting_service_1.SettingService])
], Msg91WhatsappService);
//# sourceMappingURL=Msg91WhatsappService.js.map