"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const actions_shared_1 = require("@segment/actions-shared");
const action = {
    title: 'Send Email Campaign',
    description: 'Sends a marketing email to a contact.',
    defaultSubscription: 'type = "track" and event = "Send Email Campaign"',
    fields: {
        email: {
            label: 'Email',
            description: "A single email address to send the campaign to. If the contact does not exist in your Dotdigital account, the campaign won't be sent.",
            type: 'string',
            default: {
                '@if': {
                    exists: { '@path': '$.traits.email' },
                    then: { '@path': '$.traits.email' },
                    else: { '@path': '$.properties.email' }
                }
            },
            required: true
        },
        campaignId: {
            label: 'Campaign',
            description: `The campaign to email to a contact.`,
            type: 'number',
            required: true,
            disabledInputMethods: ['literal', 'variable', 'function', 'freeform', 'enrichment'],
            dynamic: true
        },
        sendDate: {
            label: 'Send Date',
            description: `The campaign will be sent immediately if the send date is left blank.`,
            type: 'datetime',
            required: false
        },
        sendTimeOptimised: {
            label: 'Send Time Optimised',
            description: `Send the campaign at the most appropriate time based upon their previous opens.`,
            type: 'boolean',
            required: false,
            default: false
        }
    },
    dynamicFields: {
        campaignId: async (request, { settings }) => {
            return new actions_shared_1.DDCampaignApi(settings.api_host, request).getCampaigns();
        }
    },
    perform: async (request, { settings, payload }) => {
        const contactApi = new actions_shared_1.DDContactApi(settings.api_host, request);
        const contactResponse = await contactApi.getContact('email', payload.email);
        const campaignApi = new actions_shared_1.DDCampaignApi(settings.api_host, request);
        return await campaignApi.sendCampaign(payload.campaignId, contactResponse.contactId, payload.sendDate, payload.sendTimeOptimised);
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map