{{JS_PREFIX}}_supports_keywords = true;

function {{JS_PREFIX}}_apply_blocks(blocked_users) {
    const comments = document.querySelector('.comment-list');
    if (comments === null || comments.length === 0) {
        console.warn('No comments found, not filtering');
        return;
    }
    {{JS_PREFIX}}_consider_block(comments.querySelectorAll('li.comment'), blocked_users);
}

function {{JS_PREFIX}}_consider_block(comments, blocked_users) {
    if (comments && comments.length && comments.length > 0) {
        for (let i=0; i<comments.length; i++) {
            const comment = comments[i];
            const firstEval = {{JS_PREFIX}}_mark_evaluated(comment);

            if ({{JS_PREFIX}}_includes_block_keyword(comment, firstEval)) {
                comment.parentNode.removeChild(comment);
                continue;
            }
            const userElement = comment.querySelector('cite.fn strong');
            if (userElement !== null) {
                const author = userElement.innerText;
                if (author in blocked_users) {
                    {{JS_PREFIX}}_tally_author_block(author);
                    if (!firstEval) {{JS_PREFIX}}_untally_allow();
                    comment.parentNode.removeChild(comment);
                    continue;
                }

                if (firstEval) {
                    const blockNode = document.createElement('span');
                    blockNode.className = "bubble_block";
                    blockNode.innerHTML = ' [<b><a href="#" onclick="{{JS_PREFIX}}_block_user(\'' + author.replace("'", "\\\'") + '\'); return false;">X</a></b>]&nbsp;&nbsp;';
                    userElement.parentNode.insertBefore(blockNode, userElement.nextSibling);

                    const replies = comment.querySelector('ol.children');
                    if (replies !== null) {
                        {{JS_PREFIX}}_consider_block(replies.querySelector('li.comment'));
                    }
                    {{JS_PREFIX}}_tally_allow();
                }
            }
        }
    }
}
