import wrapperRaf from './raf.mjs';
import { easeInOutCubic } from './easings.mjs';
import getScroll, { isWindow } from './getScroll.mjs';

function scrollTo(y, options = {}) {
  const { getContainer = () => window, callback, duration = 450 } = options;
  const container = getContainer();
  const scrollTop = 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 (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)
      wrapperRaf(frameFunc);
    else if (typeof callback === "function")
      callback();
  };
  wrapperRaf(frameFunc);
}

export { scrollTo as default };
//# sourceMappingURL=scrollTo.mjs.map
