import { getCookie, setCookie } from "./cookie.js"

/*
 * Newsletter modal
 */

// Get the modal
const modal = document.querySelector(".modal-menu");

const showBtn = document.querySelector(".show-modal");

// Get the <span> element that closes the modal
const closeBtn = document.querySelector(".close-modal");

const oneDay = 1 * 24 * 60 * 60 * 1000

// When the user clicks on <span> (x), close the modal
closeBtn.addEventListener("click", event => {
    modal.style.display = "none";
    setCookie('newsletter-modal', 'true', oneDay)
})

showBtn.addEventListener("click", event => {
    modal.style.display = "block";
    setCookie('newsletter-modal', 'true', oneDay)
})

const scrollFunction = () => {
    let modalDismissed = getCookie('newsletter-modal')
    if (modalDismissed != 'true') {
        if (document.body.scrollTop > 600 || document.documentElement.scrollTop > 600) {
            modal.style.display = "block";
        }
    }
}

window.onscroll = () => {
    scrollFunction();
};

// When the user clicks anywhere outside of the modal, close it
window.addEventListener("click", event => {
    if (event.target == modal) {
        modal.style.display = "none";
    }
    setCookie('newsletter-modal', 'true', oneDay)
});
