import { AppObjectUC, getSingletonComponent, } from "@vived/core";
import { LabelDropRepo } from "../Entities/LabelDropRepo";
export class LabelDropCorrectAnimationUC extends AppObjectUC {
    static get(appObject) {
        return getSingletonComponent(LabelDropCorrectAnimationUC.type, appObject);
    }
}
LabelDropCorrectAnimationUC.type = "LabelDropCorrectAnimation";
export 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.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