(function() {
    const Id = "block-" + Date.now();

    var params = {
        apiKey: 'ea1fd38594887a9c7d18f582c8ad7ce0',
        utmSource: '',
        utmCampaign: '',
        utmMedium: '',
        slug: {},
        tag: '',
        tagSelector: '.gsghdjjs',
        tagSelector2: '',
        outputSelector: '' ? '' : '#' + Id,
        addToAll: 0,
        btnPosition: 'beforeend',
    };

    if (!'') document.write('<div id="' + Id + '"></div>');
    var elem = document.querySelector(params.outputSelector);
    if (elem && elem.dataset.tag) {
        params.tag = elem.dataset.tag
    }
    var counter = 0;

    function MSetup() {
        function getData(url, option, callback, errorFunc, errorUrl) {
            var xhr = new XMLHttpRequest();
            xhr.open("GET", url, true);
            xhr.onreadystatechange = function() {
                if (xhr.readyState != 4) {
                    return;
                }
                if (xhr.status != 200) {
                    console.log(xhr.status + ": " + xhr.statusText);
                } else {
                    if (option && xhr.responseText && xhr.responseText !== "") {
                        params[option] = JSON.parse(xhr.responseText);
                    }
                    if (xhr.responseText &&
                        xhr.responseText !== "-" &&
                        params[option] !== null &&
                        typeof callback === "function"
                    ) {
                        callback();
                    }
                    if ((xhr.responseText === "-" || xhr.responseText === "") &&
                        typeof errorFunc === "function" &&
                        errorUrl
                    ) {
                        errorFunc(errorUrl, option, callback);
                    }
                }
            };
            xhr.send();
        }

        function renderBtn() {
            var styles =
                '<style>@import url("https://fonts.googleapis.com/css?family=Open+Sans:400,700&subset=cyrillic");#' +
                Id +
                '{ font-family:"Open Sans",sans-serif;line-height:1;text-align:left;margin-bottom:0px;margin-top:0px;margin-left:0px;margin-right:0px;}#' +
                Id +
                " p{margin-bottom:13px;font-size:13px;}#" +
                Id +
                ' a{cursor:pointer;text-align:center;display:inline-block;background:url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAABpElEQVRoge2a7W2DMBRFD9mAFdIRMkNHSEboTszSrpARzAjJCLc/cEqpDNjwwEj1kaxEDvK7Jx/Y4CAJo3aR5DSP88ea1K0kYYQDzpHHtsCbRVFLgdSBKouiJ4tBclIEclMEclMEclMEclMEclMEclMEchMj8EG31p9rqcyN9/C1J4m5oDG74lnI5IVPzCfwNAqyhNnaMQLvMQNtwNPXniRG4M7+Eq/w97kDY89Ce0pEh4e00+geEknhIX0e2FIiOTwsm8i2kFgUHpbPxJYSi8PDuqWEhcSq8LB+LbRGYnV4sFnMLZEwCQ92q9EUCbPwYLucjpEwDQ+dwIXu3n7MkjnUmkiJUPhmRV0HXIjcVZmjCezWPH69/gjsyjQGdR0Gg7wISTiFt5Qswkt+MEv+SoSaWXjJXkCaljANL20jIEmfkq7qg199nzmVZLfLl4N/cVtlD1rgyz/G9Pds8b1MxEmq1f1WavXz0lj/gCN8Ai39zP2kf7fH+gccQeAM1P55Tf93hbH+AUc5C7W+nRkGHev/oZKU8ieNo9GegBtTv/Lj0gK3bywIffbxvDDRAAAAAElFTkSuQmCC") no-repeat 20px 50% #46bb62;border:0px solid #fff;border-radius:0px;color:#fff;height:70px;width:390px;box-sizing:border-box;box-shadow:0 1px 2px rgba(0,0,0,.22);font-size:20px;font-weight:700;overflow: hidden;text-decoration:none !important;padding:13px 10px 13px 70px;text-overflow:ellipsis;white-space:nowrap;}#' +
                Id +
                " a:hover{opacity:.9}#" +
                Id +
                " span{font-size:13px;color:#fff;font-weight:400;display:block;opacity:1;text-transform:none;padding:10px 0 0 0;}</style>";

            var html =
                '<p>Рекомендуем использовать MultiSetup:</p><a onclick="window.goNext()">Скачать ' +
                decodeURI(params.slug.name) +
                "<span>" + params.slug.slug + "_msetup.exe</span></a>";

            if ('') {
                html = '<div id="' + Id + '">' + html + '</div>'
            }

            document.head.insertAdjacentHTML("beforeBegin", styles);

            if (params.addToAll) {
                var elems = document.querySelectorAll(params.outputSelector);
                elems.forEach(function(item) {
                    item.insertAdjacentHTML(params.btnPosition, html);
                });
            } else {
                elem.insertAdjacentHTML(params.btnPosition, html);
            }
        }

        window.goNext = function() {
            location.href =
                params.slug.domain +
                "api/getbundle/?partner_apikey=" +
                params.apiKey +
                "&program_slug=" +
                params.slug.slug +
                "&utm_campaign=" +
                params.utmCampaign +
                "&utm_term=" +
                params.slug.slug +
                "&utm_source=" +
                params.utmSource +
                "&utm_medium=" +
                params.utmMedium +
                "&utm_content=" +
                Date.now().toString().slice(0, 6) +
                "&rfr=" +
                encodeURIComponent(location.href)
        };

        if (0 !== 0) {
            var tag1 = params.tag;
            if (!tag1) {
                tag1 = document.querySelector(params.tagSelector);
                tag1 = tag1 ? tag1.innerText : '';
            }
            var slugUrl1 = tag1 ?
                encodeURI(
                    "https://determineapp.com/api/getslugv3?partner_apikey=" +
                    params.apiKey +
                    "&raw=" +
                    tag1 +
                    "&tag=" +
                    params.tag +
                    "&rfr="
                ) + encodeURIComponent(location.href) :
                "";

            if (elem && slugUrl1) getData(slugUrl1, "slug", renderBtn);
        } else {
            var tagSelectors = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
            var tagList = [];

            for (var sel of tagSelectors) {
                var elems = Array.prototype.slice.call(document.querySelectorAll(sel));
                var len = 3 - tagList.length;
                var arr = [];
                if (elems.length > len) {
                    arr = elems.slice(0, len);
                } else if (elems.length > 0) {
                    arr = elems;
                }
                var tags = arr.map(function(item) {
                    return encodeURI(
                        "https://determineapp.com/api/getslugv3?partner_apikey=" +
                        params.apiKey +
                        "&raw=" +
                        item.innerHTML +
                        "&tag=" +
                        params.tag +
                        "&rfr="
                    ) + encodeURIComponent(location.href);
                });
                tagList = tagList.concat(tags);
                if (tagList.length > 2) break;
            }

            if (tagList.length > 0) getData(tagList[counter], "slug", checkSlug);
        }

        function checkSlug() {
            if (params.slug.name) {
                renderBtn();
            } else if (counter < tagList.length) {
                counter++;
                getData(tagList[counter], "slug", checkSlug);
            }
        }
    }

    function isWindows() { return navigator.platform.indexOf('Win') > -1 }

    if (isWindows()) MSetup();
})();
