"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const fields_1 = require("../fields");
const utils_1 = require("../utils");
const sendRequest = async (request, payloads, settings) => {
    const host = utils_1.hosts[settings.region];
    const requestBody = payloads.map((payload) => {
        return {
            ...payload.additional_traits,
            user_identifiers: payload.user_identifiers,
            title: payload.title,
            name: payload.name,
            first_name: payload.firstname,
            last_name: payload.lastname,
            age: payload.age,
            ...(0, utils_1.getDOBDetails)(payload.DOB),
            gender: payload.gender,
            phone: payload.phone,
            address: payload.address,
            company: payload.company,
            image_url: payload.avatar
        };
    });
    return request(`${host}/batch_upsert_contact`, {
        method: 'post',
        json: requestBody
    });
};
const action = {
    title: 'Upsert Contact',
    description: 'Send user profile data from Segment to Optimizely Data Platform. Creates or updates a user profile in Optimzely Data Platform',
    defaultSubscription: 'type = "identify"',
    fields: {
        user_identifiers: fields_1.user_identifiers,
        company: {
            label: 'Company Name',
            type: 'string',
            description: 'The name of the company associated with the Contact',
            default: { '@path': '$.traits.company' }
        },
        title: {
            label: 'Title',
            type: 'string',
            description: "The Contact's Title",
            default: { '@path': '$.traits.title' }
        },
        name: {
            label: 'Name',
            type: 'string',
            description: "Contact's full name",
            default: { '@path': '$.traits.name' }
        },
        firstname: {
            label: 'First Name',
            type: 'string',
            description: "Contact's first name",
            default: { '@path': '$.traits.first_name' }
        },
        lastname: {
            label: 'Last Name',
            type: 'string',
            description: "Contact's last name",
            default: { '@path': '$.traits.last_name' }
        },
        gender: {
            label: 'Gender',
            type: 'string',
            description: "Contact's gender",
            default: { '@path': '$.traits.gender' }
        },
        DOB: {
            label: 'Birthday',
            type: 'datetime',
            description: "Contact's birthday. The format should be datetime",
            default: { '@path': '$.traits.birthday' }
        },
        phone: {
            label: 'Phone',
            type: 'string',
            description: "Contact's phone number.",
            default: { '@path': '$.traits.phone' }
        },
        age: {
            label: 'Age',
            type: 'number',
            description: "Contact's age.",
            default: { '@path': '$.traits.age' }
        },
        address: {
            label: 'Address',
            type: 'object',
            description: 'Address details object',
            properties: {
                street: {
                    label: 'Street',
                    type: 'string',
                    description: "The user's street."
                },
                city: {
                    label: 'City',
                    type: 'string',
                    description: "The user's city."
                },
                state: {
                    label: 'State',
                    type: 'string',
                    description: "The user's state or region."
                },
                zip: {
                    label: 'Zip code',
                    type: 'string',
                    description: 'Zip or postal code'
                },
                country: {
                    label: 'Country',
                    type: 'string',
                    description: "The user's country."
                }
            },
            default: {
                street: { '@path': '$.traits.address.street' },
                city: { '@path': '$.traits.address.city' },
                state: { '@path': '$.traits.address.state' },
                zip: { '@path': '$.traits.address.postalCode' },
                country: { '@path': '$.traits.address.country' }
            }
        },
        avatar: {
            label: 'avatar',
            type: 'string',
            description: "The user's avatar image URL.",
            default: { '@path': '$.traits.avatar' }
        },
        additional_traits: {
            label: 'Addition User Traits',
            type: 'object',
            defaultObjectUI: 'keyvalue',
            description: 'Additional user profile details'
        },
        enable_batching: fields_1.enable_batching,
        batch_size: fields_1.batch_size
    },
    perform: (request, { payload, settings }) => {
        return sendRequest(request, [payload], settings);
    },
    performBatch: (request, { payload, settings }) => {
        return sendRequest(request, payload, settings);
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map