/**
 * @author Wuner
 * @date 2020/9/21 11:36
 * @description
 */
import { raf, cancel } from "../utils/dom/raf";
import scrollUtils from "../utils/dom/scroll";
var scrollLeftRafId;
export function scrollLeftTo(scroller, to, duration) {
  cancel(scrollLeftRafId);
  var count = 0;
  var from = scroller.scrollLeft;
  var frames = duration === 0 ? 1 : Math.round(duration * 1000 / 16);

  function animate() {
    scroller.scrollLeft += (to - from) / frames;

    if (++count < frames) {
      scrollLeftRafId = raf(animate);
    }
  }

  animate();
}
export function scrollTopTo(scroller, to, duration, callback) {
  var current = scrollUtils.getScrollTop(scroller);
  var isDown = current < to;
  var frames = duration === 0 ? 1 : Math.round(duration * 1000 / 16);
  var step = (to - current) / frames;

  function animate() {
    current += step;

    if (isDown && current > to || !isDown && current < to) {
      current = to;
    }

    scrollUtils.setScrollTop(scroller, current);

    if (isDown && current < to || !isDown && current > to) {
      raf(animate);
    } else if (callback) {
      raf(callback);
    }
  }

  animate();
}