{{JS_PREFIX}}_supports_keywords = true;
{{JS_PREFIX}}_idle_interval = 5000;

const {{JS_PREFIX}}_site_host = location.protocol + '//' + window.location.hostname + '/';

function {{JS_PREFIX}}_mobile() {
    const html = Array.from(document.getElementsByTagName('html'));
    if (html.length !== 0) {
        return html[0].className && html[0].className.indexOf(' mobile ') !== -1;
    }
    return false;
}

const {{JS_PREFIX}}_jail = document.createElement('div');
{{JS_PREFIX}}_jail.style.display = 'none';

function {{JS_PREFIX}}_apply_blocks(blocked_users) {
    const adBanner = Array.from(document.getElementsByTagName('iframe')).find(i => i.className && i.className === 'ad-banner');
    if (typeof adBanner !== 'undefined') {
        let adParent = adBanner.parentNode;
        if (adParent != null) {
            adParent.innerHTML = '';
            adParent.style.display = 'none';
        } else {
        }
    }
    const articles = {{JS_PREFIX}}_mobile()
        ? Array.from(document.getElementsByClassName('feed-item'))
        : Array.from(document.getElementsByClassName('feed-shared-update-v2'));
    if (articles === null || articles.length === 0) {
        console.warn('No articles found, not filtering');
        return;
    }
    {{JS_PREFIX}}_consider_block(articles, blocked_users);
}

function {{JS_PREFIX}}_is_valid_author_name(name) {
    return !(name.startsWith('ACoAA') || name.length >= 38);
}

function {{JS_PREFIX}}_author_from_href(linkId, callback) {
    if (typeof linkId === 'undefined' || linkId === null || linkId.length === 0) {
        // console.log('author_from_href: invalid link ID: '+linkId);
        return;
    }
    const link = document.getElementById(linkId);
    if (link === null) {
        // console.log('author_from_href: link with ID '+linkId+' not found');
        return;
    }
    const href = link.href;
    if (typeof href === 'undefined' || href === null) return null;
    let h = href.startsWith({{JS_PREFIX}}_site_host) ? href.substring({{JS_PREFIX}}_site_host.length) : href;
    const qPos = h.indexOf('?');
    if (qPos !== -1) {
        h = h.substring(0, qPos);
    }
    if (h.endsWith('/')) h = h.substring(0, h.length - 1);
    let profile_type = null;

    const mobile = {{JS_PREFIX}}_mobile();
    if (mobile && h.startsWith('mwlite/')) {
        h = h.substring('mwlite/'.length);
    }
    if (h.startsWith('in/')) {
        profile_type = 'in/';
    } else if (h.startsWith('company/')) {
        profile_type = 'company/';
    } else {
        // console.log("author_from_href: skipping (not in/ or company/) href: "+href+', h='+h);
        return;
    }
    const slashPos = h.indexOf('/');
    const name = h.substring(slashPos+1);
    if ({{JS_PREFIX}}_is_valid_author_name(name)) {
        // console.log("author_from_href: found " + name + ' from ' + href);
        callback(linkId, name);
    } else {
        // only chase a link once
        let linkClass = link.className;
        const chaseClass = '{{JS_PREFIX}}_link_chased';
        if (linkClass && linkClass.indexOf(chaseClass) !== -1) {
            return;
        } else {
            link.className = link.className ? link.className + ' '+chaseClass : chaseClass;
        }

        {{JS_PREFIX}}_chase_redirects(link, true, '/voyager/api/identity/profiles/([^/]+)/privacySettings', [1], function (matches) {
            if (typeof matches.length !== 'undefined') {
                for (let i=0; i<matches.length; i++) {
                    const match = matches[i];
                    if (!('1' in match)) continue;
                    const updated_name = matches[i]['1'];
                    if ({{JS_PREFIX}}_is_valid_author_name(updated_name)) {
                        const realLink = document.getElementById(linkId);
                        if (realLink === null) {
                            console.log('author_from_href: link with id '+linkId+' seems to have disappeared from the document');
                            return;
                        }
                        link.href = {{JS_PREFIX}}_site_host + profile_type + updated_name;
                        console.log('author_from_href: updated link.href from '+href+' to: '+link.href);
                        callback(linkId, updated_name);
                        return;
                    }
                }
            }
        });
    }
}

function {{JS_PREFIX}}_remove_article_from_dom(article) {
    // todo: does this work?
    article.parentNode.removeChild(article);
}

function {{JS_PREFIX}}_create_block_control(article, authorName, articleLink) {
    let linkClass = articleLink.className;
    if (linkClass && linkClass.indexOf('{{JS_PREFIX}}_link_decorated') !== -1) {
        return null;
    } else {
        articleLink.className = articleLink.className ? articleLink.className + ' {{JS_PREFIX}}_link_decorated' : '{{JS_PREFIX}}_link_decorated';
    }
    const imgHolder = {{JS_PREFIX}}_create_block_img();
    const blockSpan = document.createElement('span');
    const blockLink = document.createElement('a');
    blockLink.style.zIndex = '{{APP_CONTROLS_Z_INDEX}}'
    blockLink.style.cursor = 'pointer';
    blockLink.addEventListener("click", function (e) {
        {{JS_PREFIX}}_remove_article_from_dom(article, authorName);
        {{JS_PREFIX}}_block_user(authorName);
        e.stopPropagation();
        e.preventDefault();
        return false;
    });
    blockLink.appendChild(imgHolder);
    blockSpan.appendChild(document.createTextNode('\u00A0\u00A0'));
    blockSpan.appendChild(blockLink);
    blockSpan.id = 'blockSpan_'+{{JS_PREFIX}}_uuidv4();
    // console.log('adding block control on '+authorName);
    return blockSpan;
}

