"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.performSnapCAPIv3 = void 0;
const utils_1 = require("./utils");
const hashing_utils_1 = require("../../../lib/hashing-utils");
const CURRENCY_ISO_4217_CODES = new Set([
    'USD',
    'AED',
    'AUD',
    'BGN',
    'BRL',
    'CAD',
    'CHF',
    'CLP',
    'CNY',
    'COP',
    'CZK',
    'DKK',
    'EGP',
    'EUR',
    'GBP',
    'GIP',
    'HKD',
    'HRK',
    'HUF',
    'IDR',
    'ILS',
    'INR',
    'JPY',
    'KRW',
    'KWD',
    'KZT',
    'LBP',
    'MXN',
    'MYR',
    'NGN',
    'NOK',
    'NZD',
    'PEN',
    'PHP',
    'PKR',
    'PLN',
    'QAR',
    'RON',
    'RUB',
    'SAR',
    'SEK',
    'SGD',
    'THB',
    'TRY',
    'TWD',
    'TZS',
    'UAH',
    'VND',
    'ZAR',
    'ALL',
    'BHD',
    'DZD',
    'GHS',
    'IQD',
    'ISK',
    'JOD',
    'KES',
    'MAD',
    'OMR',
    'XOF'
]);
const US_STATE_CODES = new Map([
    ['arizona', 'az'],
    ['alabama', 'al'],
    ['alaska', 'ak'],
    ['arkansas', 'ar'],
    ['california', 'ca'],
    ['colorado', 'co'],
    ['connecticut', 'ct'],
    ['delaware', 'de'],
    ['florida', 'fl'],
    ['georgia', 'ga'],
    ['hawaii', 'hi'],
    ['idaho', 'id'],
    ['illinois', 'il'],
    ['indiana', 'in'],
    ['iowa', 'ia'],
    ['kansas', 'ks'],
    ['kentucky', 'ky'],
    ['louisiana', 'la'],
    ['maine', 'me'],
    ['maryland', 'md'],
    ['massachusetts', 'ma'],
    ['michigan', 'mi'],
    ['minnesota', 'mn'],
    ['mississippi', 'ms'],
    ['missouri', 'mo'],
    ['montana', 'mt'],
    ['nebraska', 'ne'],
    ['nevada', 'nv'],
    ['newhampshire', 'nh'],
    ['newjersey', 'nj'],
    ['newmexico', 'nm'],
    ['newyork', 'ny'],
    ['northcarolina', 'nc'],
    ['northdakota', 'nd'],
    ['ohio', 'oh'],
    ['oklahoma', 'ok'],
    ['oregon', 'or'],
    ['pennsylvania', 'pa'],
    ['rhodeisland', 'ri'],
    ['southcarolina', 'sc'],
    ['southdakota', 'sd'],
    ['tennessee', 'tn'],
    ['texas', 'tx'],
    ['utah', 'ut'],
    ['vermont', 'vt'],
    ['virginia', 'va'],
    ['washington', 'wa'],
    ['westvirginia', 'wv'],
    ['wisconsin', 'wi'],
    ['wyoming', 'wy']
]);
const COUNTRY_CODES = new Map([
    ['afghanistan', 'af'],
    ['alandislands', 'ax'],
    ['albania', 'al'],
    ['algeria', 'dz'],
    ['americansamoa', 'as'],
    ['andorra', 'ad'],
    ['angola', 'ao'],
    ['anguilla', 'ai'],
    ['antarctica', 'aq'],
    ['antiguaandbarbuda', 'ag'],
    ['argentina', 'ar'],
    ['armenia', 'am'],
    ['aruba', 'aw'],
    ['australia', 'au'],
    ['austria', 'at'],
    ['azerbaijan', 'az'],
    ['bahamas', 'bs'],
    ['bahrain', 'bh'],
    ['bangladesh', 'bd'],
    ['barbados', 'bb'],
    ['belarus', 'by'],
    ['belgium', 'be'],
    ['belize', 'bz'],
    ['benin', 'bj'],
    ['bermuda', 'bm'],
    ['bhutan', 'bt'],
    ['bolivia', 'bo'],
    ['bosniaandherzegovina', 'ba'],
    ['botswana', 'bw'],
    ['bouvetisland', 'bv'],
    ['brazil', 'br'],
    ['britishindianoceanterritory', 'io'],
    ['bruneidarussalam', 'bn'],
    ['bulgaria', 'bg'],
    ['burkinafaso', 'bf'],
    ['burundi', 'bi'],
    ['cambodia', 'kh'],
    ['cameroon', 'cm'],
    ['canada', 'ca'],
    ['capeverde', 'cv'],
    ['caymanislands', 'ky'],
    ['centralafricanrepublic', 'cf'],
    ['chad', 'td'],
    ['chile', 'cl'],
    ['china', 'cn'],
    ['christmasisland', 'cx'],
    ['cocos(keeling)islands', 'cc'],
    ['colombia', 'co'],
    ['comoros', 'km'],
    ['congo', 'cg'],
    ['congo,democraticrepublic', 'cd'],
    ['cookislands', 'ck'],
    ['costarica', 'cr'],
    ["coted'ivoire", 'ci'],
    ['croatia', 'hr'],
    ['cuba', 'cu'],
    ['cyprus', 'cy'],
    ['czechrepublic', 'cz'],
    ['denmark', 'dk'],
    ['djibouti', 'dj'],
    ['dominica', 'dm'],
    ['dominicanrepublic', 'do'],
    ['ecuador', 'ec'],
    ['egypt', 'eg'],
    ['elsalvador', 'sv'],
    ['equatorialguinea', 'gq'],
    ['eritrea', 'er'],
    ['estonia', 'ee'],
    ['ethiopia', 'et'],
    ['falklandislands(malvinas)', 'fk'],
    ['faroeislands', 'fo'],
    ['fiji', 'fj'],
    ['finland', 'fi'],
    ['france', 'fr'],
    ['frenchguiana', 'gf'],
    ['frenchpolynesia', 'pf'],
    ['frenchsouthernterritories', 'tf'],
    ['gabon', 'ga'],
    ['gambia', 'gm'],
    ['georgia', 'ge'],
    ['germany', 'de'],
    ['ghana', 'gh'],
    ['gibraltar', 'gi'],
    ['greece', 'gr'],
    ['greenland', 'gl'],
    ['grenada', 'gd'],
    ['guadeloupe', 'gp'],
    ['guam', 'gu'],
    ['guatemala', 'gt'],
    ['guernsey', 'gg'],
    ['guinea', 'gn'],
    ['guinea-bissau', 'gw'],
    ['guyana', 'gy'],
    ['haiti', 'ht'],
    ['heardisland&mcdonaldislands', 'hm'],
    ['holysee(vaticancitystate)', 'va'],
    ['honduras', 'hn'],
    ['hongkong', 'hk'],
    ['hungary', 'hu'],
    ['iceland', 'is'],
    ['india', 'in'],
    ['indonesia', 'id'],
    ['iran,islamicrepublicof', 'ir'],
    ['iraq', 'iq'],
    ['ireland', 'ie'],
    ['isleofman', 'im'],
    ['israel', 'il'],
    ['italy', 'it'],
    ['jamaica', 'jm'],
    ['japan', 'jp'],
    ['jersey', 'je'],
    ['jordan', 'jo'],
    ['kazakhstan', 'kz'],
    ['kenya', 'ke'],
    ['kiribati', 'ki'],
    ['korea', 'kr'],
    ['kuwait', 'kw'],
    ['kyrgyzstan', 'kg'],
    ["laopeople'sdemocraticrepublic", 'la'],
    ['latvia', 'lv'],
    ['lebanon', 'lb'],
    ['lesotho', 'ls'],
    ['liberia', 'lr'],
    ['libyanarabjamahiriya', 'ly'],
    ['liechtenstein', 'li'],
    ['lithuania', 'lt'],
    ['luxembourg', 'lu'],
    ['macao', 'mo'],
    ['macedonia', 'mk'],
    ['madagascar', 'mg'],
    ['malawi', 'mw'],
    ['malaysia', 'my'],
    ['maldives', 'mv'],
    ['mali', 'ml'],
    ['malta', 'mt'],
    ['marshallislands', 'mh'],
    ['martinique', 'mq'],
    ['mauritania', 'mr'],
    ['mauritius', 'mu'],
    ['mayotte', 'yt'],
    ['mexico', 'mx'],
    ['micronesia,federatedstatesof', 'fm'],
    ['moldova', 'md'],
    ['monaco', 'mc'],
    ['mongolia', 'mn'],
    ['montenegro', 'me'],
    ['montserrat', 'ms'],
    ['morocco', 'ma'],
    ['mozambique', 'mz'],
    ['myanmar', 'mm'],
    ['namibia', 'na'],
    ['nauru', 'nr'],
    ['nepal', 'np'],
    ['netherlands', 'nl'],
    ['netherlandsantilles', 'an'],
    ['newcaledonia', 'nc'],
    ['newzealand', 'nz'],
    ['nicaragua', 'ni'],
    ['niger', 'ne'],
    ['nigeria', 'ng'],
    ['niue', 'nu'],
    ['norfolkisland', 'nf'],
    ['northernmarianaislands', 'mp'],
    ['norway', 'no'],
    ['oman', 'om'],
    ['pakistan', 'pk'],
    ['palau', 'pw'],
    ['palestinianterritory,occupied', 'ps'],
    ['panama', 'pa'],
    ['papuanewguinea', 'pg'],
    ['paraguay', 'py'],
    ['peru', 'pe'],
    ['philippines', 'ph'],
    ['pitcairn', 'pn'],
    ['poland', 'pl'],
    ['portugal', 'pt'],
    ['puertorico', 'pr'],
    ['qatar', 'qa'],
    ['reunion', 're'],
    ['romania', 'ro'],
    ['russianfederation', 'ru'],
    ['rwanda', 'rw'],
    ['saintbarthelemy', 'bl'],
    ['sainthelena', 'sh'],
    ['saintkittsandnevis', 'kn'],
    ['saintlucia', 'lc'],
    ['saintmartin', 'mf'],
    ['saintpierreandmiquelon', 'pm'],
    ['saintvincentandgrenadines', 'vc'],
    ['samoa', 'ws'],
    ['sanmarino', 'sm'],
    ['saotomeandprincipe', 'st'],
    ['saudiarabia', 'sa'],
    ['senegal', 'sn'],
    ['serbia', 'rs'],
    ['seychelles', 'sc'],
    ['sierraleone', 'sl'],
    ['singapore', 'sg'],
    ['slovakia', 'sk'],
    ['slovenia', 'si'],
    ['solomonislands', 'sb'],
    ['somalia', 'so'],
    ['southafrica', 'za'],
    ['southgeorgiaandsandwichisl.', 'gs'],
    ['spain', 'es'],
    ['srilanka', 'lk'],
    ['sudan', 'sd'],
    ['suriname', 'sr'],
    ['svalbardandjanmayen', 'sj'],
    ['swaziland', 'sz'],
    ['sweden', 'se'],
    ['switzerland', 'ch'],
    ['syrianarabrepublic', 'sy'],
    ['taiwan', 'tw'],
    ['tajikistan', 'tj'],
    ['tanzania', 'tz'],
    ['thailand', 'th'],
    ['timor-leste', 'tl'],
    ['togo', 'tg'],
    ['tokelau', 'tk'],
    ['tonga', 'to'],
    ['trinidadandtobago', 'tt'],
    ['tunisia', 'tn'],
    ['turkey', 'tr'],
    ['turkmenistan', 'tm'],
    ['turksandcaicosislands', 'tc'],
    ['tuvalu', 'tv'],
    ['uganda', 'ug'],
    ['ukraine', 'ua'],
    ['unitedarabemirates', 'ae'],
    ['unitedkingdom', 'gb'],
    ['unitedstates', 'us'],
    ['unitedstatesoutlyingislands', 'um'],
    ['uruguay', 'uy'],
    ['uzbekistan', 'uz'],
    ['vanuatu', 'vu'],
    ['venezuela', 've'],
    ['vietnam', 'vn'],
    ['virginislands,british', 'vg'],
    ['virginislands,u.s.', 'vi'],
    ['wallisandfutuna', 'wf'],
    ['westernsahara', 'eh'],
    ['yemen', 'ye'],
    ['zambia', 'zm'],
    ['zimbabwe', 'zw']
]);
const iosAppIDRegex = new RegExp('^[0-9]+$');
const buildAppData = (payload, settings) => {
    const { app_data } = payload;
    const app_id = (0, utils_1.emptyStringToUndefined)(settings.app_id);
    const advertiser_tracking_enabled = app_data?.advertiser_tracking_enabled ? 1 : 0;
    const appDataApplicationTrackingEnabled = app_data?.application_tracking_enabled ? 1 : 0;
    const application_tracking_enabled = app_data != null ? appDataApplicationTrackingEnabled : undefined;
    const appDataExtInfoVersion = app_data?.version;
    const appIDExtInfoVersion = iosAppIDRegex.test(app_id ?? '') ? 'i2' : 'a2';
    const extInfoVersion = appDataExtInfoVersion ?? appIDExtInfoVersion;
    const extinfo = [
        extInfoVersion,
        app_data?.packageName ?? '',
        app_data?.shortVersion ?? '',
        app_data?.longVersion ?? '',
        app_data?.osVersion ?? payload.os_version ?? '',
        app_data?.deviceName ?? payload.device_model ?? '',
        app_data?.locale ?? '',
        app_data?.timezone ?? '',
        app_data?.carrier ?? '',
        app_data?.width ?? '',
        app_data?.height ?? '',
        app_data?.density ?? '',
        app_data?.cpuCores ?? '',
        app_data?.storageSize ?? '',
        app_data?.freeStorage ?? '',
        app_data?.deviceTimezone ?? ''
    ];
    return {
        app_id,
        advertiser_tracking_enabled,
        application_tracking_enabled,
        extinfo
    };
};
const buildUserData = (payload) => {
    const { user_data } = payload;
    const normalizedValue = (value) => value?.replace(/\s/g, '').toLowerCase();
    const email = (0, utils_1.smartHash)(user_data?.email ?? payload.email, normalizedValue);
    const normalizedPhoneNumber = (value) => value?.replace(/\D|^0+/g, '');
    const phone_number = (0, utils_1.smartHash)(user_data?.phone ?? payload.phone_number, normalizedPhoneNumber);
    const madid = (user_data?.madid ?? payload.mobile_ad_id)?.toLowerCase();
    const normalizedGender = (value) => {
        const normalizedValue = value?.replace(/\s/g, '').toLowerCase();
        return normalizedValue === 'male' ? 'm' : normalizedValue === 'female' ? 'f' : normalizedValue;
    };
    const hashedGender = (0, utils_1.smartHash)(user_data?.gender, normalizedGender);
    const hashedLastName = (0, utils_1.smartHash)(user_data?.lastName, normalizedValue);
    const hashedFirstName = (0, utils_1.smartHash)(user_data?.firstName, normalizedValue);
    const client_ip_address = user_data?.client_ip_address ?? payload.ip_address;
    const client_user_agent = user_data?.client_user_agent ?? payload.user_agent;
    const hashedCity = (0, utils_1.smartHash)(user_data?.city, normalizedValue);
    const normalizedState = (value) => {
        const normalizedValue = value?.replace(/\s/g, '').toLowerCase();
        return US_STATE_CODES.get(normalizedValue ?? '') ?? normalizedValue;
    };
    const hashedState = (0, utils_1.smartHash)(user_data?.state, normalizedState);
    const hashedZip = (0, utils_1.smartHash)(user_data?.zip, normalizedValue);
    const normalizedCountry = (value) => {
        const normalizedValue = value?.replace(/\s/g, '').toLowerCase();
        return COUNTRY_CODES.get(normalizedValue ?? '') ?? normalizedValue;
    };
    const hashedCountry = (0, utils_1.smartHash)(user_data?.country, normalizedCountry);
    const external_id = user_data?.externalId?.map((id) => {
        return (0, hashing_utils_1.processHashing)(id, 'sha256', 'hex', normalizedValue);
    });
    const db = (0, utils_1.smartHash)(user_data?.dateOfBirth);
    const lead_id = user_data?.leadID;
    const subscription_id = user_data?.subscriptionID;
    const idfv = user_data?.idfv ?? payload.idfv;
    const sc_click_id = user_data?.sc_click_id ?? payload.click_id;
    const sc_cookie1 = user_data?.sc_cookie1 ?? payload.uuid_c1;
    return {
        client_ip_address,
        client_user_agent,
        country: hashedCountry,
        ct: hashedCity,
        db,
        em: (0, utils_1.box)(email),
        external_id,
        fn: hashedFirstName,
        ge: hashedGender,
        idfv,
        lead_id,
        ln: hashedLastName,
        madid,
        ph: (0, utils_1.box)(phone_number),
        sc_click_id,
        sc_cookie1,
        st: hashedState,
        subscription_id,
        zp: hashedZip
    };
};
const buildCustomData = (payload) => {
    const { custom_data } = payload;
    const products = payload.products?.filter(({ item_id }) => item_id != null);
    const content_ids = products?.map(({ item_id }) => item_id ?? '') ?? (0, utils_1.splitListValueToArray)(payload.item_ids ?? '');
    const content_category = products?.map(({ item_category }) => item_category ?? '') ?? (0, utils_1.splitListValueToArray)(payload.item_category ?? '');
    const brands = products?.map((product) => product.brand ?? '') ?? payload.brands;
    const num_items = custom_data?.num_items ?? products?.length ?? (0, utils_1.parseNumberSafe)(payload.number_items);
    const currency = (0, utils_1.emptyStringToUndefined)(custom_data?.currency ?? payload.currency)?.toUpperCase();
    const order_id = (0, utils_1.emptyStringToUndefined)(custom_data?.order_id ?? payload.transaction_id);
    const search_string = (0, utils_1.emptyStringToUndefined)(custom_data?.search_string ?? payload.search_string);
    const sign_up_method = (0, utils_1.emptyStringToUndefined)(custom_data?.sign_up_method ?? payload.sign_up_method);
    const value = custom_data?.value ?? payload.price;
    const checkin_date = (0, utils_1.emptyStringToUndefined)(custom_data?.checkin_date);
    const travel_end = (0, utils_1.emptyStringToUndefined)(custom_data?.travel_end);
    const travel_start = (0, utils_1.emptyStringToUndefined)(custom_data?.travel_start);
    const suggested_destinations = (0, utils_1.emptyStringToUndefined)(custom_data?.suggested_destinations);
    const destination_airport = (0, utils_1.emptyStringToUndefined)(custom_data?.destination_airport);
    const country = (0, utils_1.emptyStringToUndefined)(custom_data?.country);
    const city = (0, utils_1.emptyStringToUndefined)(custom_data?.city);
    const region = (0, utils_1.emptyStringToUndefined)(custom_data?.region);
    const neighborhood = (0, utils_1.emptyStringToUndefined)(custom_data?.neighborhood);
    const departing_departure_date = (0, utils_1.emptyStringToUndefined)(custom_data?.departing_departure_date);
    const departing_arrival_date = (0, utils_1.emptyStringToUndefined)(custom_data?.departing_arrival_date);
    const num_adults = custom_data?.num_adults;
    const origin_airport = (0, utils_1.emptyStringToUndefined)(custom_data?.origin_airport);
    const returning_departure_date = (0, utils_1.emptyStringToUndefined)(custom_data?.returning_departure_date);
    const returning_arrival_date = (0, utils_1.emptyStringToUndefined)(custom_data?.returning_arrival_date);
    const num_children = custom_data?.num_children;
    const hotel_score = (0, utils_1.emptyStringToUndefined)(custom_data?.hotel_score);
    const postal_code = (0, utils_1.emptyStringToUndefined)(custom_data?.postal_code);
    const num_infants = custom_data?.num_infants;
    const preferred_neighborhoods = (0, utils_1.emptyStringToUndefined)(custom_data?.preferred_neighborhoods);
    const preferred_star_ratings = (0, utils_1.emptyStringToUndefined)(custom_data?.preferred_star_ratings);
    const suggested_hotels = (0, utils_1.emptyStringToUndefined)(custom_data?.suggested_hotels);
    const dta_fields = {
        checkin_date,
        travel_end,
        travel_start,
        suggested_destinations,
        destination_airport,
        country,
        city,
        region,
        neighborhood,
        departing_departure_date,
        departing_arrival_date,
        num_adults,
        origin_airport,
        returning_departure_date,
        returning_arrival_date,
        num_children,
        hotel_score,
        postal_code,
        num_infants,
        preferred_neighborhoods,
        preferred_star_ratings,
        suggested_hotels
    };
    return (0, utils_1.emptyObjectToUndefined)({
        brands,
        content_category,
        content_ids,
        currency,
        num_items,
        order_id,
        search_string,
        sign_up_method,
        value,
        ...dta_fields
    });
};
const eventConversionTypeToActionSource = {
    WEB: 'website',
    MOBILE_APP: 'app',
    OFFLINE: 'OFFLINE'
};
const getSupportedActionSource = (action_source) => {
    const normalizedActionSource = (0, utils_1.emptyStringToUndefined)(action_source);
    return ['website', 'app'].indexOf(normalizedActionSource ?? '') > -1
        ? normalizedActionSource
        : normalizedActionSource != null
            ? 'OFFLINE'
            : undefined;
};
const buildPayloadData = (payload, settings) => {
    const action_source = getSupportedActionSource(payload.action_source) ??
        eventConversionTypeToActionSource[payload.event_conversion_type ?? ''];
    const event_name = payload.event_name ?? payload.event_type;
    const event_source_url = payload.event_source_url ?? payload.page_url;
    const event_id = (0, utils_1.emptyStringToUndefined)(payload.event_id) ?? (0, utils_1.emptyStringToUndefined)(payload.client_dedup_id);
    const payload_event_time = payload.event_time ?? payload.timestamp;
    const event_time_number = (0, utils_1.parseNumberSafe)(payload_event_time ?? '');
    const event_time_date_time = (0, utils_1.parseDateSafe)(payload_event_time ?? '');
    const event_time = event_time_date_time ?? event_time_number;
    const app_data = action_source === 'app' ? buildAppData(payload, settings) : undefined;
    const user_data = buildUserData(payload);
    const custom_data = buildCustomData(payload);
    const data_processing_options = payload.data_processing_options ?? false ? ['LDU'] : undefined;
    return {
        integration: 'segment',
        event_id,
        event_name,
        event_source_url,
        event_time,
        user_data,
        custom_data,
        action_source,
        app_data,
        data_processing_options,
        data_processing_options_country: payload.data_processing_options_country,
        data_processing_options_state: payload.data_processing_options_state
    };
};
const validateSettingsConfig = (settings, action_source) => {
    const { snap_app_id, pixel_id } = settings;
    const snapAppID = (0, utils_1.emptyStringToUndefined)(snap_app_id);
    const snapPixelID = (0, utils_1.emptyStringToUndefined)(pixel_id);
    (0, utils_1.raiseMisconfiguredRequiredFieldErrorIf)(action_source === 'OFFLINE' && (0, utils_1.isNullOrUndefined)(snapPixelID), 'If event conversion type is "OFFLINE" then Pixel ID must be defined');
    (0, utils_1.raiseMisconfiguredRequiredFieldErrorIf)(action_source === 'app' && (0, utils_1.isNullOrUndefined)(snapAppID), 'If event conversion type is "MOBILE_APP" then Snap App ID must be defined');
    (0, utils_1.raiseMisconfiguredRequiredFieldErrorIf)(action_source === 'website' && (0, utils_1.isNullOrUndefined)(snapPixelID), `If event conversion type is "WEB" then Pixel ID must be defined`);
};
const buildRequestURL = (settings, action_source, authToken) => {
    const { snap_app_id, pixel_id } = settings;
    const appOrPixelID = (0, utils_1.emptyStringToUndefined)((() => {
        switch (action_source) {
            case 'website':
            case 'OFFLINE':
                return pixel_id;
            case 'app':
                return snap_app_id;
            default:
                return undefined;
        }
    })());
    return `https://tr.snapchat.com/v3/${appOrPixelID}/events?access_token=${authToken}`;
};
const validatePayload = (payload) => {
    const { action_source, event_name, event_time, custom_data = {}, user_data } = payload;
    (0, utils_1.raiseMisconfiguredRequiredFieldErrorIfNullOrUndefined)(action_source, "The root value is missing the required field 'action_source'.");
    (0, utils_1.raiseMisconfiguredRequiredFieldErrorIfNullOrUndefined)(event_name, "The root value is missing the required field 'event_name'.");
    (0, utils_1.raiseMisconfiguredRequiredFieldErrorIfNullOrUndefined)(event_time, "The root value is missing the required field 'event_time'.");
    (0, utils_1.raiseMisconfiguredRequiredFieldErrorIf)(!(0, utils_1.isNullOrUndefined)(custom_data.currency) && !CURRENCY_ISO_4217_CODES.has(custom_data.currency), `${custom_data.currency} is not a valid currency code.`);
    (0, utils_1.raiseMisconfiguredRequiredFieldErrorIf)((0, utils_1.isNullOrUndefined)(user_data.em) &&
        (0, utils_1.isNullOrUndefined)(user_data.ph) &&
        (0, utils_1.isNullOrUndefined)(user_data.madid) &&
        ((0, utils_1.isNullOrUndefined)(user_data.client_ip_address) || (0, utils_1.isNullOrUndefined)(user_data.client_user_agent)), `Payload must contain values for Email or Phone Number or Mobile Ad Identifier or both IP Address and User Agent fields`);
};
const performSnapCAPIv3 = async (request, data) => {
    const { payload, settings } = data;
    const payloadData = buildPayloadData(payload, settings);
    validatePayload(payloadData);
    validateSettingsConfig(settings, payloadData.action_source);
    const authToken = (0, utils_1.emptyStringToUndefined)(data.auth?.accessToken);
    (0, utils_1.raiseMisconfiguredRequiredFieldErrorIfNullOrUndefined)(authToken, 'Missing valid auth token');
    const url = buildRequestURL(settings, payloadData.action_source, authToken);
    return request(url, {
        method: 'post',
        json: {
            data: [payloadData]
        }
    });
};
exports.performSnapCAPIv3 = performSnapCAPIv3;
//# sourceMappingURL=snap-capi-v3.js.map