"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StrictMode = exports.ApiRegions = void 0;
exports.getConcatenatedName = getConcatenatedName;
exports.getApiServerUrl = getApiServerUrl;
exports.getBrowser = getBrowser;
exports.getBrowserVersion = getBrowserVersion;
exports.cheapGuid = cheapGuid;
exports.handleMixPanelApiResponse = handleMixPanelApiResponse;
const actions_core_1 = require("@segment/actions-core");
var ApiRegions;
(function (ApiRegions) {
    ApiRegions["US"] = "US \uD83C\uDDFA\uD83C\uDDF8";
    ApiRegions["EU"] = "EU \uD83C\uDDEA\uD83C\uDDFA";
    ApiRegions["IN"] = "IN \uD83C\uDDEE\uD83C\uDDF3";
})(ApiRegions || (exports.ApiRegions = ApiRegions = {}));
var StrictMode;
(function (StrictMode) {
    StrictMode["ON"] = "1";
    StrictMode["OFF"] = "0";
})(StrictMode || (exports.StrictMode = StrictMode = {}));
function getConcatenatedName(firstName, lastName, name) {
    return name ?? (firstName && lastName ? `${firstName} ${lastName}` : undefined);
}
function getApiServerUrl(apiRegion) {
    if (apiRegion == ApiRegions.EU) {
        return 'https://api-eu.mixpanel.com';
    }
    if (apiRegion == ApiRegions.IN) {
        return 'https://api-in.mixpanel.com';
    }
    return 'https://api.mixpanel.com';
}
function getBrowser(userAgent) {
    if (userAgent.includes(' OPR/')) {
        if (userAgent.includes('Mini')) {
            return 'Opera Mini';
        }
        return 'Opera';
    }
    else if (/(BlackBerry|PlayBook|BB10)/i.test(userAgent)) {
        return 'BlackBerry';
    }
    else if (userAgent.includes('IEMobile') || userAgent.includes('WPDesktop')) {
        return 'Internet Explorer Mobile';
    }
    else if (userAgent.includes('SamsungBrowser/')) {
        return 'Samsung Internet';
    }
    else if (userAgent.includes('Edge') || userAgent.includes('Edg/')) {
        return 'Microsoft Edge';
    }
    else if (userAgent.includes('FBIOS')) {
        return 'Facebook Mobile';
    }
    else if (userAgent.includes('Chrome')) {
        return 'Chrome';
    }
    else if (userAgent.includes('CriOS')) {
        return 'Chrome iOS';
    }
    else if (userAgent.includes('UCWEB') || userAgent.includes('UCBrowser')) {
        return 'UC Browser';
    }
    else if (userAgent.includes('FxiOS')) {
        return 'Firefox iOS';
    }
    else if (userAgent.includes('Safari')) {
        if (userAgent.includes('iPhone')) {
            return `Mobile Safari`;
        }
        return 'Safari';
    }
    else if (userAgent.includes('Android')) {
        return 'Android Mobile';
    }
    else if (userAgent.includes('Konqueror')) {
        return 'Konqueror';
    }
    else if (userAgent.includes('Firefox')) {
        return 'Firefox';
    }
    else if (userAgent.includes('MSIE') || userAgent.includes('Trident/')) {
        return 'Internet Explorer';
    }
    else if (userAgent.includes('Gecko')) {
        return 'Mozilla';
    }
    else {
        return '';
    }
}
function getBrowserVersion(userAgent) {
    const browser = getBrowser(userAgent);
    const versionRegexs = {
        'Internet Explorer Mobile': /rv:(\d+(\.\d+)+)/,
        'Microsoft Edge': /Edge?\/(\d+(\.\d+)+)/,
        Chrome: /Chrome\/(\d+(\.\d+)+)/,
        'Chrome iOS': /CriOS\/(\d+(\.\d+)+)/,
        'UC Browser': /(UCBrowser|UCWEB)\/(\d+(\.\d+)+)/,
        Safari: /Version\/(\d+(\.\d+)+)/,
        'Mobile Safari': /Version\/(\d+(\.\d+)+)/,
        Opera: /(Opera|OPR)\/(\d+(\.\d+)+)/,
        Firefox: /Firefox\/(\d+(\.\d+)+)/,
        'Firefox iOS': /FxiOS\/(\d+(\.\d+)+)/,
        Konqueror: /Konqueror:(\d+(\.\d+)+)/,
        BlackBerry: /BlackBerry (\d+(\.\d+)+)/,
        'Android Mobile': /android\s(\d+(\.\d+)+)/,
        'Samsung Internet': /SamsungBrowser\/(\d+(\.\d+)+)/,
        'Internet Explorer': /(rv:|MSIE )(\d+(\.\d+)+)/,
        Mozilla: /rv:(\d+(\.\d+)+)/
    };
    const regex = versionRegexs[browser];
    if (!regex)
        return regex;
    const matches = regex.exec(userAgent);
    if (!matches) {
        return undefined;
    }
    return matches[matches.length - 2];
}
function cheapGuid(maxlen) {
    const guid = Math.random().toString(36).substring(2, 10) + Math.random().toString(36).substring(2, 10);
    return maxlen ? guid.substring(0, maxlen) : guid;
}
function handleMixPanelApiResponse(payloadCount, apiResponse, events) {
    const multiStatusResponse = new actions_core_1.MultiStatusResponse();
    if (apiResponse.data.code === 400 || apiResponse.data.code === 200) {
        for (let i = 0; i < payloadCount; i++) {
            multiStatusResponse.setSuccessResponseAtIndex(i, {
                status: 200,
                body: apiResponse.data.status ?? 'Ok',
                sent: events[i]
            });
        }
        apiResponse.data.failed_records?.map((data) => {
            multiStatusResponse.setErrorResponseAtIndex(data.index, {
                status: 400,
                errormessage: data.message,
                sent: events[data.index],
                body: data
            });
        });
    }
    if (apiResponse.data.code !== 200 && apiResponse.data.code !== 400) {
        for (let i = 0; i < payloadCount; i++) {
            multiStatusResponse.setErrorResponseAtIndex(i, {
                status: apiResponse.data.code,
                errormessage: apiResponse.data.error ?? 'Unknown error from Mixpanel',
                sent: events[i],
                body: apiResponse.data.error
            });
        }
    }
    return multiStatusResponse;
}
//# sourceMappingURL=utils.js.map