{{JS_PREFIX}}_supports_keywords = true;

function {{JS_PREFIX}}_getElementsByXPath(xpath, parent) {
  let results = [];
  let query = document.evaluate(xpath, parent || document,
  null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  for (let i = 0, length = query.snapshotLength; i < length; ++i) {
    results.push(query.snapshotItem(i));
  }
  return results;
}

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

function {{JS_PREFIX}}_apply_blocks(blocked_users) {
    const authors = {{JS_PREFIX}}_getElementsByXPath('//article//a');
    const sitePrefix = {{JS_PREFIX}}_site_host;
    for (let i=0; i<authors.length; i++) {
        const author = authors[i];

        if (author.href && author.href.startsWith(sitePrefix)
            && author.href.indexOf('?', sitePrefix.length) === -1
            && author.href.indexOf('/', sitePrefix.length) === -1
            && author.className.indexOf('_bubble_blocked') === -1) {

            const firstEval = {{JS_PREFIX}}_mark_evaluated(author);

            const authorName = author.href.substring(sitePrefix.length);
            let node = author;
            // walk parents until we find the article, then go up 3 more divs
            while (node.tagName.toUpperCase() !== 'ARTICLE') {
                node = node.parentNode;
                if (node === null) break;
            }
            if (node === null) continue;
            for (let p = 0; p < 3; p++) {
                node = node.parentNode;
                if (node === null) break;
            }
            if (node === null || node.parentNode === null) continue;
            const authorBlocked = authorName in blocked_users;
            if (authorBlocked || {{JS_PREFIX}}_includes_block_keyword(node, firstEval)) {
                if (authorBlocked) {
                    if ({{JS_PREFIX}}_mark_removed(node)) {{JS_PREFIX}}_tally_author_block(authorName);
                    if (!firstEval) {{JS_PREFIX}}_untally_allow();
                }
                console.log('removing post by author: ' + authorName);
                try {
                    author.className = author.className + ' _bubble_blocked';
                    node.style = 'display: none';
                } catch (e) {
                    console.log('error removing post by author: ' + authorName+': '+e);
                }

            } else {
                // walk up until we find the tweet
                let tweet = author;
                while (tweet.getAttribute('data-testid') === 'undefined' || tweet.getAttribute('data-testid') === null || tweet.getAttribute('data-testid') !== 'tweet') {
                    if (tweet.parentNode === document) {
                        tweet = null;
                        break;
                    } else {
                        tweet = tweet.parentNode;
                    }
                }
                if (tweet === null) {
                    console.log('tweet node not found for author: '+authorName);
                    continue;
                } else {
                    // console.log('FOUND tweet node for author: '+authorName);
                    Array.from(tweet.getElementsByTagName('a')).forEach(a => {
                        if (a.href && a.href.indexOf('https://t.co/') !== -1) {
                            {{JS_PREFIX}}_chase_redirects(a);
                        }
                    });
                }

                // have we visited this tweet before?
                if (tweet.className.indexOf('{{JS_PREFIX}}_bubble_block') === -1) {
                    // console.log('VISITING tweet node for author: '+authorName);
                    const authorWrapper = author.parentNode.parentNode;
                    const authorDiv = authorWrapper.firstChild;
                    if (typeof authorDiv.tagName !== 'undefined' && authorDiv.tagName.toUpperCase() === 'DIV') {
                        const blockControl = document.createElement('div');
                        blockControl.style.textAlign = 'center';
                        tweet.className = tweet.className += ' {{JS_PREFIX}}_bubble_block';

                        const line = document.createElement('hr');
                        const imgHolder = document.createElement('img');
                        imgHolder.src = {{JS_PREFIX}}_asset_img_url('icon');
                        imgHolder.width = 32;

                        const blockLink = document.createElement('a');
                        blockLink.addEventListener("click", function (event) {
                            {{JS_PREFIX}}_block_user(authorName);
                            return false;
                        });
                        blockLink.appendChild(line);
                        blockLink.appendChild(imgHolder);
                        blockControl.appendChild(blockLink);

                        authorDiv.setAttribute('white-space', 'no-wrap');
                        authorDiv.parentNode.appendChild(blockControl);
                        {{JS_PREFIX}}_tally_allow();
                    }
                }
            }
        }
    }
}
