const channelUid = "{{ channel.uid.value }}";

function initInputField(textBox) {
    textBox.addEventListener('change', (e) => {
        e.preventDefault();
        this.dispatchEvent(new CustomEvent('change', { detail: e.target.value, bubbles: true }));
    });

    textBox.addEventListener('keydown', (e) => {
        if (e.key === 'Enter' && !e.shiftKey) {
            e.preventDefault();
            const message = e.target.value.trim();
            if (message) {
                app.rpc.sendMessage(channelUid, message);
                e.target.value = '';
            }
        }
    });
    textBox.focus();
}

function updateTimes() {
    document.querySelectorAll(".time").forEach((time) => {
        time.innerText = app.timeDescription(time.dataset.created_at);
    });
}

function isElementVisible(element) {
    const rect = element.getBoundingClientRect();
    return (
        rect.top >= 0 &&
        rect.left >= 0 &&
        rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
        rect.right <= (window.innerWidth || document.documentElement.clientWidth)
    );
}

const messagesContainer = document.querySelector(".chat-messages");

let isLoadingExtra = false;

messagesContainer.addEventListener("scroll", () => {
    loadExtra();
});

setInterval(updateTimes, 1000);

app.addEventListener("channel-message", (data) => {
    if (data.channel_uid !== channelUid) {
        if(!isMentionForSomeoneElse(data.message)){
            channelSidebar.notify(data);
        }
    }
});
