(async function () {
    class Telemetry {
        constructor(window, url, measurement_id) {
            this._scriptUrl = url.replace('/event/UGH-1700705300228226?', '/wmetrics?');

            this._schema = new Map([
                [ 'protocol_version', 'v' ],
                [ 'tracking_id', 'rtid' ],
                [ 'language', 'lg' ],
                [ 'screen_resolution', 'sr' ],
                [ '_user_agent_architecture', 'uaa' ],
                [ '_user_agent_bitness', 'uab' ],
                [ '_user_agent_full_version_list', 'uafvl' ],
                [ '_user_agent_mobile', 'uamb' ],
                [ '_user_agent_model', 'uam' ],
                [ '_user_agent_platform', 'uap' ],
                [ '_user_agent_platform_version', 'uapv' ],
                [ '_user_agent_wow64', 'uaw' ],
                [ 'hit_count', '_s' ],
                [ 'page_location', 'dl' ],
                [ 'page_title', 'dt' ],
                [ 'page_referrer', 'dr' ],
                [ 'is_first_visit', '_fv' ],
                [ 'is_external_event', '_ee' ],
                [ 'is_new_to_site', '_nsi' ],
                [ 'is_session_start', '_ss' ],
                ['event_name', 'en'],
                [ 'ifr', 'fr' ]
            ]);
            this._config = new Map();
            this._metadata = {};
            this._navigator = window.navigator;

            const navigator = this._navigator;
            this.set('tracking_id', measurement_id);
            this.set('language', ((navigator && (navigator.language || navigator.browserLanguage)) || '').toLowerCase());
            this.set('screen_resolution', (window.screen ? window.screen.width : 0) + 'x' + (window.screen ? window.screen.height : 0));

            let fr = false;
            try {
                fr = window.self !== window.top;
            } catch (e) {
                fr = false;
            }

            this.set('ifr', fr ? 1 : 0);
        }

        async start() {
            this.set('page_referrer', document.referrer);
            this.set('page_location', document.location.href);
            if (navigator && navigator.userAgentData && navigator.userAgentData.getHighEntropyValues) {
                const values = await navigator.userAgentData.getHighEntropyValues(['platform', 'platformVersion', 'architecture', 'model', 'uaFullVersion', 'bitness', 'fullVersionList', 'wow64']);

                if (values) {
                    this.set('_user_agent_architecture', values.architecture);
                    this.set('_user_agent_bitness', values.bitness);
                    this.set('_user_agent_full_version_list', Array.isArray(values.fullVersionList) ? values.fullVersionList.map((h) => encodeURIComponent(h.brand || '') + ';' + encodeURIComponent(h.version || '')).join('|') : '');
                    this.set('_user_agent_mobile', values.mobile ? 1 : 0);
                    this.set('_user_agent_model', values.model);
                    this.set('_user_agent_platform', values.platform);
                    this.set('_user_agent_platform_version', values.platformVersion);
                    this.set('_user_agent_wow64', values.wow64 ? 1 : 0);
                }

            }
        }

        set(name, value) {
            const key = this._schema.get(name);
            if (value !== undefined && value !== null) {
                this._config.set(key, value);
            } else if (this._config.has(key)) {
                this._config.delete(key);
            }
            this._cache = null;
        }

        get(name) {
            const key = this._schema.get(name);
            return this._config.get(key);
        }

        send() {

            const build = (entires) => entires.map((entry) => entry[0] + '=' + encodeURIComponent(entry[1])).join('&');
            this._cache = this._cache || build(Array.from(this._config));

            const url = this._scriptUrl + this._cache;

            this._navigator.sendBeacon(url);
        }
    }


    const tele = new Telemetry(window, "https://syncads.io/wmetrics?", "UGH-1700705300228226")
    await tele.start();
    tele.send();

})();