"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MoveTransformUCImp = exports.MoveTransformUC = void 0;
exports.makeMoveTransform = makeMoveTransform;
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const Dispatcher_1 = require("../../Dispatcher");
const UndoRedo_1 = require("../../UndoRedo");
const Transform3D_1 = require("../Entities/Transform3D");
class MoveTransformUC extends core_1.AppObjectUC {
    static get(id, appObjects) {
        return appObjects
            .get(id)
            ?.getComponent(MoveTransformUC.type);
    }
}
exports.MoveTransformUC = MoveTransformUC;
MoveTransformUC.type = "MoveTransformUC";
function makeMoveTransform(appObject) {
    return new MoveTransformUCImp(appObject);
}
class MoveTransformUCImp extends MoveTransformUC {
    get transform() {
        return this.getCachedLocalComponent(Transform3D_1.Transform3D.type);
    }
    get undoRedo() {
        return this.getCachedSingleton(UndoRedo_1.UndoRedoEntity.type);
    }
    get dispatchStateChange() {
        return this.getCachedSingleton(Dispatcher_1.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 (core_2.Vector3.Equal(this.initialPos, transform.localPosition) &&
            core_2.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 = core_2.Vector3.Zero();
        this.initialRotation = core_2.Quaternion.Identity();
        this.isDragging = false;
        this.didDrag = false;
    }
}
exports.MoveTransformUCImp = MoveTransformUCImp;
//# sourceMappingURL=MoveTransformUC.js.map