"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.animateSimulate = void 0;
const animateSimulateTick_1 = require("./animateSimulateTick");
const animateSimulate = (system, n0, n1, ff, tf, callback) => {
    const { api: { animation: { requestAnimationFrame, cancelAnimationFrame }, }, } = system;
    let n = n0;
    let frame;
    let cancelled = false;
    const next = () => {
        frame = requestAnimationFrame(tick);
    };
    const tick = () => {
        const n_ = n1();
        const ended = (0, animateSimulateTick_1.animateSimulateTick)(n, n_, ff, tf);
        if (ended) {
            void callback();
        }
        if (!cancelled) {
            next();
        }
    };
    next();
    return () => {
        if (frame !== undefined) {
            cancelAnimationFrame(frame);
            cancelled = true;
            frame = undefined;
        }
    };
};
exports.animateSimulate = animateSimulate;
//# sourceMappingURL=animateSimulate.js.map