"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CLIENT_ID_STORAGE_KEY = void 0;
exports.getClientId = getClientId;
exports.clearClientId = clearClientId;
exports.getOrCreateClientId = getOrCreateClientId;
exports.appendClientIdHeader = appendClientIdHeader;
exports.buildTrackedPageUrl = buildTrackedPageUrl;
exports.CLIENT_ID_STORAGE_KEY = 'work-app.visitor.client-id';
function generateFallbackClientId() {
    return "client-".concat(Date.now().toString(36), "-").concat(Math.random().toString(36).slice(2, 10));
}
function generateClientId() {
    if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
        return crypto.randomUUID();
    }
    return generateFallbackClientId();
}
function getClientId() {
    var _a, _b;
    if (typeof window === 'undefined') {
        return '';
    }
    return (_b = (_a = window.localStorage.getItem(exports.CLIENT_ID_STORAGE_KEY)) === null || _a === void 0 ? void 0 : _a.trim()) !== null && _b !== void 0 ? _b : '';
}
function clearClientId() {
    if (typeof window === 'undefined') {
        return;
    }
    window.localStorage.removeItem(exports.CLIENT_ID_STORAGE_KEY);
}
function getOrCreateClientId() {
    var existingClientId = getClientId();
    if (existingClientId) {
        return existingClientId;
    }
    if (typeof window === 'undefined') {
        return '';
    }
    var nextClientId = generateClientId();
    window.localStorage.setItem(exports.CLIENT_ID_STORAGE_KEY, nextClientId);
    return nextClientId;
}
function appendClientIdHeader(headersInit) {
    var headers = new Headers(headersInit);
    var clientId = getOrCreateClientId();
    if (clientId && !headers.has('X-Client-Id')) {
        headers.set('X-Client-Id', clientId);
    }
    return headers;
}
function buildTrackedPageUrl(page) {
    if (typeof window === 'undefined') {
        return '';
    }
    var url = new URL(window.location.href);
    if (page.topMenu === 'plans') {
        url.searchParams.set('topMenu', 'plans');
        url.searchParams.set('planFilter', page.section);
        url.searchParams.delete('planSection');
    }
    else {
        url.searchParams.set('topMenu', page.topMenu);
        url.searchParams.delete('planSection');
        url.searchParams.delete('planFilter');
    }
    if (page.topMenu === 'docs') {
        url.searchParams.set('docsSection', page.section);
    }
    else {
        url.searchParams.delete('docsSection');
    }
    if (page.topMenu === 'apis') {
        url.searchParams.set('apiSection', page.section);
    }
    else {
        url.searchParams.delete('apiSection');
    }
    if (page.topMenu === 'chat') {
        url.searchParams.set('chatSection', page.section);
    }
    else {
        url.searchParams.delete('chatSection');
    }
    if (page.topMenu === 'play') {
        url.searchParams.set('playSection', page.section);
    }
    else {
        url.searchParams.delete('playSection');
    }
    return url.toString();
}
