import { AppObjectUC } from "@vived/core";
import { Quaternion, Vector3 } from "@vived/core";
import { DispatchStateChangeUC } from "../../Dispatcher";
import { UndoRedoEntity } from "../../UndoRedo";
import { Transform3D } from "../Entities/Transform3D";
export class MoveTransformUC extends AppObjectUC {
    static get(id, appObjects) {
        return appObjects
            .get(id)
            ?.getComponent(MoveTransformUC.type);
    }
}
MoveTransformUC.type = "MoveTransformUC";
export function makeMoveTransform(appObject) {
    return new MoveTransformUCImp(appObject);
}
export class MoveTransformUCImp extends MoveTransformUC {
    get transform() {
        return this.getCachedLocalComponent(Transform3D.type);
    }
    get undoRedo() {
        return this.getCachedSingleton(UndoRedoEntity.type);
    }
    get dispatchStateChange() {
        return this.getCachedSingleton(DispatchStateChangeUC.type)?.doDispatch;
    }
    initialize(helper) {
        this.isDragging = false;
        this.didDrag = false;
        if (!this.transform) {
            return;
        }
        helper.worldPosition = this.transform.worldPosition;
        helper.worldRotation = this.transform.worldRotation;
        this.initialPos = this.transform.localPosition;
        this.initialRotation = this.transform.localRotation;
        this.isDragging = true;
    }
    update(helper, ray) {
        if (!this.isDragging)
            return;
        if (!this.transform)
            return;
        this.transform.worldRotation = helper.worldRotation;
        this.transform.worldPosition = helper.worldPosition;
        this.didDrag = true;
    }
    finish() {
        if (!this.transform)
            return;
        this.isDragging = false;
        if (!this.didDrag)
            return;
        this.submitDispatchAndUndo();
        this.didDrag = false;
    }
    cancel() {
        if (!this.transform)
            return;
        if (!this.isDragging)
            return;
        this.isDragging = false;
        this.didDrag = false;
    }
    submitDispatchAndUndo() {
        const dispatch = this.dispatchStateChange;
        const transform = this.transform;
        if (!transform || !dispatch)
            return;
        if (Vector3.Equal(this.initialPos, transform.localPosition) &&
            Quaternion.Equal(this.initialRotation, transform.localRotation))
            return;
        if (this.dispatchStateChangeOnComplete) {
            dispatch();
        }
        if (this.submitUndoOnComplete) {
            const redoPos = transform.localPosition;
            const redoRot = transform.localRotation;
            this.undoRedo?.submitAction({
                undoAction: () => {
                    transform.localPosition = this.initialPos;
                    transform.localRotation = this.initialRotation;
                    dispatch();
                },
                redoAction: () => {
                    transform.localPosition = redoPos;
                    transform.localRotation = redoRot;
                    dispatch();
                },
            });
        }
    }
    constructor(appObject) {
        super(appObject, MoveTransformUC.type);
        this.dispatchStateChangeOnComplete = true;
        this.submitUndoOnComplete = true;
        this.initialPos = Vector3.Zero();
        this.initialRotation = Quaternion.Identity();
        this.isDragging = false;
        this.didDrag = false;
    }
}
//# sourceMappingURL=MoveTransformUC.js.map