"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveRequestPayload = exports.eventRequestParams = void 0;
const vars_1 = require("./vars");
const apiBaseUrl = 'https://events.usermaven.com';
const defaultRequestParams = (settings, relativeUrl) => {
    return {
        url: `${apiBaseUrl}/${relativeUrl}?token=${settings.api_key}.${settings.server_token}`,
        options: {
            method: 'post',
            headers: {
                'Content-Type': 'application/json'
            }
        }
    };
};
const eventRequestParams = (settings, payload, eventType) => {
    const defaultRequest = defaultRequestParams(settings, 'api/v1/s2s/event');
    return {
        ...defaultRequest,
        options: {
            ...defaultRequest.options,
            json: {
                ...payload,
                event_type: eventType || payload?.event_type
            }
        }
    };
};
exports.eventRequestParams = eventRequestParams;
const resolveRequestPayload = (settings, payload) => {
    const properties = {
        api_key: settings.api_key,
        ids: {},
        doc_encoding: 'utf-8',
        src: 'usermaven-segment',
        screen_resolution: '0',
        ...payload
    };
    if (payload.url) {
        const url = new URL(payload.url);
        properties.doc_host = url.host;
    }
    if (payload?.screen && payload.screen.width && payload.screen.height) {
        const { width, height } = payload.screen;
        properties.screen_resolution = `${width || 0}x${height || 0}`;
        properties.vp_size = `${width || 0}x${height || 0}`;
        delete properties.screen;
    }
    if (payload?.timezone) {
        const now = new Date();
        const dateInTimezone = new Date(now.toLocaleString('en-US', {
            timeZone: payload.timezone,
            hour12: false
        }));
        const offset = (now.getTime() - dateInTimezone.getTime()) / (1000 * 60);
        const offsetMinutes = Math.round(offset);
        properties.local_tz_offset = offsetMinutes;
        delete properties.timezone;
    }
    if (payload?.event) {
        properties.event_type = payload.event;
        delete properties.event;
    }
    properties.user = {
        id: payload?.user_id,
        anonymous_id: payload?.user_anonymous_id || (0, vars_1.generateId)(),
        email: payload?.user_email,
        first_name: payload?.user_first_name,
        last_name: payload?.user_last_name,
        created_at: payload?.user_created_at,
        custom: payload?.user_custom_attributes
    };
    delete properties.user_id;
    delete properties.user_anonymous_id;
    delete properties.user_email;
    delete properties.user_first_name;
    delete properties.user_last_name;
    delete properties.user_custom_attributes;
    delete properties.user_created_at;
    if (payload?.company_id) {
        properties.company = {
            id: payload?.company_id,
            name: payload?.company_name,
            created_at: payload?.company_created_at,
            custom: payload?.company_custom_attributes
        };
    }
    delete properties.company_id;
    delete properties.company_name;
    delete properties.company_created_at;
    delete properties.company_custom_attributes;
    if (payload?.user_custom_attributes) {
        const { first_name, last_name, email, created_at, ...customAttributes } = payload.user_custom_attributes;
        properties.user.custom = customAttributes;
    }
    if (payload?.event_attributes) {
        const { email, created_at, ...eventAttributes } = payload.event_attributes;
        properties.event_attributes = eventAttributes;
    }
    if (payload?.company && payload?.company_custom_attributes) {
        const { name, created_at, company_id, company_created_at, ...customAttributes } = payload.company_custom_attributes;
        properties.company.custom = customAttributes;
    }
    return properties;
};
exports.resolveRequestPayload = resolveRequestPayload;
//# sourceMappingURL=request-params.js.map