"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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SmsMessageSender = void 0;
const actions_core_1 = require("@segment/actions-core");
const utils_1 = require("../utils");
const actions_shared_1 = require("@segment/actions-shared");
class SmsMessageSender extends utils_1.PhoneMessageSender {
    constructor() {
        super(...arguments);
        this.supportedTemplateTypes = ['twilio/text', 'twilio/media'];
    }
    async getBody(phone) {
        if (!this.payload.body && !this.payload.contentSid) {
            throw new actions_core_1.PayloadValidationError('Unable to process sms, no body provided and no content sid provided');
        }
        let traits;
        if (this.payload.traitEnrichment) {
            traits = this.payload?.traits ? this.payload?.traits : {};
        }
        else {
            traits = await this.getProfileTraits();
        }
        const profile = {
            user_id: this.payload.userId ?? undefined,
            phone,
            traits
        };
        let parsedBody;
        let parsedMedia = [];
        if (this.payload.contentSid) {
            const data = await this.getContentTemplateTypes();
            const parsed = await this.parseContent(data, profile);
            parsedBody = parsed.body;
            parsedMedia = parsed.media;
        }
        else {
            const parsed = await this.parseContent({ body: this.payload.body ?? '', media: this.payload.media ?? [] }, profile);
            parsedBody = parsed.body;
            parsedMedia = parsed.media;
        }
        const body = new URLSearchParams({
            Body: parsedBody,
            From: this.payload.from,
            To: phone,
            ShortenUrls: 'true'
        });
        parsedMedia?.forEach((media) => {
            body.append('MediaUrl', media);
        });
        return body;
    }
    getChannelType() {
        return 'sms';
    }
    isSupportedExternalId(externalId) {
        if (externalId.type !== 'phone') {
            return false;
        }
        return !externalId.channelType || externalId.channelType.toLowerCase() === this.getChannelType();
    }
}
exports.SmsMessageSender = SmsMessageSender;
__decorate([
    (0, actions_shared_1.track)()
], SmsMessageSender.prototype, "getBody", null);
//# sourceMappingURL=SmsMessageSender.js.map