"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StickableEntity = void 0;
exports.makeStickableEntity = makeStickableEntity;
const core_1 = require("@vived/core");
const Transform3D_1 = require("../../Transform3D");
class StickableEntity extends core_1.AppObjectEntity {
    static get(id, appObjects) {
        return appObjects
            .get(id)
            ?.getComponent(StickableEntity.type);
    }
}
exports.StickableEntity = StickableEntity;
StickableEntity.type = "StickableEntity";
function makeStickableEntity(appObject) {
    return new StickableEntityImp(appObject);
}
class StickableEntityImp extends StickableEntity {
    doStick(onCompleteCallback) {
        if (!this.canBeStuck)
            return;
        const lerpData = {
            durationMS: this.stickDurationMS,
            ease: this.easeFn,
            desiredPosition: this.stickPosition,
            desiredRotation: this.stickRotation,
            desiredScale: new core_1.Vector3(this.stickScale, this.stickScale, this.stickScale),
            onCompletedCallback: onCompleteCallback,
        };
        this.transform.lerp(lerpData);
    }
    get canBeStuck() {
        return this.memoizedCanBeStuck.val;
    }
    get stickPosition() {
        return this.memoizedStickPosition.val;
    }
    set stickPosition(val) {
        this.memoizedStickPosition.val = val;
        this.checkIfStickable();
    }
    get stickRotation() {
        return this.memoizedStickRotation.val;
    }
    set stickRotation(val) {
        this.memoizedStickRotation.val = val;
        this.checkIfStickable();
    }
    get stickScale() {
        return this.memoizedStickScale.val;
    }
    set stickScale(val) {
        this.memoizedStickScale.val = val;
        this.checkIfStickable();
    }
    get showStickTarget() {
        return this.memoizedShowStickTarget.val;
    }
    set showStickTarget(showStickTarget) {
        this.memoizedShowStickTarget.val = showStickTarget;
    }
    get highlightTarget() {
        return this.memoizedHighlightTarget.val;
    }
    set highlightTarget(highlightTarget) {
        this.memoizedHighlightTarget.val = highlightTarget;
    }
    constructor(appObject) {
        super(appObject, StickableEntity.type);
        this.stickDurationMS = 500;
        this.easeFn = core_1.expoOut;
        this.memoizedCanBeStuck = new core_1.MemoizedBoolean(false, this.notifyOnChange);
        this.memoizedStickPosition = new core_1.MemoizedVector3(core_1.Vector3.Zero(), this.notifyOnChange);
        this.memoizedStickRotation = new core_1.MemoizedQuaternion(core_1.Quaternion.Identity(), this.notifyOnChange);
        this.memoizedStickScale = new core_1.MemoizedNumber(1, this.notifyOnChange);
        this.memoizedShowStickTarget = new core_1.MemoizedBoolean(false, this.notifyOnChange);
        this.memoizedHighlightTarget = new core_1.MemoizedBoolean(false, this.notifyOnChange);
        this.checkIfStickable = () => {
            if (!core_1.Vector3.Equal(this.transform.localPosition, this.memoizedStickPosition.val)) {
                this.memoizedCanBeStuck.val = true;
                return;
            }
            if (this.transform.localScale.x !== this.memoizedStickScale.val) {
                this.memoizedCanBeStuck.val = true;
                return;
            }
            if (!core_1.Quaternion.Close(this.transform.localRotation, this.memoizedStickRotation.val)) {
                this.memoizedCanBeStuck.val = true;
                return;
            }
            this.memoizedCanBeStuck.val = false;
        };
        this.transform = Transform3D_1.Transform3D.addIfMissing(appObject);
        this.transform.addChangeObserver(this.checkIfStickable);
        this.checkIfStickable();
    }
}
//# sourceMappingURL=Stickable.js.map