"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 = 'Account';
const action = {
    title: 'Account',
    description: 'Create, update, or upsert accounts in Salesforce.',
    defaultSubscription: 'type = "group"',
    fields: {
        operation: sf_properties_1.operation,
        enable_batching: sf_properties_1.enable_batching,
        batch_size: sf_properties_1.batch_size,
        recordMatcherOperator: sf_properties_1.recordMatcherOperator,
        traits: sf_properties_1.traits,
        bulkUpsertExternalId: sf_properties_1.bulkUpsertExternalId,
        bulkUpdateRecordId: sf_properties_1.bulkUpdateRecordId,
        name: {
            label: 'Name',
            description: 'Name of the account. **This is required to create an account.**',
            type: 'string',
            default: {
                '@path': '$.traits.name'
            },
            depends_on: sf_properties_1.hideIfDeleteOperation
        },
        account_number: {
            label: 'Account Number',
            description: 'Account number assigned to the account. This is not the unique, Salesforce-generated ID assigned during creation.',
            type: 'string',
            default: {
                '@path': '$.groupId'
            },
            depends_on: sf_properties_1.hideIfDeleteOperation
        },
        number_of_employees: {
            label: 'Number of employees',
            description: 'Number of employees working at the company represented by the account.',
            type: 'integer',
            default: {
                '@if': {
                    exists: { '@path': '$.traits.employees' },
                    then: { '@path': '$.traits.employees' },
                    else: { '@path': '$.properties.employees' }
                }
            },
            depends_on: sf_properties_1.hideIfDeleteOperation
        },
        billing_city: {
            label: 'Billing City',
            description: 'City for the billing address of the account.',
            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
        },
        billing_postal_code: {
            label: 'Billing Postal Code',
            description: 'Postal code for the billing address of the account.',
            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
        },
        billing_country: {
            label: 'Billing Country',
            description: 'Country for the billing address of the account.',
            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
        },
        billing_street: {
            label: 'Billing Street',
            description: 'Street address for the billing address of the account.',
            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
        },
        billing_state: {
            label: 'Billing State',
            description: 'State for the billing address of the account.',
            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
        },
        shipping_city: {
            label: 'Shipping City',
            description: 'City for the shipping address of the account.',
            type: 'string',
            depends_on: sf_properties_1.hideIfDeleteOperation
        },
        shipping_postal_code: {
            label: 'Shipping Postal Code',
            description: 'Postal code for the shipping address of the account.',
            type: 'string',
            depends_on: sf_properties_1.hideIfDeleteOperation
        },
        shipping_country: {
            label: 'Shipping Country',
            description: 'Country for the shipping address of the account.',
            type: 'string',
            depends_on: sf_properties_1.hideIfDeleteOperation
        },
        shipping_street: {
            label: 'Shipping Street',
            description: 'Street address for the shipping address of the account.',
            type: 'string',
            depends_on: sf_properties_1.hideIfDeleteOperation
        },
        shipping_state: {
            label: 'Shipping State',
            description: 'State for the shipping address of the account.',
            type: 'string',
            depends_on: sf_properties_1.hideIfDeleteOperation
        },
        phone: {
            label: 'Phone',
            description: 'Phone number for the account.',
            type: 'string',
            default: {
                '@if': {
                    exists: { '@path': '$.traits.phone' },
                    then: { '@path': '$.traits.phone' },
                    else: { '@path': '$.properties.phone' }
                }
            },
            depends_on: sf_properties_1.hideIfDeleteOperation
        },
        description: {
            label: 'Description',
            description: 'Text description of the account.',
            type: 'string',
            default: {
                '@if': {
                    exists: { '@path': '$.traits.description' },
                    then: { '@path': '$.traits.description' },
                    else: { '@path': '$.properties.description' }
                }
            },
            depends_on: sf_properties_1.hideIfDeleteOperation
        },
        website: {
            label: 'Website',
            description: 'The website of the account.',
            type: 'string',
            default: {
                '@if': {
                    exists: { '@path': '$.traits.website' },
                    then: { '@path': '$.traits.website' },
                    else: { '@path': '$.properties.website' }
                }
            },
            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.name) {
                throw new actions_core_1.IntegrationError('Missing 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.name) {
                throw new actions_core_1.IntegrationError('Missing 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].name) {
                throw new actions_core_1.IntegrationError('Missing 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