let {{JS_PREFIX}}_blocked_users = null;
let {{JS_PREFIX}}_doc_ready = false;
const {{JS_PREFIX}}_request_id = '{{BUBBLE_REQUEST_ID}}';
const {{JS_PREFIX}}_interval = 50;
const {{JS_PREFIX}}_idle_interval = 1000;

function {{JS_PREFIX}}_onReady(callback) {
    const intervalId = window.setInterval(function() {
        if (document.getElementsByTagName('body')[0] !== undefined) {
            {{JS_PREFIX}}_doc_ready = true;
            window.clearInterval(intervalId);
            callback.call(this);
        }
    }, {{JS_PREFIX}}_interval);
}

function {{JS_PREFIX}}_fetch_blocks (do_apply) {
    const requestOptions = { method: 'GET' };
    const blocked_users_url = '/__bubble/api/filter/data/{{BUBBLE_DATA_ID}}/read';
    fetch(blocked_users_url, requestOptions)
        .then(resp => resp.json())
        .then(data => {
            const blocked_users = [];
            for (let i=0; i<data.length; i++) {
                blocked_users.push(data[i]);
            }
            if ({{JS_PREFIX}}_doc_ready) {
                {{JS_PREFIX}}_blocked_users = blocked_users;
                {{JS_PREFIX}}_apply_blocks({{JS_PREFIX}}_blocked_users);
                window.setInterval(function () {
                    {{JS_PREFIX}}_apply_blocks({{JS_PREFIX}}_blocked_users)
                }, {{JS_PREFIX}}_idle_interval);
            } else {
                const intervalId = window.setInterval(function() {
                    if ({{JS_PREFIX}}_doc_ready) {
                        window.clearInterval(intervalId);
                        {{JS_PREFIX}}_blocked_users = blocked_users;
                        {{JS_PREFIX}}_apply_blocks({{JS_PREFIX}}_blocked_users);
                        window.setInterval(function () {
                            {{JS_PREFIX}}_apply_blocks({{JS_PREFIX}}_blocked_users)
                        }, {{JS_PREFIX}}_idle_interval);
                    }
                }, {{JS_PREFIX}}_interval);
            }
        });
}

{{JS_PREFIX}}_fetch_blocks();

function {{JS_PREFIX}}_block_user (author) {
    {{JS_PREFIX}}_blocked_users.push(author);
    {{JS_PREFIX}}_apply_blocks({{JS_PREFIX}}_blocked_users);
    const block_user_url = '/__bubble/api/filter/data/{{BUBBLE_DATA_ID}}/write';
    const requestOptions = {
        method: 'POST',
        body: JSON.stringify({key: author, data: true})
    };
    fetch(block_user_url, requestOptions);
}

{{{APPLY_BLOCKS_JS}}}


{{JS_PREFIX}}_onReady(function() {
    const controlDivId = '{{JS_PREFIX}}_controlDiv';
    let bubbleControlDiv = document.getElementById(controlDivId);
    if (bubbleControlDiv === null) {
        bubbleControlDiv = document.createElement('div');
        bubbleControlDiv.id = controlDivId;
        bubbleControlDiv.style.position = 'fixed';
        bubbleControlDiv.style.bottom = '0';
        bubbleControlDiv.style.right = '0';
        document.getElementsByTagName('body')[0].appendChild(bubbleControlDiv);
    }
    bubbleControlDiv.innerHTML = bubbleControlDiv.innerHTML + '<br/><a href="{{{BUBBLE_HOME}}}/app/UserBlocker/site/{{SITE}}/view/blocked_users"><img width="64" src="/__bubble/api/filter/assets/{{BUBBLE_REQUEST_ID}}/UserBlocker/icon?raw=true"/></a>';
});

