"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildContextProps = buildContextProps;
exports.sendTrack = sendTrack;
exports.sendIdentify = sendIdentify;
exports.sendTrackBatch = sendTrackBatch;
exports.sendIdentifyBatch = sendIdentifyBatch;
exports.sendAlias = sendAlias;
exports.sendAliasBatch = sendAliasBatch;
function contextFields(context) {
    return context;
}
function buildContextProps(context, channel) {
    const result = {};
    if (context) {
        const { ip, url, referrer, os, user_agent, utm_campaign, utm_source, utm_medium, utm_term, utm_content, screen_width, screen_height, library_name, library_version } = context;
        if (ip !== undefined && ip !== '') {
            result.$ip = ip;
        }
        if (url) {
            result.$url = url;
        }
        if (referrer) {
            result.$referer = referrer;
        }
        if (os) {
            result.$os = os;
        }
        if (user_agent) {
            result.$user_agent = user_agent;
        }
        if (utm_campaign) {
            result.$utm_campaign = utm_campaign;
        }
        if (utm_source) {
            result.$utm_source = utm_source;
        }
        if (utm_medium) {
            result.$utm_medium = utm_medium;
        }
        if (utm_term) {
            result.$utm_term = utm_term;
        }
        if (utm_content) {
            result.$utm_content = utm_content;
        }
        if (screen_width && screen_height) {
            result.$viewport = `${screen_width}x${screen_height}`;
        }
        result.$lib = library_name || 'altertable-segment';
        if (library_version) {
            result.$lib_version = library_version;
        }
    }
    else {
        result.$lib = 'altertable-segment';
    }
    if (channel === 'server' && !('$ip' in result)) {
        result.$ip = 0;
    }
    return result;
}
function buildTrackJSON(settings, payload) {
    const { userId, anonymousId, properties } = payload;
    const channel = payload.channel === 'server' ? 'server' : undefined;
    const contextProps = buildContextProps(contextFields(payload.context), channel);
    const device_id = payload.context?.device_id;
    return {
        environment: settings.environment,
        timestamp: payload.timestamp,
        distinct_id: userId || anonymousId,
        anonymous_id: userId && anonymousId ? anonymousId : undefined,
        device_id,
        event: payload.event,
        properties: {
            ...contextProps,
            ...properties
        }
    };
}
function buildIdentifyJSON(settings, payload) {
    const { traits } = payload;
    const channel = payload.channel === 'server' ? 'server' : undefined;
    const contextProps = buildContextProps(contextFields(payload.context), channel);
    const distinctId = payload.userId || payload.anonymousId;
    const anonymousId = payload.userId && payload.userId !== payload.anonymousId ? payload.anonymousId : undefined;
    return {
        environment: settings.environment,
        traits: {
            ...traits,
            ...contextProps
        },
        timestamp: payload.timestamp,
        distinct_id: distinctId,
        anonymous_id: anonymousId,
        device_id: payload.context?.device_id
    };
}
function buildAliasJSON(settings, payload) {
    return {
        environment: settings.environment,
        new_user_id: payload.userId,
        timestamp: payload.timestamp,
        distinct_id: payload.previousId,
        device_id: contextFields(payload.context)?.device_id
    };
}
function sendTrack(request, settings, payload) {
    const json = buildTrackJSON(settings, payload);
    return request(`${settings.endpoint}/track`, {
        method: 'post',
        json
    });
}
function sendIdentify(request, settings, payload) {
    const json = buildIdentifyJSON(settings, payload);
    return request(`${settings.endpoint}/identify`, {
        method: 'post',
        json
    });
}
function sendTrackBatch(request, settings, payloads) {
    const json = payloads.map((p) => buildTrackJSON(settings, p));
    return request(`${settings.endpoint}/track`, {
        method: 'post',
        json
    });
}
function sendIdentifyBatch(request, settings, payloads) {
    const json = payloads.map((p) => buildIdentifyJSON(settings, p));
    return request(`${settings.endpoint}/identify`, {
        method: 'post',
        json
    });
}
function sendAlias(request, settings, payload) {
    const json = buildAliasJSON(settings, payload);
    return request(`${settings.endpoint}/alias`, {
        method: 'post',
        json
    });
}
function sendAliasBatch(request, settings, payloads) {
    const json = payloads.map((p) => buildAliasJSON(settings, p));
    return request(`${settings.endpoint}/alias`, {
        method: 'post',
        json
    });
}
//# sourceMappingURL=utils.js.map