"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.send = send;
exports.toUnixTS = toUnixTS;
exports.parseIntFromString = parseIntFromString;
exports.parseTemplateId = parseTemplateId;
const actions_core_1 = require("@segment/actions-core");
const constants_1 = require("./constants");
async function send(request, payload, settings) {
    validate(payload);
    const groupId = parseIntFromString(payload.group_id);
    const templateId = parseTemplateId(payload.template_id);
    const json = {
        personalizations: [
            {
                to: [{ email: payload.to.email, name: payload.to?.name ?? undefined }],
                cc: payload.cc
                    ?.filter((cc) => cc.email !== undefined)
                    .map((cc) => ({ email: cc.email, name: cc.name ?? undefined })) ?? undefined,
                bcc: payload.bcc
                    ?.filter((bcc) => bcc.email !== undefined)
                    .map((bcc) => ({ email: bcc.email, name: bcc.name ?? undefined })) ?? undefined,
                headers: Object.entries(payload?.headers ?? {}).reduce((acc, [key, value]) => {
                    acc[key] = String(value);
                    return acc;
                }, {}) || undefined,
                dynamic_template_data: payload.dynamic_template_data,
                custom_args: payload?.custom_args && Object.keys(payload.custom_args).length > 0
                    ? Object.fromEntries(Object.entries(payload.custom_args).map(([key, value]) => [key, String(value)]))
                    : undefined,
                send_at: toUnixTS(payload.send_at) ?? undefined
            }
        ],
        from: { email: payload.from.email, name: payload.from?.name ?? undefined },
        reply_to: {
            email: payload?.reply_to?.email ?? payload.from.email,
            name: payload?.reply_to?.name ?? payload.from.name ?? undefined
        },
        template_id: templateId,
        categories: payload.categories,
        asm: typeof groupId === 'number' ? { group_id: groupId } : undefined,
        ip_pool_name: payload.ip_pool_name
    };
    return await request((0, constants_1.sendEmailURL)(settings), {
        method: 'post',
        json
    });
}
function toUnixTS(date) {
    if (!date) {
        return undefined;
    }
    return Math.floor(new Date(date).getTime() / 1000);
}
function parseIntFromString(value) {
    if (!value) {
        return undefined;
    }
    const extractNumber = (regex, value) => {
        const match = regex.exec(value);
        return match ? parseInt(match[1], 10) : null;
    };
    return extractNumber(/^(\d+)/, value) ?? extractNumber(/\[(\d+)\]/, value) ?? undefined;
}
function parseTemplateId(value) {
    if (value.startsWith('d-')) {
        return value.split(' ')[0];
    }
    const regex = /\[(.*?)\]/;
    const match = regex.exec(value);
    return match ? match[1] : null;
}
function validate(payload) {
    if (payload.group_id && typeof parseIntFromString(payload.group_id) !== 'number') {
        throw new actions_core_1.PayloadValidationError('Group ID value must be a numberic (integer) string');
    }
    if (payload.domain && !payload.from.email.endsWith(payload.domain)) {
        throw new actions_core_1.PayloadValidationError('From email must be from the selected domain');
    }
    const templateId = parseTemplateId(payload.template_id);
    if (templateId == null || !templateId.startsWith('d-')) {
        throw new actions_core_1.PayloadValidationError('Template must refer to a Dynamic Template. Dynamic Template IDs start with "d-"');
    }
    if (Object.keys(payload?.headers ?? {}).some((key) => constants_1.RESERVED_HEADERS.includes(key))) {
        throw new actions_core_1.PayloadValidationError(`Headers cannot contain any of the following reserved headers: ${constants_1.RESERVED_HEADERS.join(', ')}`);
    }
    payload?.categories?.forEach((category) => {
        if (category.length > constants_1.MAX_CATEGORY_LENGTH) {
            throw new actions_core_1.PayloadValidationError(`Category with name ${category} exceeds the max length of ${constants_1.MAX_CATEGORY_LENGTH} characters`);
        }
    });
    payload.cc = payload?.cc?.filter((obj) => obj?.email && obj.email.trim() !== '');
    if (Array.isArray(payload.cc) && payload.cc.length === 0) {
        delete payload.cc;
    }
    payload.bcc = payload?.bcc?.filter((obj) => obj?.email && obj.email.trim() !== '');
    if (Array.isArray(payload.bcc) && payload.bcc.length === 0) {
        delete payload.bcc;
    }
    if (payload.send_at) {
        const sendAt = new Date(payload.send_at);
        const now = new Date();
        if (sendAt.getTime() < now.getTime() || sendAt.getTime() > now.getTime() + 72 * 60 * 60 * 1000) {
            throw new actions_core_1.PayloadValidationError('send_at should be less than 72 hours from now');
        }
    }
    if (payload.ip_pool_name &&
        (payload.ip_pool_name.length >= constants_1.MAX_IP_POOL_NAME_LENGTH || payload.ip_pool_name.length <= constants_1.MIN_IP_POOL_NAME_LENGTH)) {
        throw new actions_core_1.PayloadValidationError(`IP Pool Name should at least ${constants_1.MIN_IP_POOL_NAME_LENGTH} characters and at most ${constants_1.MAX_IP_POOL_NAME_LENGTH} characters in length`);
    }
    return;
}
//# sourceMappingURL=utils.js.map