import { __awaiter, __generator } from "tslib";
import TWEEN from '@tweenjs/tween.js';
import Instance from '../core/Instance';
var AnimaControl = /** @class */ (function () {
    function AnimaControl() {
        var _this = this;
        this._tMap = new WeakMap();
        this._isAnimaPlaying = false;
        this._flashList = new Map();
        Instance.getInstanceClock().then(function (clock) {
            clock.on('tick', _this._update);
        });
    }
    AnimaControl.prototype._update = function (_t) {
        TWEEN.update(_t);
    };
    AnimaControl.prototype.sleep = function (timeout) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                return [2 /*return*/, new Promise(function (resolve) {
                        setTimeout(resolve, timeout);
                    })];
            });
        });
    };
    AnimaControl.prototype.transInWorker = function (from, to, timespan, onUpdate) {
        if (timespan === void 0) { timespan = 200; }
        if (onUpdate === void 0) { onUpdate = null; }
        return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) {
            return [2 /*return*/];
        }); });
    };
    AnimaControl.prototype.trans = function (from, to, timespan, onUpdate) {
        if (timespan === void 0) { timespan = 200; }
        if (onUpdate === void 0) { onUpdate = null; }
        return __awaiter(this, void 0, void 0, function () {
            var _this = this;
            return __generator(this, function (_a) {
                return [2 /*return*/, new Promise(function (resolve, reject) {
                        var _tMap = _this._tMap;
                        var tween;
                        if ((tween = _tMap.get(from))) {
                            // reject();
                            tween.stop();
                        }
                        tween =
                            _tMap.get(from) || _tMap.set(from, new TWEEN.Tween(from)).get(from);
                        tween
                            .to(to, timespan)
                            .easing(TWEEN.Easing.Quadratic.Out)
                            .onUpdate(function (evt) {
                            if (onUpdate && onUpdate instanceof Function) {
                                onUpdate(evt);
                            }
                        })
                            .onComplete(function () {
                            _tMap.delete(from);
                            resolve();
                        })
                            .onStop(function () {
                            _tMap.delete(from);
                            reject();
                        });
                        tween.start();
                    })];
            });
        });
    };
    return AnimaControl;
}());
export default AnimaControl;
