import { AppObjectEntity, expoOut, MemoizedBoolean, MemoizedNumber, MemoizedQuaternion, MemoizedVector3, Quaternion, Vector3, } from "@vived/core";
import { Transform3D } from "../../Transform3D";
export class StickableEntity extends AppObjectEntity {
    static get(id, appObjects) {
        return appObjects
            .get(id)
            ?.getComponent(StickableEntity.type);
    }
}
StickableEntity.type = "StickableEntity";
export 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 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 = expoOut;
        this.memoizedCanBeStuck = new MemoizedBoolean(false, this.notifyOnChange);
        this.memoizedStickPosition = new MemoizedVector3(Vector3.Zero(), this.notifyOnChange);
        this.memoizedStickRotation = new MemoizedQuaternion(Quaternion.Identity(), this.notifyOnChange);
        this.memoizedStickScale = new MemoizedNumber(1, this.notifyOnChange);
        this.memoizedShowStickTarget = new MemoizedBoolean(false, this.notifyOnChange);
        this.memoizedHighlightTarget = new MemoizedBoolean(false, this.notifyOnChange);
        this.checkIfStickable = () => {
            if (!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 (!Quaternion.Close(this.transform.localRotation, this.memoizedStickRotation.val)) {
                this.memoizedCanBeStuck.val = true;
                return;
            }
            this.memoizedCanBeStuck.val = false;
        };
        this.transform = Transform3D.addIfMissing(appObject);
        this.transform.addChangeObserver(this.checkIfStickable);
        this.checkIfStickable();
    }
}
//# sourceMappingURL=Stickable.js.map