const url = 'https://statistics.webixty.com/api';
const wst = window.wst=async (request) => {

    try {
        request.meta = {
            ...request.meta,
            meta: navigator.userAgent,
            from: document.referrer,
            url: window.location.href,
        };
        const response = await fetch(url+'/set', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                ...request,
                screen: {
                    width: window.screen.width,
                    height: window.screen.height,
                },
                user_id: getWSTCookie('wst'),
            }),
        });
        const data = await response.json();
        setWSTCookie('wst', data.user_id, 30);
        return data;
    } catch (error) {
        console.error('Error sending POST request:', error);
        throw error;
    }
};
const wstE = window.wstE=async (event,request) => {

    try {
        await fetch(url + '/event', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                ...request,
                event:event,
                user_id: getWSTCookie('wst'),
            }),
        });
    } catch (error) {
        console.error('Error sending POST request:', error);
        throw error;
    }
};

const getWSTCookie = (cookieName) => {
    const cookieValue = document.cookie
        .split('; ')
        .find(row => row.startsWith(cookieName + '='));
    return cookieValue ? cookieValue.split('=')[1] : null;
};

const setWSTCookie = (cookieName, cookieValue, expirationDays) => {
    const date = new Date();
    date.setTime(date.getTime() + expirationDays * 24 * 60 * 60 * 1000);
    const expires = `expires=${date.toUTCString()}`;
    document.cookie = `${cookieName}=${cookieValue}; ${expires}; path=/; SameSite=Strict`;
};
