"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createEventRequestParams = exports.createUserRequestParams = exports.deleteUserRequestParams = exports.setUserPropertiesRequestParams = exports.customEventRequestParams = exports.meRequestParams = void 0;
const versioning_info_1 = require("./versioning-info");
const apiBaseUrl = 'https://api.fullstory.com';
const segmentIntegrationSource = 'segment';
const integrationSourceQueryParam = `integration=${segmentIntegrationSource}`;
const defaultRequestParams = (settings, relativeUrl) => {
    return {
        url: `${apiBaseUrl}/${relativeUrl}`,
        options: {
            method: 'get',
            headers: {
                'Content-Type': 'application/json',
                Authorization: `Basic ${settings.apiKey}`,
                'Integration-Source': segmentIntegrationSource
            }
        }
    };
};
const meRequestParams = (settings) => defaultRequestParams(settings, 'me');
exports.meRequestParams = meRequestParams;
const customEventRequestParams = (settings, requestValues) => {
    const { userId, eventName, eventData, timestamp, useRecentSession, sessionUrl } = requestValues;
    const defaultParams = defaultRequestParams(settings, `users/${versioning_info_1.FULLSTORY_API_VERSION}/individual/${encodeURIComponent(userId)}/customevent?${integrationSourceQueryParam}`);
    const requestBody = {
        event: {
            event_name: eventName,
            event_data: eventData
        }
    };
    if (timestamp) {
        requestBody.event.timestamp = timestamp;
    }
    if (useRecentSession) {
        requestBody.event.use_recent_session = useRecentSession;
    }
    if (sessionUrl) {
        requestBody.event.session_url = sessionUrl;
    }
    return {
        ...defaultParams,
        options: {
            ...defaultParams.options,
            method: 'post',
            json: requestBody
        }
    };
};
exports.customEventRequestParams = customEventRequestParams;
const setUserPropertiesRequestParams = (settings, userId, requestBody) => {
    const defaultParams = defaultRequestParams(settings, `users/${versioning_info_1.FULLSTORY_API_VERSION}/individual/${encodeURIComponent(userId)}/customvars?${integrationSourceQueryParam}`);
    return {
        ...defaultParams,
        options: {
            ...defaultParams.options,
            method: 'post',
            json: requestBody
        }
    };
};
exports.setUserPropertiesRequestParams = setUserPropertiesRequestParams;
const deleteUserRequestParams = (settings, userId) => {
    const defaultParams = defaultRequestParams(settings, `v2/users?uid=${encodeURIComponent(userId)}`);
    return {
        ...defaultParams,
        options: {
            ...defaultParams.options,
            method: 'delete'
        }
    };
};
exports.deleteUserRequestParams = deleteUserRequestParams;
const createUserRequestParams = (settings, requestBody) => {
    const defaultParams = defaultRequestParams(settings, `v2/users`);
    return {
        ...defaultParams,
        options: {
            ...defaultParams.options,
            method: 'post',
            json: requestBody
        }
    };
};
exports.createUserRequestParams = createUserRequestParams;
const createEventRequestParams = (settings, requestValues) => {
    const { userId, eventName, properties: eventData, timestamp, useRecentSession, sessionUrl } = requestValues;
    const defaultParams = defaultRequestParams(settings, `v2/events`);
    const requestBody = {
        name: eventName,
        properties: eventData
    };
    if (userId) {
        requestBody.user = {
            uid: userId
        };
    }
    if (timestamp) {
        requestBody.timestamp = timestamp;
    }
    if (sessionUrl) {
        requestBody.session = {
            id: decodeURIComponent(sessionUrl.substring(sessionUrl.lastIndexOf('/') + 1))
        };
    }
    if (useRecentSession) {
        if (requestBody.session) {
            requestBody.session.use_most_recent = useRecentSession;
        }
        else {
            requestBody.session = {
                use_most_recent: useRecentSession
            };
        }
    }
    return {
        ...defaultParams,
        options: {
            ...defaultParams.options,
            method: 'post',
            json: requestBody
        }
    };
};
exports.createEventRequestParams = createEventRequestParams;
//# sourceMappingURL=request-params.js.map