/**
 * slideTo.js
 *
 * @Author: jruif
 * @Date: 2018/6/1 下午3:38
 */

import './rAF.js';
import tweenFunction from 'tween-functions';
var handleRAF = null;

/* *
*
示例：
slideTo({
    beginValue: current.ref.scrollTop,
    endValue: checked.offsetTop - current.ref.offsetTop - current.ref.offsetHeight * 0.3,
    stepCallback: value => {
        current.ref.scrollTop = value;
    },
});
* */
var slideTo = function slideTo(_ref) {
    var beginValue = _ref.beginValue,
        endValue = _ref.endValue,
        _ref$time = _ref.time,
        time = _ref$time === undefined ? 800 : _ref$time,
        _ref$tweenName = _ref.tweenName,
        tweenName = _ref$tweenName === undefined ? 'easeOutQuint' : _ref$tweenName,
        stepCallback = _ref.stepCallback,
        _ref$endCallback = _ref.endCallback,
        endCallback = _ref$endCallback === undefined ? function () {} : _ref$endCallback;
    var _window = window,
        requestAnimationFrame = _window.requestAnimationFrame;

    var totalDuration = time * 60 / 1000; // 1s 60fps
    var currentTime = 1;

    var run = function run() {
        var tweenValue = tweenFunction[tweenName](currentTime++, beginValue, endValue, totalDuration);
        if (handleRAF) {
            cancelAnimationFrame(handleRAF);
        }
        // scrollTo(pageXOffset, tweenValue);
        stepCallback && stepCallback(tweenValue);
        if (Math.floor((tweenValue - endValue) * 10e5) !== 0) {
            handleRAF = requestAnimationFrame(run);
        } else {
            setTimeout(endCallback);
        }
    };
    run();
};

export default slideTo;