"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 Event asynchronously to Data Extension',
    description: `Upsert event records asynchronously as rows into a data extension in Salesforce Marketing Cloud.
  
    Note that Segment cannot provide real-time delivery confirmation or error tracking for events sent through this action- That will come in near future`,
    fields: {
        keys: { ...sfmc_properties_1.keys, required: true, dynamic: true },
        values: { ...sfmc_properties_1.values_dataExtensionFields, dynamic: true },
        enable_batching: {
            label: 'Batch data to SFMC',
            description: 'If true, data is batched before sending to the SFMC Data Extension.',
            type: 'boolean',
            default: true,
            unsafe_hidden: true
        },
        batch_bytes: {
            label: 'Batch Bytes',
            description: 'The maximum size of a batch in bytes.',
            type: 'number',
            required: false,
            default: 5000000,
            unsafe_hidden: 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,
            minimum: 1000,
            default: 28000,
            maximum: 30000,
            unsafe_hidden: true
        }
    },
    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.asyncUpsertRowsV2)(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.asyncUpsertRowsV2)(request, settings.subdomain, payload, dataExtensionId);
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map