"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const actions_core_1 = require("@segment/actions-core");
const sfmc_properties_1 = require("../sfmc-properties");
const sfmc_operations_1 = require("../sfmc-operations");
const action = {
    title: 'Send Contact to Data Extension (V2)',
    defaultSubscription: 'type = "identify"',
    description: 'Upsert contact data as rows into a data extension in Salesforce Marketing Cloud.',
    fields: {
        keys: {
            ...sfmc_properties_1.keys,
            dynamic: true,
            properties: {
                contactKey: {
                    label: 'Contact Key',
                    description: 'The unique identifier that you assign to a contact. Contact Key must be a Primary Key in the data extension that contains contact information.',
                    type: 'string',
                    required: true
                }
            },
            default: {
                contactKey: { '@path': '$.userId' }
            }
        },
        values: { ...sfmc_properties_1.values_contactFields, dynamic: true },
        enable_batching: sfmc_properties_1.enable_batching,
        batch_size: sfmc_properties_1.batch_size
    },
    dynamicFields: {
        keys: {
            __keys__: async (request, { settings, payload }) => {
                const dataExtensionId = payload.onMappingSave?.outputs.id || payload.retlOnMappingSave?.outputs?.id || '';
                return await (0, sfmc_operations_1.getDataExtensionFields)(request, settings.subdomain, settings, dataExtensionId, true);
            }
        },
        values: {
            __keys__: async (request, { settings, payload }) => {
                const dataExtensionId = payload.onMappingSave?.outputs?.id || payload.retlOnMappingSave?.outputs?.id || '';
                return await (0, sfmc_operations_1.getDataExtensionFields)(request, settings.subdomain, settings, dataExtensionId, false);
            }
        }
    },
    hooks: {
        retlOnMappingSave: {
            ...sfmc_properties_1.dataExtensionHook
        },
        onMappingSave: {
            ...sfmc_properties_1.dataExtensionHook
        }
    },
    perform: async (request, { settings, payload, hookOutputs }) => {
        const dataExtensionId = hookOutputs?.onMappingSave?.outputs?.id || hookOutputs?.retlOnMappingSave?.outputs?.id;
        if (!dataExtensionId) {
            throw new actions_core_1.IntegrationError('No Data Extension Connected', 'INVALID_CONFIGURATION', 400);
        }
        return (0, sfmc_operations_1.upsertRowsV2)(request, settings.subdomain, [payload], dataExtensionId);
    },
    performBatch: async (request, { settings, payload, hookOutputs }) => {
        const dataExtensionId = hookOutputs?.onMappingSave?.outputs?.id || hookOutputs?.retlOnMappingSave?.outputs?.id;
        if (!dataExtensionId) {
            throw new actions_core_1.IntegrationError('No Data Extension Connected', 'INVALID_CONFIGURATION', 400);
        }
        return (0, sfmc_operations_1.executeUpsertWithMultiStatus)(request, settings.subdomain, payload, dataExtensionId);
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map