function isMobile() {
    const mobile = ['Android', 'webOS', 'iPhone', 'iPod', 'BlackBerry', 'Windows Phone', 'Opera Mini', 'IEMobile'];
    for (let i = 0; i <= mobile.length - 1; i++) {
        let mobilex = navigator.userAgent.indexOf(mobile[i]);
        if (mobilex != -1) {
            return true;
            break;
        }
    }
    return false;
}

function isPad() {
    const pad = ['iPad'];
    for (let i = 0; i <= pad.length - 1; i++) {
        let mobilex = navigator.userAgent.indexOf(pad[i]);
        if (mobilex !== -1) {
            return true;
        }
    }
    return false;
}
function jwtDecode(token) {
    if (!token) return null;
    const base64Url = token.split('.')[1];
    const base64 = base64Url.replace('-', '+').replace('_', '/');
    return JSON.parse(window.atob(base64)) || false;
}

function setCookie(name, value, domain) {
    if (domain) {
        document.cookie = name + '=' + value + '; domain=' + domain + ';';
    } else {
        document.cookie = name + '=' + value;
    }
}

function getCookie(cname) {
    let name = cname + '=';
    let decodedCookie = decodeURIComponent(document.cookie);
    let ca = decodedCookie.split(';');
    for (let i = 0; i < ca.length; i++) {
        let c = ca[i];
        while (c.charAt(0) === ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) === 0) {
            return c.substring(name.length, c.length);
        }
    }
    return '';
}

function getTmCookie(cookieName, key) {
    let name = cookieName + '=';
    let decodedCookie = decodeURIComponent(document.cookie);
    let ca = decodedCookie.split(';');

    for (let i = 0; i < ca.length; i++) {
        let c = ca[i].trim();
        if (c.indexOf(name) === 0) {
            let jsonString = c.substring(name.length);
            try {
                let jsonObject = JSON.parse(jsonString);
                return jsonObject[key];
            } catch (e) {
                console.error('Error parsing JSON from cookie:', e);
            }
        }
    }
    return false;
}
const checkActiveChatSession = async (API_URL) => {
    try {
        const response = await fetch(API_URL, {
            method: 'GET',
            credentials: 'include',
            headers: {
                'Content-Type': 'application/json'
            }
        });

        if (!response.ok) {
            throw new Error(`HTTP error! Status: ${response.status}`);
        }

        // Get the response json from the API
        const data = await response.json();
        if (data.enable_tcp) {
            return data;
        }
        return {websocket_session_count: 0};
    } catch (error) {
        console.error('Error:', error);
        return {websocket_session_count: 0};
    }
};
