"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const sharedCompanyProperties_1 = require("../sharedCompanyProperties");
const utils_1 = require("../utils");
const companyProperties = sharedCompanyProperties_1.getCompanyProperties();
const action = {
    title: 'Identify Company',
    description: 'Create or update a company in Intercom.',
    defaultSubscription: 'type = "group"',
    platform: 'web',
    fields: {
        company: {
            description: "The user's company.",
            label: 'Company',
            type: 'object',
            required: true,
            properties: companyProperties,
            default: {
                company_id: { '@path': '$.groupId' },
                name: { '@path': '$.traits.name' },
                created_at: {
                    '@if': {
                        exists: { '@path': '$.traits.createdAt' },
                        then: { '@path': '$.traits.createdAt' },
                        else: { '@path': '$.traits.created_at' }
                    }
                },
                plan: { '@path': '$.traits.plan' },
                size: { '@path': '$.traits.size' },
                website: { '@path': '$.traits.website' },
                industry: { '@path': '$.traits.industry' },
                monthly_spend: { '@path': '$.traits.monthly_spend' }
            }
        },
        hide_default_launcher: {
            description: 'Selectively show the chat widget. As per [Intercom docs](https://www.intercom.com/help/en/articles/189-turn-off-show-or-hide-the-intercom-messenger), you want to first hide the Messenger for all users inside the Intercom UI using Messenger settings. Then think about how you want to programmatically decide which users you would like to show the widget to.',
            label: 'Hide Default Launcher',
            type: 'boolean',
            required: false,
            default: {
                '@if': {
                    exists: { '@path': '$.context.Intercom.hideDefaultLauncher' },
                    then: { '@path': '$.context.Intercom.hideDefaultLauncher' },
                    else: { '@path': '$.context.Intercom.hide_default_launcher' }
                }
            }
        }
    },
    perform: (Intercom, event) => {
        const { company_custom_traits, ...rest } = event.payload.company;
        let company = { ...rest };
        if (company?.created_at) {
            company.created_at = utils_1.convertDateToUnix(company.created_at);
        }
        const filteredCustomTraits = utils_1.filterCustomTraits(company_custom_traits);
        company = { ...company, ...filteredCustomTraits };
        const widgetOptions = utils_1.getWidgetOptions(event.payload.hide_default_launcher, Intercom.activator);
        Intercom('update', {
            company,
            ...widgetOptions
        });
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map