"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LabelDropCorrectAnimationUC = void 0;
exports.makeLabelDropCorrectAnimationUC = makeLabelDropCorrectAnimationUC;
const core_1 = require("@vived/core");
const LabelDropRepo_1 = require("../Entities/LabelDropRepo");
class LabelDropCorrectAnimationUC extends core_1.AppObjectUC {
    static get(appObject) {
        return (0, core_1.getSingletonComponent)(LabelDropCorrectAnimationUC.type, appObject);
    }
}
exports.LabelDropCorrectAnimationUC = LabelDropCorrectAnimationUC;
LabelDropCorrectAnimationUC.type = "LabelDropCorrectAnimation";
function makeLabelDropCorrectAnimationUC(appObject) {
    return new LabelDropCorrectAnimationUCImp(appObject);
}
class LabelDropCorrectAnimationUCImp extends LabelDropCorrectAnimationUC {
    getIsAnimating() {
        return this._isAnimating;
    }
    constructor(appObject) {
        super(appObject, LabelDropCorrectAnimationUC.type);
        this.feedbackDurationMS = 1000;
        this.fadeDurationMS = 200;
        this._isAnimating = false;
        this.intervalDeltaMS = 10;
        this.cancelAnimation = false;
        this.start = () => {
            if (!this.labelDropEntity) {
                return Promise.reject(new Error("No label drop entity"));
            }
            return new Promise((resolve) => {
                const startTime = Date.now();
                const fadeStartTime = startTime + this.feedbackDurationMS;
                const endTime = startTime + this.fadeDurationMS + this.feedbackDurationMS;
                this._isAnimating = true;
                const interval = setInterval(() => {
                    if (!this.labelDropEntity)
                        return;
                    if (this.cancelAnimation) {
                        clearInterval(interval);
                        this.cancelAnimation = false;
                        resolve(false);
                        return;
                    }
                    const currentTime = Date.now();
                    if (currentTime >= endTime || this.cancelAnimation) {
                        this._isAnimating = false;
                        this.labelDropEntity.dragAlpha = 0;
                        clearInterval(interval);
                        resolve(true);
                    }
                    else if (currentTime >= fadeStartTime) {
                        const fadePercent = (currentTime - fadeStartTime) / this.fadeDurationMS;
                        this.labelDropEntity.dragAlpha = 1 - fadePercent;
                    }
                }, this.intervalDeltaMS);
            });
        };
        this.cancel = () => {
            if (!this.getIsAnimating) {
                return;
            }
            this.cancelAnimation = true;
        };
        this.labelDropEntity = appObject.getComponent(LabelDropRepo_1.LabelDropRepo.type);
        if (!this.labelDropEntity) {
            this.error("UC added to an app object that does not have a LabelDropEntity");
        }
        this.appObjects.registerSingleton(this);
    }
}
//# sourceMappingURL=CorrectAnimationUC.js.map