/*
// Usage
!function(s, h, o, r, t, p, n) {
    if (s.shr) return;
    t = s.shr = function() {
        t.queue.push(arguments)
    };
    if (!s._shr) s._shr = t;
    t.queue = [];
    p = h.createElement(o);
    p.src = r;
    n = h.getElementsByTagName(o)[0];
    n.parentNode.insertBefore(p, n)
}
(window, document, 'script', 'http://pixel.shortpen.test/events.js');
shr('event', 'S-472389325', 'PageView');


<noscript>
    <img height="1" width="1" style="display:none" src="https://pixel.shortpen.test?id=PIXEL_ID&ev=PageView&noscript=1"/>
</noscript>

*/

let shr_pixelid, shr_user, shr_spid, shr_spuid, shr_spsid;
(function (t) {
    t.shr = function (method, pixel_id, value) {
        let uniqid = localStorage.getItem('shr_uid');
        if (!uniqid) {
            uniqid = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
            localStorage.setItem('shr_uid', uniqid);
        }

        let custom_params = {};
        if (method == "init") {
            shr_pixelid = pixel_id;
        } else if (method == "event") {
            custom_params = value == null ? {} : value;
            value = pixel_id;
            pixel_id = shr_pixelid;
        }

        if (shr_spid == null || shr_spuid == null || shr_spsid == null) {
            const urlParams = new URLSearchParams(window.location.search);
            shr_spid = urlParams.get('spid');
            shr_spuid = urlParams.get('spuid');
            shr_spsid = urlParams.get('spsid');

            if (shr_spid) {
                localStorage.setItem(uniqid + '_spid', shr_spid);
                urlParams.delete('spid');
                const newUrl = window.location.pathname +
                    (urlParams.toString() ? '?' + urlParams.toString() : '') +
                    window.location.hash;
                window.history.replaceState({}, document.title, newUrl);
            } else {
                shr_spid = localStorage.getItem(uniqid + '_spid');
            }
            if (shr_spuid) {
                localStorage.setItem(uniqid + '_spuid', shr_spuid);
                urlParams.delete('spuid');
                const newUrl = window.location.pathname +
                    (urlParams.toString() ? '?' + urlParams.toString() : '') +
                    window.location.hash;
                window.history.replaceState({}, document.title, newUrl);
            } else {
                shr_spuid = localStorage.getItem(uniqid + '_spuid');
            }

            if (shr_spsid) {
                localStorage.setItem(uniqid + '_spsid', shr_spsid);
                urlParams.delete('spsid');
                const newUrl = window.location.pathname +
                    (urlParams.toString() ? '?' + urlParams.toString() : '') +
                    window.location.hash;
                window.history.replaceState({}, document.title, newUrl);
            } else {
                shr_spsid = localStorage.getItem(uniqid + '_spsid');
            }
        }

        let custom_params_qs = '';
        if (custom_params != null && Object.keys(custom_params).length > 0) {
            custom_params_qs = '&p=' + btoa(encodeURIComponent(JSON.stringify(custom_params)));
        }

        const urlParams = new URLSearchParams(window.location.search);

        let tevt_expire = localStorage.getItem(uniqid + '_tevt_expire');
        if (tevt_expire && new Date().getTime() > tevt_expire) {
            localStorage.removeItem(uniqid + '_tevt');
            localStorage.removeItem(uniqid + '_tevt_expire');
        }

        let tevt = localStorage.getItem(uniqid + '_tevt');
        if (urlParams.has('tevt') || tevt) {
            custom_params_qs += '&tevt=' + urlParams.get('tevt');
            localStorage.setItem(uniqid + '_tevt', urlParams.get('tevt'));
            localStorage.setItem(uniqid + '_tevt_expire', new Date().getTime() + 10 * 60 * 1000);
        }

        fetch(window.shr_workerurl + method + '/' + pixel_id + '/' + (value == null ? '' : value) + '?spid=' + shr_spid + '&spuid=' + shr_spuid + '&spsid=' + shr_spsid + custom_params_qs + '&f=' + encodeURIComponent(window.location.origin + window.location.pathname))
            .then(function (r) {
                return r.json();
            })
            .catch(function (error) {
                console.error('An error occurred, please try again later or contact support.', error);
            });
    };
    if (t._shr.queue) {
        for (let q in t._shr.queue)
            t.shr.apply(t.shr, t._shr.queue[q]);
    }
})(window);
