"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const sf_properties_1 = require("../sf-properties");
const sf_operations_1 = __importStar(require("../sf-operations"));
const actions_core_1 = require("@segment/actions-core");
const OPERATIONS_WITH_CUSTOM_FIELDS = ['create', 'update', 'upsert'];
const action = {
    title: 'Custom Object',
    description: 'Create, update, or upsert records in any custom or standard object in Salesforce.',
    fields: {
        operation: sf_properties_1.operation,
        recordMatcherOperator: sf_properties_1.recordMatcherOperator,
        enable_batching: sf_properties_1.enable_batching,
        batch_size: sf_properties_1.batch_size,
        traits: sf_properties_1.traits,
        bulkUpsertExternalId: sf_properties_1.bulkUpsertExternalId,
        bulkUpdateRecordId: sf_properties_1.bulkUpdateRecordId,
        customObjectName: {
            label: 'Salesforce Object',
            description: 'The API name of the Salesforce object that records will be added or updated within. This can be a standard or custom object. Custom objects must be predefined in your Salesforce account and should end with "__c".',
            type: 'string',
            required: true,
            dynamic: true
        },
        customFields: sf_properties_1.customFields
    },
    dynamicFields: {
        customObjectName: async (request, data) => {
            const sf = new sf_operations_1.default(data.settings.instanceUrl, await (0, sf_operations_1.generateSalesforceRequest)(data.settings, request));
            return sf.customObjectName();
        }
    },
    perform: async (request, { settings, payload }) => {
        if (OPERATIONS_WITH_CUSTOM_FIELDS.includes(payload.operation) && !payload.customFields) {
            throw new actions_core_1.PayloadValidationError('Custom fields are required for this operation.');
        }
        const sf = new sf_operations_1.default(settings.instanceUrl, await (0, sf_operations_1.generateSalesforceRequest)(settings, request));
        if (payload.operation === 'create') {
            return await sf.createRecord(payload, payload.customObjectName);
        }
        (0, sf_properties_1.validateLookup)(payload);
        if (payload.operation === 'update') {
            return await sf.updateRecord(payload, payload.customObjectName);
        }
        if (payload.operation === 'upsert') {
            return await sf.upsertRecord(payload, payload.customObjectName);
        }
        if (payload.operation === 'delete') {
            return await sf.deleteRecord(payload, payload.customObjectName);
        }
    },
    performBatch: async (request, { settings, payload, features, statsContext, logger }) => {
        if (OPERATIONS_WITH_CUSTOM_FIELDS.includes(payload[0].operation) && !payload[0].customFields) {
            throw new actions_core_1.PayloadValidationError('Custom fields are required for this operation.');
        }
        const sf = new sf_operations_1.default(settings.instanceUrl, await (0, sf_operations_1.generateSalesforceRequest)(settings, request));
        let shouldShowAdvancedLogging = false;
        if (features && features['salesforce-advanced-logging']) {
            shouldShowAdvancedLogging = true;
        }
        return sf.bulkHandler(payload, payload[0].customObjectName, {
            shouldLog: shouldShowAdvancedLogging,
            stats: statsContext,
            logger
        });
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map