import { AppObjectUC, cubicOut, getSingletonComponent, Vector3, } from "@vived/core";
import { LabelDropRepo } from "../Entities/LabelDropRepo";
export class LabelDropOnNothingAnimationUC extends AppObjectUC {
    static get(appObject) {
        return getSingletonComponent(LabelDropOnNothingAnimationUC.type, appObject);
    }
}
LabelDropOnNothingAnimationUC.type = "LabelDropOnNothingAnimationUC";
export function makeLabelDropOnNothingAnimationUC(appObject) {
    return new LabelDropOnNothingAnimationUCImp(appObject);
}
class LabelDropOnNothingAnimationUCImp extends LabelDropOnNothingAnimationUC {
    getIsAnimating() {
        return this._isAnimating;
    }
    constructor(appObject) {
        super(appObject, LabelDropOnNothingAnimationUC.type);
        this.animationDurationMS = 500;
        this.desiredFinalPosition = Vector3.Zero();
        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 endTime = startTime + this.animationDurationMS;
                this._isAnimating = true;
                const startPosition = this.labelDropEntity?.dragLabelTransform.worldPosition ??
                    Vector3.Zero();
                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._isAnimating = false;
                        this.labelDropEntity.dragAlpha = 0;
                        this.labelDropEntity.dragLabelTransform.worldPosition = this.desiredFinalPosition;
                        clearInterval(interval);
                        resolve(true);
                    }
                    else {
                        const animationPercent = (currentTime - startTime) / this.animationDurationMS;
                        const acceleratorValue = 1.5;
                        let alpha = 1 - acceleratorValue * animationPercent;
                        if (alpha < 0)
                            alpha = 0;
                        this.labelDropEntity.dragAlpha = alpha;
                        const easedPercent = cubicOut(animationPercent);
                        const lerpedPos = Vector3.Lerp(startPosition, this.desiredFinalPosition, easedPercent);
                        this.labelDropEntity.dragLabelTransform.worldPosition = lerpedPos;
                    }
                }, 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=DropOnNothingAnimationUC.js.map