"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StylusMoveTransformUC = void 0;
exports.makeStylusMoveTransformUC = makeStylusMoveTransformUC;
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const DispatchStateChangeUC_1 = require("../../Dispatcher/UC/DispatchStateChangeUC");
const StylusEntity_1 = require("../../Stylus/Entity/StylusEntity");
const UndoRedoEntity_1 = require("../../UndoRedo/Entities/UndoRedoEntity");
const Transform3D_1 = require("../Entities/Transform3D");
const MoveTransformUndoRedo_1 = require("./MoveTransformUndoRedo");
class StylusMoveTransformUC extends core_1.AppObjectUC {
    static get(appObject) {
        return appObject.getComponent(StylusMoveTransformUC.type);
    }
}
exports.StylusMoveTransformUC = StylusMoveTransformUC;
StylusMoveTransformUC.type = "StylusMoveTransformUC";
function makeStylusMoveTransformUC(appObject) {
    return new StylusMoveTransformUCImp(appObject);
}
class StylusMoveTransformUCImp extends StylusMoveTransformUC {
    get dispatchStateChange() {
        return this.getCachedSingleton(DispatchStateChangeUC_1.DispatchStateChangeUC.type);
    }
    get undoRedoEntity() {
        return this.getCachedSingleton(UndoRedoEntity_1.UndoRedoEntity.type);
    }
    get stylus() {
        return this.getCachedSingleton(StylusEntity_1.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 = core_2.Vector3.Zero();
        this.initialRotation = core_2.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_1.MoveTransformUndoRedo(undoDTO, this.transform, this.appObject.appObjectRepo));
                }
            }
            this.isMoving = false;
            this.didMove = false;
        };
        this.transform = Transform3D_1.Transform3D.addIfMissing(this.appObject);
        const helperAO = this.appObjects.getOrCreate(this.appObject.id + "_helper");
        this.helperTransform = (0, Transform3D_1.makeTransform3D)(helperAO);
        if (this.stylus) {
            this.helperTransform.parent = this.stylus.transform;
        }
    }
}
//# sourceMappingURL=StylusMoveTransformUC.js.map