"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LabelDropIncorrectAnimationUC = void 0;
exports.makeLabelDropIncorrectAnimationUC = makeLabelDropIncorrectAnimationUC;
const core_1 = require("@vived/core");
const LabelDropRepo_1 = require("../Entities/LabelDropRepo");
class LabelDropIncorrectAnimationUC extends core_1.AppObjectUC {
    static get(appObject) {
        return (0, core_1.getSingletonComponent)(LabelDropIncorrectAnimationUC.type, appObject);
    }
}
exports.LabelDropIncorrectAnimationUC = LabelDropIncorrectAnimationUC;
LabelDropIncorrectAnimationUC.type = "LabelDropIncorrectAnimationUC";
function makeLabelDropIncorrectAnimationUC(appObject) {
    return new LabelDropIncorrectAnimationUCImp(appObject);
}
class LabelDropIncorrectAnimationUCImp extends LabelDropIncorrectAnimationUC {
    getIsAnimating() {
        return this._isAnimating;
    }
    constructor(appObject) {
        super(appObject, LabelDropIncorrectAnimationUC.type);
        this.feedbackDurationMS = 1000;
        this.fadeDurationMS = 300;
        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, reject) => {
                const startTime = Date.now();
                const fadeStartTime = startTime + this.feedbackDurationMS;
                const endTime = startTime + this.fadeDurationMS + this.feedbackDurationMS;
                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.cancelAnimation) {
                        this._isAnimating = false;
                        this.labelDropEntity.dragAlpha = 0;
                        this.labelDropEntity.dragLabelTransform.worldPosition = this.desiredFinalPosition;
                        clearInterval(interval);
                        resolve(true);
                    }
                    else if (currentTime >= fadeStartTime) {
                        const animationPercent = (currentTime - fadeStartTime) / this.fadeDurationMS;
                        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=IncorrectAnimationUC.js.map