"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDebugConfig = exports.getStorageSize = exports.getServerUrl = exports.generateSessionReplayId = exports.getCurrentUrl = exports.isSessionInSample = exports.generateHashCode = exports.maskFn = void 0;
var tslib_1 = require("tslib");
var analytics_core_1 = require("@amplitude/analytics-core");
var rrweb_snapshot_1 = require("@amplitude/rrweb-snapshot");
var types_1 = require("./config/types");
var constants_1 = require("./constants");
/**
 * Light: Subset of inputs
 * Medium: All inputs
 * Conservative: All inputs and all texts
 */
var isMaskedForLevel = function (elementType, level, element) {
    switch (level) {
        case 'light': {
            if (elementType !== 'input') {
                return true;
            }
            var inputType = element ? (0, rrweb_snapshot_1.getInputType)(element) : '';
            /* istanbul ignore if */ // TODO(lew): For some reason it's impossible to test this.
            if (!inputType) {
                return false;
            }
            if (['password', 'hidden', 'email', 'tel'].includes(inputType)) {
                return true;
            }
            if (element.autocomplete.startsWith('cc-')) {
                return true;
            }
            return false;
        }
        case 'medium':
        case 'conservative':
            return true;
        default:
            return isMaskedForLevel(elementType, types_1.DEFAULT_MASK_LEVEL, element);
    }
};
/**
 * Checks if the given element set to be masked by rrweb
 *
 * Priority is:
 *  1. [In code] Element/class based masking/unmasking <> [Config based] Selector based masking/unmasking
 *  2. Use app defaults
 */
var isMasked = function (elementType, config, element) {
    var _a, _b, _c;
    if (config === void 0) { config = { defaultMaskLevel: types_1.DEFAULT_MASK_LEVEL }; }
    if (element) {
        // Element or parent is explicitly instrumented in code to mask
        if (element.closest('.' + constants_1.MASK_TEXT_CLASS)) {
            return true;
        }
        // Config has override for mask
        var shouldMask = ((_a = config.maskSelector) !== null && _a !== void 0 ? _a : []).some(function (selector) { return element.closest(selector); });
        if (shouldMask) {
            return true;
        }
        // Code or config has override to unmask
        if (element.closest('.' + constants_1.UNMASK_TEXT_CLASS)) {
            return false;
        }
        // Here we are probably sent an element, but we want to match if they have a
        // parent with an unmask selector.
        var shouldUnmask = ((_b = config.unmaskSelector) !== null && _b !== void 0 ? _b : []).some(function (selector) { return element.closest(selector); });
        if (shouldUnmask) {
            return false;
        }
    }
    return isMaskedForLevel(elementType, (_c = config.defaultMaskLevel) !== null && _c !== void 0 ? _c : types_1.DEFAULT_MASK_LEVEL, element);
};
var maskFn = function (elementType, config) {
    return function (text, element) {
        return isMasked(elementType, config, element) ? text.replace(/[^\s]/g, '*') : text;
    };
};
exports.maskFn = maskFn;
var generateHashCode = function (str) {
    var hash = 0;
    if (str.length === 0)
        return hash;
    for (var i = 0; i < str.length; i++) {
        var chr = str.charCodeAt(i);
        hash = (hash << 5) - hash + chr;
        hash |= 0;
    }
    return hash;
};
exports.generateHashCode = generateHashCode;
var isSessionInSample = function (sessionId, sampleRate) {
    var hashNumber = (0, exports.generateHashCode)(sessionId.toString());
    var absHash = Math.abs(hashNumber);
    var absHashMultiply = absHash * 31;
    var mod = absHashMultiply % 1000000;
    return mod / 1000000 < sampleRate;
};
exports.isSessionInSample = isSessionInSample;
var getCurrentUrl = function () {
    var globalScope = (0, analytics_core_1.getGlobalScope)();
    return (globalScope === null || globalScope === void 0 ? void 0 : globalScope.location) ? globalScope.location.href : '';
};
exports.getCurrentUrl = getCurrentUrl;
var generateSessionReplayId = function (sessionId, deviceId) {
    return "".concat(deviceId, "/").concat(sessionId);
};
exports.generateSessionReplayId = generateSessionReplayId;
var getServerUrl = function (serverZone, trackServerUrl) {
    if (trackServerUrl) {
        return trackServerUrl;
    }
    if (serverZone === analytics_core_1.ServerZone.STAGING) {
        return constants_1.SESSION_REPLAY_STAGING_URL;
    }
    if (serverZone === analytics_core_1.ServerZone.EU) {
        return constants_1.SESSION_REPLAY_EU_URL;
    }
    return constants_1.SESSION_REPLAY_SERVER_URL;
};
exports.getServerUrl = getServerUrl;
var getStorageSize = function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
    var globalScope, _a, usage, quota, usageDetails, totalStorageSize, percentOfQuota, e_1;
    return tslib_1.__generator(this, function (_b) {
        switch (_b.label) {
            case 0:
                _b.trys.push([0, 3, , 4]);
                globalScope = (0, analytics_core_1.getGlobalScope)();
                if (!globalScope) return [3 /*break*/, 2];
                return [4 /*yield*/, globalScope.navigator.storage.estimate()];
            case 1:
                _a = _b.sent(), usage = _a.usage, quota = _a.quota, usageDetails = _a.usageDetails;
                totalStorageSize = usage ? Math.round(usage / constants_1.KB_SIZE) : 0;
                percentOfQuota = usage && quota ? Math.round((usage / quota + Number.EPSILON) * 1000) / 1000 : 0;
                return [2 /*return*/, { totalStorageSize: totalStorageSize, percentOfQuota: percentOfQuota, usageDetails: JSON.stringify(usageDetails) }];
            case 2: return [3 /*break*/, 4];
            case 3:
                e_1 = _b.sent();
                return [3 /*break*/, 4];
            case 4: return [2 /*return*/, { totalStorageSize: 0, percentOfQuota: 0, usageDetails: '' }];
        }
    });
}); };
exports.getStorageSize = getStorageSize;
var getDebugConfig = function (config) {
    var debugConfig = tslib_1.__assign({}, config);
    var apiKey = debugConfig.apiKey;
    debugConfig.apiKey = "****".concat(apiKey.substring(apiKey.length - 4));
    return debugConfig;
};
exports.getDebugConfig = getDebugConfig;
//# sourceMappingURL=helpers.js.map