"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.send = send;
const actions_core_1 = require("@segment/actions-core");
const formatter_1 = require("./formatter");
const constants_1 = require("./constants");
const common_fields_1 = require("../reportAppEvent/fields/common_fields");
function send(request, payload, settings) {
    const { event_source, event, event_id, test_event_code, limited_data_use, timestamp } = payload;
    const { appID } = settings;
    const user = getUser(payload);
    const properties = getProps(payload);
    const app = getApp(payload);
    const ad = getAd(payload);
    const requestJson = {
        event_source,
        event_source_id: appID,
        partner_name: 'Segment',
        ...(test_event_code ? { test_event_code } : {}),
        data: [
            {
                event,
                event_time: timestamp
                    ? Math.floor(new Date(timestamp).getTime() / 1000)
                    : Math.floor(new Date().getTime() / 1000),
                event_id: event_id || undefined,
                user,
                properties,
                app,
                ad,
                limited_data_use: typeof limited_data_use === 'boolean' ? limited_data_use : false
            }
        ]
    };
    return request('https://business-api.tiktok.com/open_api/v1.3/event/track/', {
        method: 'post',
        json: requestJson
    });
}
function getUser(payload) {
    const { phone_number, email, external_id, ip, user_agent, locale, device_details: { device_type, device_id } = {} } = payload;
    const phone_numbers = (0, formatter_1.formatPhones)(phone_number);
    const emails = (0, formatter_1.formatEmails)(email);
    const userIds = (0, formatter_1.formatUserIds)(external_id);
    const attStatus = getATTStatus(payload);
    const advertisingId = getAdvertisingId(payload);
    const requestUser = {
        ...(userIds && userIds.length > 0 ? { external_id: userIds } : {}),
        ...(phone_numbers && phone_numbers.length > 0 ? { phone: phone_numbers } : {}),
        ...(emails && emails.length > 0 ? { email: emails } : {}),
        ...(advertisingId && device_type?.toLocaleLowerCase() === 'ios' ? { idfa: advertisingId } : {}),
        ...(advertisingId && device_type?.toLocaleLowerCase() === 'android' ? { gaid: advertisingId } : {}),
        ...(device_id && device_type?.toLocaleLowerCase() === 'ios' ? { idfv: device_id } : {}),
        ...(attStatus ? { att_status: attStatus } : {}),
        ...(ip ? { ip } : {}),
        ...(user_agent ? { user_agent } : {}),
        ...(locale ? { locale } : {})
    };
    return requestUser;
}
function getATTStatus(payload) {
    const { att_status, device_details: { device_type, device_version, ad_tracking_enabled } = {} } = payload;
    if (att_status === 'AUTO') {
        if (device_type?.toLocaleLowerCase() !== 'ios') {
            return constants_1.APP_STATUS.NOT_APPLICABLE;
        }
        else {
            if (device_version && isVersionLower(device_version, '14.0.0')) {
                return constants_1.APP_STATUS.NOT_APPLICABLE;
            }
        }
        if (typeof ad_tracking_enabled === 'boolean') {
            return ad_tracking_enabled ? constants_1.APP_STATUS.AUTHORIZED : constants_1.APP_STATUS.DENIED;
        }
        return constants_1.APP_STATUS.NOT_DETERMINED;
    }
    else {
        return att_status;
    }
}
function getAdvertisingId(payload) {
    const { advertising_id, device_details: { device_type } = {} } = payload;
    if (!advertising_id || !device_type) {
        return undefined;
    }
    const platform = device_type.toLocaleLowerCase();
    if (platform === 'ios') {
        return (0, formatter_1.formatAdvertisingId)(advertising_id, true);
    }
    if (platform === 'android') {
        return (0, formatter_1.formatAdvertisingId)(advertising_id, false);
    }
    return undefined;
}
function getProps(payload) {
    const { event, content_type, currency, value, description, content_ids, num_items, search_string, contents } = payload;
    const productMappingType = common_fields_1.STANDARD_EVENTS.find((se) => se.ttEventName === event)?.productMappingType;
    if (productMappingType && (productMappingType === common_fields_1.PRODUCT_MAPPING_TYPE.SINGLE || productMappingType === common_fields_1.PRODUCT_MAPPING_TYPE.MULTIPLE)) {
        contents?.forEach(content => {
            if (!content.content_id) {
                throw new actions_core_1.PayloadValidationError(`content_id is required for event ${event}`);
            }
        });
    }
    const requestProperties = {
        contents: contents
            ? contents.map(({ price, quantity, content_category, content_id, content_name, brand }) => ({
                ...(price ? { price } : {}),
                ...(typeof quantity === 'number' ? { quantity } : {}),
                ...(content_category ? { content_category } : {}),
                ...(content_id ? { content_id } : {}),
                ...(content_name ? { content_name } : {}),
                ...(brand ? { brand } : {})
            }))
            : [],
        ...(content_type !== undefined && { content_type }),
        ...(currency !== undefined && { currency }),
        ...(value !== undefined && { value }),
        ...(description !== undefined && { description }),
        ...(content_ids !== undefined && { content_ids }),
        ...(num_items !== undefined && { num_items }),
        ...(search_string !== undefined && { search_string })
    };
    return requestProperties;
}
function getApp(payload) {
    const { app_id, app_name, app_version } = payload.app || {};
    const app = {
        app_id,
        ...(app_name ? { app_name } : {}),
        ...(app_version ? { app_version } : {})
    };
    return app;
}
function getAd(payload) {
    const { callback, campaign_id, ad_id, creative_id, is_retargeting, attributed, attribution_type, attribution_provider } = payload.ad || {};
    const ad = {
        ...(callback ? { callback } : {}),
        ...(campaign_id ? { campaign_id } : {}),
        ...(ad_id ? { ad_id } : {}),
        ...(creative_id ? { creative_id } : {}),
        ...(typeof is_retargeting === 'boolean' ? { is_retargeting } : {}),
        ...(typeof attributed === 'boolean' ? { attributed } : {}),
        ...(attribution_type ? { attribution_type } : {}),
        ...(attribution_provider ? { attribution_provider } : {})
    };
    return ad;
}
function isVersionLower(version, target = "14.0.0") {
    const [a1 = 0, a2 = 0, a3 = 0] = version.split('.').map(Number);
    const [b1 = 0, b2 = 0, b3 = 0] = target.split('.').map(Number);
    return a1 < b1 || (a1 === b1 && a2 < b2) || (a1 === b1 && a2 === b2 && a3 < b3);
}
//# sourceMappingURL=utils.js.map