"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_properties_1 = require("../sf-properties");
const sf_operations_1 = __importStar(require("../sf-operations"));
const OBJECT_NAME = 'Contact';
const action = {
    title: 'Contact',
    description: 'Create, update, or upsert contacts 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,
        last_name: {
            label: 'Last Name',
            description: "The contact's last name up to 80 characters. **This is required to create a contact.**",
            type: 'string',
            default: {
                '@if': {
                    exists: { '@path': '$.traits.last_name' },
                    then: { '@path': '$.traits.last_name' },
                    else: { '@path': '$.properties.last_name' }
                }
            },
            depends_on: sf_properties_1.hideIfDeleteOperation
        },
        first_name: {
            label: 'First Name',
            description: "The contact's first name up to 40 characters.",
            type: 'string',
            default: {
                '@if': {
                    exists: { '@path': '$.traits.first_name' },
                    then: { '@path': '$.traits.first_name' },
                    else: { '@path': '$.properties.first_name' }
                }
            },
            depends_on: sf_properties_1.hideIfDeleteOperation
        },
        account_id: {
            label: 'Account ID',
            description: 'The ID of the account that this contact is associated with. This is the Salesforce-generated ID assigned to the account during creation (i.e. 0018c00002CDThnAAH).',
            type: 'string',
            depends_on: sf_properties_1.hideIfDeleteOperation
        },
        email: {
            label: 'Email',
            description: "The contact's email address.",
            type: 'string',
            default: {
                '@if': {
                    exists: { '@path': '$.traits.email' },
                    then: { '@path': '$.traits.email' },
                    else: { '@path': '$.properties.email' }
                }
            },
            depends_on: sf_properties_1.hideIfDeleteOperation
        },
        mailing_city: {
            label: 'Mailing City',
            description: "City for the contact's mailing address.",
            type: 'string',
            default: {
                '@if': {
                    exists: { '@path': '$.traits.address.city' },
                    then: { '@path': '$.traits.address.city' },
                    else: { '@path': '$.properties.address.city' }
                }
            },
            depends_on: sf_properties_1.hideIfDeleteOperation
        },
        mailing_postal_code: {
            label: 'Mailing Postal Code',
            description: "Postal Code for the contact's mailing address.",
            type: 'string',
            default: {
                '@if': {
                    exists: { '@path': '$.traits.address.postal_code' },
                    then: { '@path': '$.traits.address.postal_code' },
                    else: { '@path': '$.properties.address.postal_code' }
                }
            },
            depends_on: sf_properties_1.hideIfDeleteOperation
        },
        mailing_country: {
            label: 'Mailing Country',
            description: "Country for the contact's mailing address.",
            type: 'string',
            default: {
                '@if': {
                    exists: { '@path': '$.traits.address.country' },
                    then: { '@path': '$.traits.address.country' },
                    else: { '@path': '$.properties.address.country' }
                }
            },
            depends_on: sf_properties_1.hideIfDeleteOperation
        },
        mailing_street: {
            label: 'Mailing Street',
            description: "Street number and name for the contact's mailing address.",
            type: 'string',
            default: {
                '@if': {
                    exists: { '@path': '$.traits.address.street' },
                    then: { '@path': '$.traits.address.street' },
                    else: { '@path': '$.properties.address.street' }
                }
            },
            depends_on: sf_properties_1.hideIfDeleteOperation
        },
        mailing_state: {
            label: 'Mailing State',
            description: "State for the contact's mailing address.",
            type: 'string',
            default: {
                '@if': {
                    exists: { '@path': '$.traits.address.state' },
                    then: { '@path': '$.traits.address.state' },
                    else: { '@path': '$.properties.address.state' }
                }
            },
            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.last_name) {
                throw new actions_core_1.IntegrationError('Missing last_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.last_name) {
                throw new actions_core_1.IntegrationError('Missing last_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].last_name) {
                throw new actions_core_1.IntegrationError('Missing last_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