"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const fields_1 = require("./fields");
const constants_1 = require("./constants");
const hashing_utils_1 = require("../../../lib/hashing-utils");
const action = {
    title: 'Standard Fields Event',
    description: 'Sync an event containing standard fields only to Epsilon',
    defaultSubscription: 'type = "track"',
    fields: {
        ...fields_1.standardFields,
        dtmc_department: fields_1.dtmc_department,
        dtmc_category: fields_1.dtmc_category,
        dtmc_sub_category: fields_1.dtmc_sub_category,
        dtmc_product_id: fields_1.dtmc_product_id,
        dtmc_brand: fields_1.dtmc_brand,
        dtmc_upc: fields_1.dtmc_upc,
        dtmc_mpn: fields_1.dtmc_mpn,
        dtmc_transaction_id: fields_1.dtmc_transaction_id,
        dtm_conv_val: fields_1.dtm_conv_val,
        dtm_items: fields_1.dtm_items,
        dtm_conv_curr: fields_1.dtm_conv_curr,
        dtmc_conv_type: fields_1.dtmc_conv_type,
        dtmc_conv_store_location: fields_1.dtmc_conv_store_location,
        customEventName: fields_1.customEventName,
        customProperties: fields_1.customProperties
    },
    perform: (request, { payload, settings }) => {
        const { id, appId, dtm_event, version, dtm_fid, dtm_promo_id, deviceType, dtmc_department, dtmc_category, dtmc_sub_category, dtmc_product_id, dtmc_brand, dtmc_upc, dtmc_mpn, dtmc_transaction_id, dtm_conv_val, dtm_items, dtm_conv_curr, dtmc_conv_type, dtmc_conv_store_location, customEventName, customProperties, identifiers: { deviceID, advertisingId, dtm_user_agent, dtm_user_ip, dtm_email_hash, dtm_mobile_hash, dtm_user_id } = {} } = payload;
        const { dtm_cid, dtm_cmagic } = settings;
        let eventData = {
            ...customProperties,
            dtmc_tms: 9,
            dtm_cid: dtm_cid.trim(),
            dtm_cmagic,
            dtm_fid,
            dtm_promo_id,
            idfa: deviceType === 'ios' ? advertisingId : undefined,
            google_play_id: deviceType === 'android' ? advertisingId : undefined,
            idfv: deviceType === 'ios' ? deviceID : undefined,
            google_app_set_id: deviceType === 'android' ? deviceID : undefined,
            dtm_user_agent,
            dtm_user_ip,
            dtm_email_hash: dtm_email_hash
                ? (0, hashing_utils_1.processHashing)(dtm_email_hash, 'sha256', 'hex', (value) => value?.trim().toLowerCase())
                : undefined,
            dtm_mobile_hash: dtm_mobile_hash
                ? (0, hashing_utils_1.processHashing)(dtm_mobile_hash, 'sha256', 'hex', (value) => value?.trim().toLowerCase())
                : undefined,
            dtm_user_id,
        };
        switch (dtm_event) {
            case 'department': {
                const updated = {
                    ...eventData,
                    dtmc_department
                };
                eventData = updated;
                break;
            }
            case 'category': {
                const updated = {
                    ...eventData,
                    dtmc_department,
                    dtmc_category
                };
                eventData = updated;
                break;
            }
            case 'sub_category': {
                const updated = {
                    ...eventData,
                    dtmc_department,
                    dtmc_category,
                    dtmc_sub_category
                };
                eventData = updated;
                break;
            }
            case 'product':
            case 'addFavorites':
            case 'addSavedList':
            case 'cart': {
                const updated = {
                    ...eventData,
                    dtmc_department,
                    dtmc_category,
                    dtmc_sub_category,
                    dtmc_product_id,
                    dtmc_brand,
                    dtmc_upc,
                    dtmc_mpn
                };
                eventData = updated;
                break;
            }
            case 'conversion': {
                const updated = {
                    ...eventData,
                    dtmc_transaction_id,
                    dtm_conv_val: dtm_conv_val,
                    dtm_items: dtm_items,
                    dtm_conv_curr: dtm_conv_curr,
                    dtmc_conv_type: dtmc_conv_type,
                    dtmc_conv_store_location: dtmc_conv_store_location
                };
                eventData = updated;
                break;
            }
            case 'custom': {
                const updated = {
                    ...eventData,
                    dtmc_department,
                    dtmc_category,
                    dtmc_sub_category,
                    dtmc_product_id,
                    dtmc_brand,
                    dtmc_upc,
                    dtmc_mpn,
                    dtmc_transaction_id,
                    dtm_conv_val,
                    dtm_items,
                    dtm_conv_curr,
                    dtmc_conv_type,
                    dtmc_conv_store_location
                };
                eventData = updated;
            }
        }
        const json = {
            id,
            jsonrpc: '2.0',
            method: 'syncEvent',
            params: {
                appId,
                dtm_event: dtm_event === 'custom' ? customEventName : dtm_event,
                version,
                eventData
            }
        };
        return request(constants_1.URL, {
            method: 'post',
            json
        });
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map