const scroll = (to, duration) => {
    if (duration <= 0) {
        window.scrollTo(0, to);
    } else {
        let difference = to - window.pageYOffset;

        let perTick = difference / duration * 10;
        $(this).scrollToTimerCache = setTimeout(() => {
            if (! isNaN(parseInt(perTick, 10))) {
                window.scrollTo(0, window.pageYOffset + perTick);
                scroll(to, duration - 10);
            }
        }, 10);
    }
};

export function smoothScroll(target, duration = 300, cb) {
    let targetSelector = '';
    const hrefTarget = $(target).attr('href');

    // We need to handle href and name since spy navigation is using name as target
    if (hrefTarget.length) {
        targetSelector = 'a[name="' + hrefTarget.split('#')[1] + '"]';
        if (! $(targetSelector).length) {
            targetSelector = hrefTarget;
        }
    } else {
        const nameTarget = $(target).attr('name');
        if (nameTarget.length) {
            targetSelector = 'a[name="' + nameTarget + '"]';
        } else {
            console.warn('No target for scroll');
            return;
        }
    }

    const to = $(targetSelector).offset().top;
    scroll(to, duration);
    if (cb) {cb();}
}

export default function registerSmoothScrolling() {
    // TODO: Get rid of .sc-smooth-scroll class once all teams have migrated to data attribute
    $(document).on('click', '.sc-smooth-scroll, [data-smooth-scroll]', (e) => {
        e.preventDefault();
        smoothScroll(e.currentTarget);
        return false;
    });
}

const scroll = (to, duration) => {
    if (duration <= 0) {
        window.scrollTo(0, to);
    } else {
        let difference = to - window.pageYOffset;

        let perTick = difference / duration * 10;
        $(this).scrollToTimerCache = setTimeout(() => {
            if (! isNaN(parseInt(perTick, 10))) {
                window.scrollTo(0, window.pageYOffset + perTick);
                scroll(to, duration - 10);
            }
        }, 10);
    }
};

export function smoothScroll(target, duration = 300, cb) {
    let targetSelector = '';
    const hrefTarget = $(target).attr('href');

    // We need to handle href and name since spy navigation is using name as target
    if (hrefTarget.length) {
        targetSelector = 'a[name="' + hrefTarget.split('#')[1] + '"]';
        if (! $(targetSelector).length) {
            targetSelector = hrefTarget;
        }
    } else {
        const nameTarget = $(target).attr('name');
        if (nameTarget.length) {
            targetSelector = 'a[name="' + nameTarget + '"]';
        } else {
            console.warn('No target for scroll');
            return;
        }
    }

    const to = $(targetSelector).offset().top;
    
    scroll(to, duration);

    if (cb) {cb();}
}

export default function registerSmoothScrolling() {
    // TODO: Get rid of .sc-smooth-scroll class once all teams have migrated to data attribute
    $(document).on('click', '.sc-smooth-scroll, [data-smooth-scroll]', (e) => {
        e.preventDefault();
        smoothScroll(e.currentTarget);
        return false;
    });
}

test commit