import * as bootstrap from 'bootstrap'

document.querySelectorAll('[data-bs-toggle="popover"]')
    .forEach(popover => {
        new bootstrap.Popover(popover)
    })

const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]')
const tooltipList = [...tooltipTriggerList].map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl))

window.addEventListener('load', function() {

    seenPosts = document.querySelectorAll('.seen-overlay');

    for (let i = 0; i < seenPosts.length; i++) {
        let hash = seenPosts[i].getAttribute('data-hash');

        fetch('/api/profile/seen_posts/' + hash, {
            method: 'GET'
        })
        .then(response => {
            if (response.ok) {
                // Handle the response here
                return response.json(); // Parse the response JSON if applicable
            } else {
                throw new Error('Network response was not ok.');
            }
        })
        .then(data => {
            // Access the JSON data here
            if (data.hasOwnProperty('seen')) {
                if (data.seen === true) {
                    seenPosts[i].innerText = 'Viewed';
                    seenPosts[i].setAttribute('data-seen', 'true');
                };
            } else {
                console.log('No data.seen in response for hash:', hash, data);
            }
        })
        .catch(error => {
            // Handle errors here
            console.error('Error:', error);
        });
    };
});