{{JS_PREFIX}}_supports_keywords = true;

const {{JS_PREFIX}}_site_host = location.protocol + '//' + window.location.hostname + '/';
const {{JS_PREFIX}}_old = window.location.hostname === 'old.reddit.com';

{{JS_PREFIX}}_idle_interval = {{JS_PREFIX}}_old ? 5000 : 8000;

function {{JS_PREFIX}}_create_block_control(comment, authorName) {
    let blockTarget = null;
    if ({{JS_PREFIX}}_old) {
        blockTarget = document.createElement('strong');
        blockTarget.style.fontWeight = 'bolder';
        blockTarget.style.color = 'red';
        blockTarget.appendChild(document.createTextNode(' [X] '));
    } else {
        blockTarget = {{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_comment(comment);
        {{JS_PREFIX}}_block_user(authorName);
        e.stopPropagation();
        e.preventDefault();
        return false;
    });
    blockLink.appendChild(blockTarget);
    blockSpan.appendChild(document.createTextNode('\u00A0\u00A0'));
    blockSpan.appendChild(blockLink);
    if ({{JS_PREFIX}}_old) {
        blockSpan.appendChild(document.createTextNode('\u00A0\u00A0'));
    }
    blockSpan.id = 'blockSpan_'+{{JS_PREFIX}}_uuidv4();
    // console.log('adding block control on '+authorName);
    return blockSpan;
}

function {{JS_PREFIX}}_remove_comment(comment) {
    if ({{JS_PREFIX}}_old) {
        comment.parentNode.removeChild(comment);
        return;
    }
    let paddingLeft = comment.parentNode && comment.parentNode.style ? comment.parentNode.style.paddingLeft : null;
    if (paddingLeft && paddingLeft.endsWith('px')) {
        paddingLeft = paddingLeft.substring(0, paddingLeft.length-2);
    } else {
        return;
    }
    let commentWrapper = comment.parentNode.parentNode.parentNode;
    let wrappersToRemove = [commentWrapper];
    let nextCommentWrapper = commentWrapper.nextElementSibling ? commentWrapper.nextElementSibling : null;
    while (nextCommentWrapper !== null) {
        const nextPadding = Array.from(nextCommentWrapper.getElementsByTagName('div'))
            .find(w => w.style && w.style.paddingLeft && w.style.paddingLeft.length && w.style.paddingLeft.length > 0 && w.style.paddingLeft.endsWith('px'));
        if (nextPadding) {
            // console.log('nextPadding.style.paddingLeft='+nextPadding.style.paddingLeft+', nextPadding.style.paddingLeft.substring(nextPadding.style.paddingLeft.length-2) ='+nextPadding.style.paddingLeft.substring(0, nextPadding.style.paddingLeft.length-2));
            const nextPadAmount = parseInt(nextPadding.style.paddingLeft.substring(0, nextPadding.style.paddingLeft.length-2));
            if (nextPadAmount > paddingLeft) {
                // console.log('startPad='+paddingLeft+', nextPadAmount='+nextPadAmount+', adding wrapper to remove');
                wrappersToRemove.push(nextCommentWrapper);
                nextCommentWrapper = nextCommentWrapper.nextElementSibling ? nextCommentWrapper.nextElementSibling : null;
            } else {
                // console.log('startPad='+paddingLeft+', nextPadAmount='+nextPadAmount+', stopping removals');
                break;
            }
        } else {
            break;
        }
    }
    wrappersToRemove.forEach(w => w.parentNode.removeChild(w));
}

function {{JS_PREFIX}}_apply_blocks(blocked_users) {
    let currentUser = null;
    try {
        currentUser = {{JS_PREFIX}}_old
            ? document.getElementsByClassName('user')[0].getElementsByTagName('a')[0].innerText
            : Array.from(document.getElementsByClassName('header-user-dropdown')[0].getElementsByTagName('span'))[3].innerText;
    } catch (e) {
        // console.log('no current user');
    }

    const userPrefix = {{JS_PREFIX}}_site_host + 'user/';
    const authors = Array.from(document.getElementsByTagName('a')).filter(a => a.href.indexOf('/user/') !== -1);
    authors.forEach(author => {
        let authorName = author.href;
        if (!authorName.startsWith(userPrefix)) return;
        authorName = authorName.substring(userPrefix.length);
        if (authorName.endsWith('/')) authorName = authorName.substring(0, authorName.length-1);

        let node = author.parentNode;
        let comment = null;
        const commentClass = {{JS_PREFIX}}_old ? ' comment' : 'Comment ';
        for (let i=0; i<10; i++) {
            if (node.parentNode === null || node.parentNode === document) break;
            node = node.parentNode;
            if (node.className && node.className.indexOf(commentClass) !== -1) {
                comment = node;
                break;
            }
        }

        if (comment === null || comment.parentNode === null) return;
        const firstEval = comment.className.indexOf('{{JS_PREFIX}}_bubble_evaluated') === -1;
        if (firstEval) {
            comment.className = comment.className + ' {{JS_PREFIX}}_bubble_evaluated';
        }
        const authorBlocked = authorName in blocked_users;
        if (authorBlocked || {{JS_PREFIX}}_includes_block_keyword(comment, firstEval)) {
            if (authorBlocked) {
                {{JS_PREFIX}}_tally_author_block(authorName);
                if (!firstEval) {{JS_PREFIX}}_untally_allow();
            }
            {{JS_PREFIX}}_remove_comment(comment);
            return;
        }

        if (author.className.indexOf('{{JS_PREFIX}}_bubble_has_icon') === -1) {
            if (authorName !== currentUser) {
                let b = {{JS_PREFIX}}_create_block_control(comment, authorName);
                if ({{JS_PREFIX}}_old) {
                    author.parentNode.insertBefore(b, author);
                } else {
                    author.appendChild(b);
                }
            }
            author.className = author.className + ' {{JS_PREFIX}}_bubble_has_icon';
            {{JS_PREFIX}}_tally_allow();
        }
    });
}
