"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PhoneMessageSender = void 0;
const TwilioMessageSender_1 = require("./TwilioMessageSender");
const actions_shared_1 = require("@segment/actions-shared");
class PhoneMessageSender extends TwilioMessageSender_1.TwilioMessageSender {
    constructor() {
        super(...arguments);
        this.EXTERNAL_ID_KEY = 'phone';
        this.DEFAULT_HOSTNAME = 'api.twilio.com';
        this.DEFAULT_CONNECTION_OVERRIDES = 'rp=all&rc=5';
    }
    get twilioHostname() {
        return this.settings.twilioHostname ?? this.DEFAULT_HOSTNAME;
    }
    get twilioToken() {
        return Buffer.from(`${this.settings.twilioApiKeySID}:${this.settings.twilioApiKeySecret}`).toString('base64');
    }
    async sendToRecepient(recepient) {
        const phone = recepient.id;
        const op = this.currentOperation;
        this.currentOperation?.onFinally.push(() => {
            const error = op.error;
            if (error)
                op.tags.push('not_sent_reason:error_operation_' + actions_shared_1.OperationDecorator.getOperationName(error.trackedContext || op));
        });
        const body = await this.getBody(phone);
        const webhookUrlWithParams = this.getWebhookUrlWithParams(this.EXTERNAL_ID_KEY, phone, this.DEFAULT_CONNECTION_OVERRIDES);
        const tags = {
            audience_id: this.payload.customArgs && this.payload.customArgs['audience_id'],
            correlation_id: this.payload.customArgs && this.payload.customArgs['correlation_id'],
            journey_name: this.payload.customArgs && this.payload.customArgs['journey_name'],
            step_name: this.payload.customArgs && this.payload.customArgs['step_name'],
            campaign_name: this.payload.customArgs && this.payload.customArgs['campaign_name'],
            campaign_key: this.payload.customArgs && this.payload.customArgs['campaign_key'],
            user_id: this.payload.customArgs && this.payload.customArgs['user_id'],
            message_id: this.payload.customArgs && this.payload.customArgs['message_id'],
            external_id_type: recepient.type,
            external_id_value: phone
        };
        body.append('Tags', JSON.stringify(tags));
        if (webhookUrlWithParams) {
            body.append('StatusCallback', webhookUrlWithParams);
        }
        this.statsSet('message_body_size', body?.toString().length);
        const response = await this.request(`https://${this.twilioHostname}/2010-04-01/Accounts/${this.settings.twilioAccountSID}/Messages.json`, {
            method: 'POST',
            headers: {
                authorization: `Basic ${this.twilioToken}`
            },
            body
        });
        this.logDetails['twilio-request-id'] = response.headers?.get('twilio-request-id');
        return response;
    }
    isSupportedExternalId(externalId) {
        return externalId.type === 'phone' && externalId.channelType?.toLowerCase() === this.getChannelType();
    }
    getRecepients() {
        if (this.payload.toNumber)
            return [
                {
                    id: this.payload.toNumber
                }
            ];
        const res = super.getRecepients();
        return res.length > 0 ? [res[0]] : res;
    }
}
exports.PhoneMessageSender = PhoneMessageSender;
//# sourceMappingURL=PhoneMessageSender.js.map