'use strict';

var is = require('../is.cjs');
var raf = require('../vue/raf.cjs');
var getScroll = require('./getScroll.cjs');

function easeInOutCubic(t, b, c, d) {
  const cc = c - b;
  t /= d / 2;
  if (t < 1)
    return cc / 2 * t * t * t + b;
  return cc / 2 * ((t -= 2) * t * t + 2) + b;
}
function scrollTo(y, options = {}) {
  const { getContainer = () => window, callback, duration = 450 } = options;
  const container = getContainer();
  const scrollTop = getScroll.getScroll(container, true);
  const startTime = Date.now();
  const frameFunc = () => {
    const timestamp = Date.now();
    const time = timestamp - startTime;
    const nextScrollTop = easeInOutCubic(time > duration ? duration : time, scrollTop, y, duration);
    if (is.isWindow(container))
      container.scrollTo(window.pageXOffset, nextScrollTop);
    else if (container instanceof Document || container.constructor.name === "HTMLDocument")
      container.documentElement.scrollTop = nextScrollTop;
    else
      container.scrollTop = nextScrollTop;
    if (time < duration)
      raf.raf(frameFunc);
    else if (typeof callback === "function")
      callback();
  };
  raf.raf(frameFunc);
}

exports.easeInOutCubic = easeInOutCubic;
exports.scrollTo = scrollTo;
//# sourceMappingURL=scrollTo.cjs.map
