"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = scrollTo;

var _raf = _interopRequireDefault(require("raf"));

var _getScroll = _interopRequireDefault(require("./getScroll"));

var _easings = require("./easings");

function scrollTo(to) {
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  var _options$getContainer = options.getContainer,
      getContainer = _options$getContainer === void 0 ? function () {
    return window;
  } : _options$getContainer,
      callback = options.callback,
      _options$duration = options.duration,
      duration = _options$duration === void 0 ? 450 : _options$duration,
      _options$top = options.top,
      top = _options$top === void 0 ? true : _options$top;
  var container = getContainer();
  var scroll = (0, _getScroll["default"])(container, top);
  var startTime = Date.now();

  var frameFunc = function frameFunc() {
    var timestamp = Date.now();
    var time = timestamp - startTime;
    var nextScroll = (0, _easings.easeInOutCubic)(time > duration ? duration : time, scroll, to, duration);

    if (top) {
      if (container === window) {
        window.scrollTo(window.pageXOffset, nextScroll);
      } else {
        container.scrollTop = nextScroll;
      }
    } else if (container === window) {
      window.scrollTo(nextScroll, window.pageYOffset);
    } else {
      container.scrollLeft = nextScroll;
    }

    if (time < duration) {
      (0, _raf["default"])(frameFunc);
    } else if (typeof callback === 'function') {
      callback();
    }
  };

  (0, _raf["default"])(frameFunc);
}
//# sourceMappingURL=scrollTo.js.map
