import smoothscroll from 'smoothscroll-polyfill';
smoothscroll.polyfill();

document.addEventListener('DOMContentLoaded',function() {
    const button = document.querySelector(".scroll-form");

    const runScroll = function(el, addOffset) {
        const offsetTop = document.querySelector(el).offsetTop;
        let off = 0;
        
        if (addOffset) {
            off = addOffset;
        } 
        
        console.log(off);
        scroll({
            top: offsetTop - 200 - off,
            behavior: "smooth"
        })
    };

    const clickHandler = function(e) {
        e.preventDefault();
        runScroll('.js-target-scroll');
    };
    
    if (button) {
        button.addEventListener('click', clickHandler);        

        if (document.body.classList.contains('run-scroll')) {
            setTimeout(runScroll, 2000);
        }
    }
    

    const gtt = document.querySelectorAll("[data-target]");
    
    if (gtt.length > 0) {
        const action = function(e) {
                      
            const target = e.currentTarget.dataset.target;
            const addOffset = e.currentTarget.dataset.offset;

            runScroll(target, addOffset);
  
        	e.preventDefault() ? e.preventDefault() : e.preventDefault = false;
        };


        for (let i = 0; i < gtt.length; i++) {
            gtt[i].addEventListener('click', action);
        }
    }
    
}, false);