import scroll from './scroll';

const anchors = Array.prototype.slice.call(document.querySelectorAll('a[href^="#"]'))

anchors.forEach((elem) => {
    const href = elem.getAttribute('href')
    if (href.length < 2) {
        return
    }

    elem.addEventListener("click", (e) => {
        e.preventDefault()
        const target = document.getElementById(href.slice(1))
        const viewportOffset = target.getBoundingClientRect();

        scroll(viewportOffset.top, 1000, "easeOutQuart")
    })
});

const nav = document.querySelector('.nav')

// Comment

document.addEventListener('scroll', () => {
    const doc = document.documentElement;
    const top = (window.pageYOffset || doc.scrollTop)  - (doc.clientTop || 0);

    top > (window.innerHeight / 2) ? nav.classList.add('open') : nav.classList.remove('open')
})

!function () {
    let formEnabled = false
    const submit = document.querySelector('.js-submit')
    const form = document.querySelector('.js-require')

    if (!form) {
        return;
    }

    var inputs = Array.prototype.slice.call(form.querySelectorAll('.js-input'))

    var check = (e) => {
        if (inputs.filter(x => !x.value).length) {
            formEnabled = false
            submit.classList.add('disabled')
        } else {
            formEnabled = true
            submit.classList.remove('disabled')
        }
    }

    form.addEventListener('keyup', check)
    form.addEventListener('submit', (e) => {
        !formEnabled && e.preventDefault()
    })
    check()
}()