function {{JS_PREFIX}}_hash_url(url) { return btoa(url).replaceAll('=', ''); }

function {{JS_PREFIX}}_is_ad(article) {
    const mobile = {{JS_PREFIX}}_mobile();
    return (mobile && article.getAttribute('data-is-sponsored') && article.getAttribute('data-is-sponsored') !== "false")
    || (article.innerHTML.indexOf('<span>Promoted</span>') !== -1)
    || (article.innerHTML.indexOf('<span dir="ltr">Promoted</span>') !== -1);
}

function {{JS_PREFIX}}_find_append_span(link) {
    const mobile = {{JS_PREFIX}}_mobile();
    if (!mobile) {
        let authorSpans = Array.from(link.getElementsByClassName('feed-shared-actor__name'));
        if (authorSpans.length > 0) {
            return authorSpans[0];
        } else {
            return Array.from(link.getElementsByTagName('span'))
                .find(s => s.getAttribute('dir') === 'ltr' || s.getAttribute('data-entity-type'));
        }
    } else {
        const ltrSpan = Array.from(link.getElementsByTagName('span'))
            .find(s => s.getAttribute('dir') === 'ltr' || s.getAttribute('data-entity-type'));
        if (ltrSpan) return ltrSpan;
        if (link.className && link.className.indexOf('profile-link') !== -1) {
            return link.lastChild;
        }
    }
}

function {{JS_PREFIX}}_consider_block(articles, blocked_users) {
    const mobile = {{JS_PREFIX}}_mobile();
    if (articles && articles.length && articles.length > 0) {
        for (let i=0; i<articles.length; i++) {
            const article = articles[i];
            if ({{JS_PREFIX}}_is_ad(article)) {
                {{JS_PREFIX}}_tally_author_block({{PAGE_PREFIX}}_msg_or_default({{JS_PREFIX}}_messages, 'web_advertOrOtherBlock', 'ad/other'));
                {{JS_PREFIX}}_remove_article_from_dom(article);
                continue;
            }
            const firstEval = {{JS_PREFIX}}_mark_evaluated(article);
            if ({{JS_PREFIX}}_includes_block_keyword(article, firstEval)) {
                {{JS_PREFIX}}_remove_article_from_dom(article);
                continue;
            }
            const articleLinks = mobile
                ? Array.from(article.getElementsByTagName('a')).filter(a => !a.hasAttribute('aria-hidden'))
                : Array.from(article.getElementsByTagName('a'));
            // console.log('consider_block: found '+articleLinks.length+' articleLinks');
            for (let j=0; j<articleLinks.length; j++) {
                const articleLink = articleLinks[j];
                if (typeof articleLink === 'undefined' || articleLink === null || typeof articleLink.href === 'undefined') {
                    // console.log('consider_block: skipping invalid articleLink: '+JSON.stringify(articleLink));
                    continue;
                }
                if (typeof articleLink.id === 'undefined' || articleLink.id === null || articleLink.id.length === 0) {
                    articleLink.id = {{JS_PREFIX}}_uuidv4();
                }
                const href = articleLink.href;
                if (href === null || href.trim().length === 0) continue;

                let examinedClass = articleLink.className;
                const seenClass = '{{JS_PREFIX}}_link_examined_'+{{JS_PREFIX}}_hash_url(href)+'_'+articleLink.id;
                let seenBefore = false;
                if (examinedClass && examinedClass.indexOf(seenClass) !== -1) {
                    seenBefore = true;
                } else {
                    articleLink.className = articleLink.className ? articleLink.className + ' '+seenClass : seenClass;
                }

                {{JS_PREFIX}}_author_from_href(articleLink.id, function (linkId, author) {
                    if (author === null) return;

                    const realLink = document.getElementById(linkId);
                    if (realLink === null) {
                        // console.log('consider_block: link with id '+linkId+' seems to have disappeared from the document');
                        return;
                    }

                    // console.log('consider_block: examining linkId with author='+author);
                    if (author in blocked_users) {
                        {{JS_PREFIX}}_tally_author_block(author);
                        if (!firstEval) {{JS_PREFIX}}_untally_allow();
                        {{JS_PREFIX}}_remove_article_from_dom(article);

                    } else if (!seenBefore) {
                        let appendToSpan = {{JS_PREFIX}}_find_append_span(realLink);
                        if (!appendToSpan) {
                            // console.log('consider_block: no span found to append to for author '+author);
                            return;
                        }

                        let b = {{JS_PREFIX}}_create_block_control(article, author, realLink);
                        if (b !== null) {
                            // console.log('consider_block: inserting span='+b.id+' for article by '+author);
                            appendToSpan.parentNode.appendChild(b);
                            {{JS_PREFIX}}_tally_allow();
                        } else {
                            // console.log('consider_block: create_block_control returned null for author '+author)
                        }
                    }
                });
            }
        }
    }
}
