"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const actions_core_1 = require("@segment/actions-core");
const dynamic_functions_1 = require("../dynamic-functions");
const constants_1 = require("../constants");
const action = {
    title: 'Update Company',
    description: 'Update all Leads withing a Company in Qualified.',
    defaultSubscription: 'type = "track" and event = "Leads In Company Updated"',
    fields: {
        domain: {
            label: 'Domain',
            description: 'The domain of the company to update.',
            type: 'string',
            required: true,
            default: { '@path': '$.traits.domain' }
        },
        string_fields: {
            label: 'String Fields',
            description: 'String, text or picklist field values to set on all Leads associated with the Company.',
            type: 'object',
            defaultObjectUI: 'keyvalue',
            dynamic: true
        },
        boolean_fields: {
            label: 'Boolean Fields',
            description: 'boolean / checkbox field values to set on all Leads associated with the Company.',
            type: 'object',
            defaultObjectUI: 'keyvalue',
            dynamic: true
        },
        number_fields: {
            label: 'Number Fields',
            description: 'Numeric / decimal field values to set on all Leads associated with the Company.',
            type: 'object',
            defaultObjectUI: 'keyvalue',
            dynamic: true
        }
    },
    dynamicFields: {
        string_fields: {
            __keys__: async (request) => {
                return await (0, dynamic_functions_1.dynamicReadFields)(request, ['string', 'picklist', 'text'], true);
            }
        },
        boolean_fields: {
            __keys__: async (request) => {
                return await (0, dynamic_functions_1.dynamicReadFields)(request, ['boolean'], true);
            }
        },
        number_fields: {
            __keys__: async (request) => {
                return await (0, dynamic_functions_1.dynamicReadFields)(request, ['decimal'], true);
            }
        }
    },
    perform: async (request, { payload }) => {
        const { domain, string_fields, boolean_fields, number_fields } = payload;
        Object.entries(string_fields || {}).forEach(([key, value]) => {
            if (typeof value !== 'string') {
                throw new actions_core_1.PayloadValidationError(`Invalid field value for: ${key}. Should be a string.`);
            }
        });
        Object.entries(boolean_fields || {}).forEach(([key, value]) => {
            if (typeof value !== 'boolean') {
                throw new actions_core_1.PayloadValidationError(`Invalid field value for: ${key}. Should be a boolean.`);
            }
        });
        Object.entries(number_fields || {}).forEach(([key, value]) => {
            if (typeof value !== 'number') {
                throw new actions_core_1.PayloadValidationError(`Invalid field value for: ${key}. Should be a number.`);
            }
        });
        const json = {
            domain,
            fields: {
                ...string_fields,
                ...boolean_fields,
                ...number_fields
            }
        };
        return request(constants_1.base_url + 'companies', {
            method: 'post',
            json
        });
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map