import { AppObjectUC } from "@vived/core";
import { Vector3 } from "@vived/core";
import { DispatchStateChangeUC } from "../../Dispatcher/UC/DispatchStateChangeUC";
import { Transform3D } from "../../Transform3D/Entities/Transform3D";
import { UndoRedoEntity } from "../../UndoRedo/Entities/UndoRedoEntity";
import { StickableEntity } from "../Entities/Stickable";
export class StickUC extends AppObjectUC {
    static get(id, appObjects) {
        return appObjects.get(id)?.getComponent(StickUC.type);
    }
}
StickUC.type = "StickUC";
export function makeStickUC(appObject) {
    return new StickUCImp(appObject);
}
class StickUCImp extends StickUC {
    get stickEntity() {
        return this.getCachedLocalComponent(StickableEntity.type);
    }
    get transform3D() {
        return this.getCachedLocalComponent(Transform3D.type);
    }
    get dispatchStateChange() {
        return this.getCachedSingleton(DispatchStateChangeUC.type)?.doDispatch;
    }
    get undo() {
        return this.getCachedSingleton(UndoRedoEntity.type);
    }
    constructor(appObject) {
        super(appObject, StickUC.type);
        this.doStick = () => {
            const transform = this.transform3D;
            const dispatch = this.dispatchStateChange;
            if (!transform || !this.stickEntity || !dispatch) {
                this.error("Missing Components");
                return;
            }
            if (!this.stickEntity.canBeStuck)
                return;
            const startPos = transform.localPosition;
            const finalPos = this.stickEntity.stickPosition;
            const startRot = transform.localRotation;
            const finalRot = this.stickEntity.stickRotation;
            const startScale = transform.localScale;
            const finalScale = new Vector3(this.stickEntity.stickScale, this.stickEntity.stickScale, this.stickEntity.stickScale);
            this.stickEntity.doStick(dispatch);
            this.undo?.submitAction({
                undoAction: () => {
                    transform.localPosition = startPos;
                    transform.localRotation = startRot;
                    transform.localScale = startScale;
                    dispatch();
                },
                redoAction: () => {
                    transform.localPosition = finalPos;
                    transform.localRotation = finalRot;
                    transform.localScale = finalScale;
                    dispatch();
                },
            });
        };
    }
}
//# sourceMappingURL=StickUC.js.map