"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 actions_core_1 = require("@segment/actions-core");
const sf_operations_1 = __importStar(require("../sf-operations"));
const sf_properties_1 = require("../sf-properties");
const OBJECT_NAME = 'Opportunity';
const action = {
    title: 'Opportunity',
    description: 'Create, update, or upsert opportunities 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,
        close_date: {
            label: 'Close Date',
            description: 'Date when the opportunity is expected to close. Use yyyy-MM-dd format. **This is required to create an opportunity.**',
            type: 'string',
            depends_on: sf_properties_1.hideIfDeleteOperation
        },
        name: {
            label: 'Name',
            description: 'A name for the opportunity. **This is required to create an opportunity.**',
            type: 'string',
            depends_on: sf_properties_1.hideIfDeleteOperation
        },
        stage_name: {
            label: 'Stage Name',
            description: 'Current stage of the opportunity. **This is required to create an opportunity.**',
            type: 'string',
            depends_on: sf_properties_1.hideIfDeleteOperation
        },
        amount: {
            label: 'Amount',
            description: 'Estimated total sale amount.',
            type: 'string',
            depends_on: sf_properties_1.hideIfDeleteOperation
        },
        description: {
            label: 'Description',
            description: 'A text description of the opportunity.',
            type: 'string',
            depends_on: sf_properties_1.hideIfDeleteOperation
        },
        customFields: sf_properties_1.customFields
    },
    perform: async (request, { settings, payload }) => {
        const sf = new sf_operations_1.default(settings.instanceUrl, await (0, sf_operations_1.generateSalesforceRequest)(settings, request));
        if (payload.operation === 'create') {
            if (!payload.close_date || !payload.name || !payload.stage_name) {
                throw new actions_core_1.IntegrationError('Missing close_date, name or stage_name value', 'Misconfigured required field', 400);
            }
            return await sf.createRecord(payload, OBJECT_NAME);
        }
        (0, sf_properties_1.validateLookup)(payload);
        if (payload.operation === 'update') {
            return await sf.updateRecord(payload, OBJECT_NAME);
        }
        if (payload.operation === 'upsert') {
            if (!payload.close_date || !payload.name || !payload.stage_name) {
                throw new actions_core_1.IntegrationError('Missing close_date, name or stage_name value', 'Misconfigured required field', 400);
            }
            return await sf.upsertRecord(payload, OBJECT_NAME);
        }
        if (payload.operation === 'delete') {
            return await sf.deleteRecord(payload, OBJECT_NAME);
        }
    },
    performBatch: async (request, { settings, payload, features, statsContext, logger }) => {
        const sf = new sf_operations_1.default(settings.instanceUrl, await (0, sf_operations_1.generateSalesforceRequest)(settings, request));
        if (payload[0].operation === 'upsert') {
            if (!payload[0].close_date || !payload[0].name || !payload[0].stage_name) {
                throw new actions_core_1.IntegrationError('Missing close_date, name or stage_name value', 'Misconfigured required field', 400);
            }
        }
        let shouldShowAdvancedLogging = false;
        if (features && features['salesforce-advanced-logging']) {
            shouldShowAdvancedLogging = true;
        }
        return sf.bulkHandler(payload, OBJECT_NAME, { shouldLog: shouldShowAdvancedLogging, stats: statsContext, logger });
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map