import { stepper } from './stepper.js';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
var noop = function () {
    var args = [];
    for (var _i = 0; _i < arguments.length; _i++) {
        args[_i] = arguments[_i];
    }
    /* do nothing */
};
export var animation = function (globalObject) {
    var animating = false;
    var currentPosition = 0;
    var currentVelocity = 0;
    var rafId = 0;
    var target = 0;
    var deltaHandler = noop;
    var animationEndHandler = noop;
    function reset() {
        currentVelocity = 0;
        currentPosition = 0;
        target = 0;
        animating = false;
        deltaHandler = noop;
        animationEndHandler = noop;
    }
    function inProgress() {
        return animating;
    }
    function cancel() {
        var _a;
        (_a = globalObject.cancelAnimationFrame) === null || _a === void 0 ? void 0 : _a.call(globalObject, rafId);
        reset();
    }
    function finish() {
        animationEndHandler();
        cancel();
    }
    function animate(amount, onDelta, onEnd) {
        if (onEnd === void 0) { onEnd = noop; }
        target += amount;
        deltaHandler = onDelta;
        animationEndHandler = onEnd;
        var animateInternal = function () {
            animating = true;
            var _a = stepper(currentPosition, currentVelocity, target), nextPosition = _a[0], nextVelocity = _a[1];
            var delta = nextPosition - currentPosition;
            deltaHandler(delta);
            if (nextPosition === target && nextVelocity === 0) {
                animationEndHandler();
                reset();
                return;
            }
            currentPosition = nextPosition;
            currentVelocity = nextVelocity;
            if (globalObject.requestAnimationFrame) {
                rafId = globalObject.requestAnimationFrame(animateInternal);
            }
        };
        if (!inProgress()) {
            animateInternal();
        }
    }
    return {
        animate: animate,
        inProgress: inProgress,
        cancel: cancel,
        finish: finish,
    };
};
//# sourceMappingURL=index.js.map