"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const functions_1 = require("./functions");
const action = {
    title: 'Sync Audience',
    description: 'Sync Segment Engage Audience to a Dotdigital List',
    defaultSubscription: 'type = "identify" or type = "track"',
    fields: {
        segment_computation_action: {
            label: 'Segment Computation Action',
            description: "Hidden field used to verify that the payload is generated by an Audience. Payloads not containing computation_class = 'audience' or 'journey_step' will be dropped before the perform() fuction call.",
            type: 'string',
            required: true,
            unsafe_hidden: true,
            default: {
                '@path': '$.context.personas.computation_class'
            },
            choices: [
                { label: 'audience', value: 'audience' },
                { label: 'journey_step', value: 'journey_step' }
            ]
        },
        computation_key: {
            label: 'Audience Computation Key',
            description: "Segment's friendly name for the Audience",
            type: 'string',
            required: true,
            dynamic: true,
            default: {
                '@path': '$.context.personas.computation_key'
            }
        },
        external_audience_id: {
            type: 'string',
            label: 'Audience ID',
            description: 'Unique Audience Identifier returned by the createAudience() function call.',
            required: true,
            default: {
                '@path': '$.context.personas.external_audience_id'
            }
        },
        emailIdentifier: {
            label: 'Email address',
            description: "The Contact's email address.",
            type: 'string',
            format: 'email',
            default: {
                '@if': {
                    exists: { '@path': '$.traits.email' },
                    then: { '@path': '$.traits.email' },
                    else: { '@path': '$.properties.email' }
                }
            }
        },
        mobileNumberIdentifier: {
            label: 'Mobile Number',
            description: "The Contact's mobile number.",
            type: 'string',
            default: {
                '@if': {
                    exists: { '@path': '$.traits.phone' },
                    then: { '@path': '$.traits.phone' },
                    else: { '@path': '$.properties.phone' }
                }
            }
        },
        traits_or_props: {
            label: 'Traits or properties object',
            description: 'A computed object for track and identify events. This field should not need to be edited.',
            type: 'object',
            required: true,
            unsafe_hidden: true,
            default: {
                '@if': {
                    exists: { '@path': '$.properties' },
                    then: { '@path': '$.properties' },
                    else: { '@path': '$.traits' }
                }
            }
        },
        dataFields: {
            label: 'Data Fields',
            description: `An object containing key/value pairs for data fields assigned to this Contact. Custom Data Fields must already be defined in Dotdigital.`,
            type: 'object',
            required: false,
            defaultObjectUI: 'keyvalue:only',
            additionalProperties: false,
            dynamic: true
        }
    },
    dynamicFields: {
        dataFields: {
            __keys__: async (request, { settings }) => {
                const { api_host } = settings;
                try {
                    const url = `${api_host}/v2/data-fields`;
                    const response = await request(url, {
                        method: 'GET',
                        skipResponseCloning: true
                    });
                    return {
                        choices: [
                            ...response.data
                                .map((item) => {
                                return {
                                    label: `${item.name} - ${item.type}`,
                                    value: item.name
                                };
                            })
                        ]
                    };
                }
                catch (err) {
                    return {
                        choices: [],
                        error: {
                            message: err?.response?.data?.message ?? 'Unknown error: dynamicReadProperties',
                            code: err?.response?.status ?? '500'
                        }
                    };
                }
            }
        }
    },
    perform: async (request, { payload, settings }) => {
        return await (0, functions_1.send)(request, payload, settings);
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map