import { AppObjectUC } from "@vived/core";
import { Quaternion, Vector3 } from "@vived/core";
import { DispatchStateChangeUC } from "../../Dispatcher/UC/DispatchStateChangeUC";
import { StylusEntity } from "../../Stylus/Entity/StylusEntity";
import { UndoRedoEntity } from "../../UndoRedo/Entities/UndoRedoEntity";
import { makeTransform3D, Transform3D } from "../Entities/Transform3D";
import { MoveTransformUndoRedo, } from "./MoveTransformUndoRedo";
export class StylusMoveTransformUC extends AppObjectUC {
    static get(appObject) {
        return appObject.getComponent(StylusMoveTransformUC.type);
    }
}
StylusMoveTransformUC.type = "StylusMoveTransformUC";
export function makeStylusMoveTransformUC(appObject) {
    return new StylusMoveTransformUCImp(appObject);
}
class StylusMoveTransformUCImp extends StylusMoveTransformUC {
    get dispatchStateChange() {
        return this.getCachedSingleton(DispatchStateChangeUC.type);
    }
    get undoRedoEntity() {
        return this.getCachedSingleton(UndoRedoEntity.type);
    }
    get stylus() {
        return this.getCachedSingleton(StylusEntity.type);
    }
    initialize() {
        if (!this.transform)
            return false;
        if (!this.stylus)
            return false;
        this.initialPosition = this.transform.localPosition;
        this.initialRotation = this.transform.localRotation;
        this.helperTransform.worldPosition = this.transform.worldPosition;
        this.helperTransform.worldRotation = this.transform.worldRotation;
        this.isMoving = true;
        return;
    }
    constructor(appObject) {
        super(appObject, StylusMoveTransformUC.type);
        this.isMoving = false;
        this.didMove = false;
        this.initialPosition = Vector3.Zero();
        this.initialRotation = Quaternion.Identity();
        this.move = () => {
            if (!this.isMoving)
                return;
            if (!this.transform)
                return;
            if (!this.stylus)
                return;
            this.transform.worldPosition = this.helperTransform.worldPosition;
            this.transform.worldRotation = this.helperTransform.worldRotation;
            this.didMove = true;
        };
        this.stop = (registerUndo = false) => {
            if (this.didMove) {
                this.dispatchStateChange?.doDispatch();
                if (registerUndo && this.undoRedoEntity && this.transform) {
                    const undoDTO = {
                        endPosition: this.transform.localPosition,
                        endRotation: this.transform.localRotation,
                        startPosition: this.initialPosition,
                        startRotation: this.initialRotation,
                    };
                    this.undoRedoEntity.submitAction(new MoveTransformUndoRedo(undoDTO, this.transform, this.appObject.appObjectRepo));
                }
            }
            this.isMoving = false;
            this.didMove = false;
        };
        this.transform = Transform3D.addIfMissing(this.appObject);
        const helperAO = this.appObjects.getOrCreate(this.appObject.id + "_helper");
        this.helperTransform = makeTransform3D(helperAO);
        if (this.stylus) {
            this.helperTransform.parent = this.stylus.transform;
        }
    }
}
//# sourceMappingURL=StylusMoveTransformUC.js.map