"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const actions_core_1 = require("@segment/actions-core");
const action = {
    title: 'Send',
    description: 'Send an HTTP request.',
    fields: {
        url: {
            label: 'URL',
            description: 'URL to deliver data to.',
            type: 'string',
            required: true,
            format: 'uri'
        },
        method: {
            label: 'Method',
            description: 'HTTP method to use.',
            type: 'string',
            choices: [
                { label: 'POST', value: 'POST' },
                { label: 'PUT', value: 'PUT' },
                { label: 'PATCH', value: 'PATCH' }
            ],
            default: 'POST',
            required: true
        },
        batch_size: {
            label: 'Batch Size',
            description: 'Maximum number of events to include in each batch. Actual batch sizes may be lower.',
            type: 'number',
            required: false,
            default: 0
        },
        headers: {
            label: 'Headers',
            description: 'HTTP headers to send with each request. Only ASCII characters are supported.',
            type: 'object',
            defaultObjectUI: 'keyvalue:only'
        },
        data: {
            label: 'Data',
            description: 'Payload to deliver to webhook URL (JSON-encoded).',
            type: 'object',
            default: { '@path': '$.' }
        }
    },
    perform: (request, { payload }) => {
        try {
            let body;
            let contentType = 'application/json';
            if (payload.headers) {
                contentType = payload.headers['Content-Type'] || payload.headers['content-type'];
            }
            if (payload.data) {
                body = encodeBody(payload.data, contentType);
            }
            return request(payload.url, {
                method: payload.method,
                headers: payload.headers,
                ...body
            });
        }
        catch (error) {
            if (error instanceof TypeError)
                throw new actions_core_1.PayloadValidationError(error.message);
            throw error;
        }
    },
    performBatch: (request, { payload }) => {
        try {
            const { url, method, headers } = payload[0];
            return request(url, {
                method: method,
                headers: headers,
                json: payload.map(({ data }) => {
                    let contentType = 'application/json';
                    if (headers) {
                        contentType = headers['Content-Type'] || headers['content-type'];
                    }
                    if (data)
                        return encodeBody(data, contentType);
                    return data;
                })
            });
        }
        catch (error) {
            if (error instanceof TypeError)
                throw new actions_core_1.PayloadValidationError(error.message);
            throw error;
        }
    }
};
const encodeBody = (payload, contentType) => {
    if (contentType === 'application/json') {
        return { json: payload };
    }
    else if (contentType === 'application/x-www-form-urlencoded') {
        const formUrlEncoded = new URLSearchParams(payload).toString();
        return { body: formUrlEncoded };
    }
    else {
        return { json: payload };
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map