/**
 *  * 
 * 
 * @param {Object} options ref  // speed 滚动速度 默认30
 * @param {Function} cb 鼠标移出回调，返回新的定时器对象
 * 初始化之后只需加载一次
 */
export function vsTableRoll(options = {}, cb) {

    const {
        ref,
        speed = 30
    } = options;
    const bodyWrapper = ref;
    let interval = null;

    interval = rollInterval(bodyWrapper, speed, cb)

    bodyWrapper.addEventListener('mouseenter', mouseenterListener)

    bodyWrapper.addEventListener('mouseleave', mouseleaveListener)

    function mouseenterListener() {
        clearInterval(interval)
    }

    function mouseleaveListener() {
        interval = rollInterval(bodyWrapper, speed, cb);
    }
}



function rollInterval(bodyWrapper, speed, cb) {
    const interval = setInterval(() => {
        bodyWrapper.scrollTop += 1
        if (bodyWrapper.clientHeight + bodyWrapper.scrollTop == bodyWrapper.scrollHeight) {
            bodyWrapper.scrollTop = 0
        }
    }, speed)

    if (cb && typeof cb === 'function') {
        cb(interval)
    }

    return interval
}