"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LabelDropOnNothingAnimationUC = void 0;
exports.makeLabelDropOnNothingAnimationUC = makeLabelDropOnNothingAnimationUC;
const core_1 = require("@vived/core");
const LabelDropRepo_1 = require("../Entities/LabelDropRepo");
class LabelDropOnNothingAnimationUC extends core_1.AppObjectUC {
    static get(appObject) {
        return (0, core_1.getSingletonComponent)(LabelDropOnNothingAnimationUC.type, appObject);
    }
}
exports.LabelDropOnNothingAnimationUC = LabelDropOnNothingAnimationUC;
LabelDropOnNothingAnimationUC.type = "LabelDropOnNothingAnimationUC";
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 = core_1.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 ??
                    core_1.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 = (0, core_1.cubicOut)(animationPercent);
                        const lerpedPos = core_1.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_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=DropOnNothingAnimationUC.js.map