"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ortto_client_1 = __importDefault(require("../ortto-client"));
const common_fields_1 = require("../common-fields");
const action = {
    title: 'Upsert Contact',
    description: 'Create or update a Contact in Ortto',
    defaultSubscription: 'type = "identify"',
    fields: {
        timestamp: {
            label: 'Timestamp',
            description: 'Event timestamp (ISO 8601)',
            type: 'string',
            readOnly: true,
            format: 'date-time',
            required: false,
            unsafe_hidden: true,
            default: {
                '@path': '$.timestamp'
            }
        },
        message_id: {
            label: 'Message ID',
            description: 'Message ID',
            type: 'string',
            readOnly: true,
            required: false,
            unsafe_hidden: true,
            default: {
                '@path': '$.messageId'
            }
        },
        user_id: common_fields_1.commonFields.user_id,
        anonymous_id: common_fields_1.commonFields.anonymous_id,
        enable_batching: common_fields_1.commonFields.enable_batching,
        ip: common_fields_1.commonFields.ipV4,
        location: common_fields_1.commonFields.location,
        traits: common_fields_1.commonFields.traits,
        audience_update_mode: common_fields_1.commonFields.audience_update_mode,
        batch_size: common_fields_1.commonFields.batch_size
    },
    hooks: {
        onMappingSave: {
            label: 'Associate Audience',
            description: 'Link the Contact to an Audience in Ortto. If the Audience does not already exist, it will be created in Ortto.',
            inputFields: {
                audience_name: {
                    type: 'string',
                    label: 'Audience Name',
                    description: 'The name of the Ortto Audience to link the Contact to.'
                }
            },
            outputTypes: {
                audience_id: {
                    type: 'string',
                    label: 'ID',
                    description: 'The ID of the Ortto Audience Contacts will be linked to.',
                    required: false
                },
                audience_name: {
                    type: 'string',
                    label: 'Name',
                    description: 'The name of the Ortto Audience contacts will be linkted to.',
                    required: false
                }
            },
            performHook: async (request, { settings, hookInputs }) => {
                if (!hookInputs.audience_name) {
                    return {};
                }
                try {
                    const client = new ortto_client_1.default(request);
                    const audience = await client.createAudience(settings, hookInputs.audience_name);
                    return {
                        successMessage: `Connected to Audience '${audience.name}' (id: ${audience.id}) successfully.`,
                        savedData: {
                            audience_id: audience.id,
                            audience_name: audience.name
                        }
                    };
                }
                catch (err) {
                    return {
                        error: {
                            message: err.message ?? 'Unknown Error',
                            code: err.status?.toString() ?? 'Unknown Error'
                        }
                    };
                }
            }
        }
    },
    perform: async (request, { settings, payload, hookOutputs }) => {
        const client = new ortto_client_1.default(request);
        return await client.upsertContacts(settings, [payload], hookOutputs?.onMappingSave?.outputs?.audience_id ?? '');
    },
    performBatch: async (request, { settings, payload, hookOutputs }) => {
        const client = new ortto_client_1.default(request);
        return await client.upsertContacts(settings, payload, hookOutputs?.onMappingSave?.outputs?.audience_id ?? '');
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map