"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StickUC = void 0;
exports.makeStickUC = makeStickUC;
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const DispatchStateChangeUC_1 = require("../../Dispatcher/UC/DispatchStateChangeUC");
const Transform3D_1 = require("../../Transform3D/Entities/Transform3D");
const UndoRedoEntity_1 = require("../../UndoRedo/Entities/UndoRedoEntity");
const Stickable_1 = require("../Entities/Stickable");
class StickUC extends core_1.AppObjectUC {
    static get(id, appObjects) {
        return appObjects.get(id)?.getComponent(StickUC.type);
    }
}
exports.StickUC = StickUC;
StickUC.type = "StickUC";
function makeStickUC(appObject) {
    return new StickUCImp(appObject);
}
class StickUCImp extends StickUC {
    get stickEntity() {
        return this.getCachedLocalComponent(Stickable_1.StickableEntity.type);
    }
    get transform3D() {
        return this.getCachedLocalComponent(Transform3D_1.Transform3D.type);
    }
    get dispatchStateChange() {
        return this.getCachedSingleton(DispatchStateChangeUC_1.DispatchStateChangeUC.type)?.doDispatch;
    }
    get undo() {
        return this.getCachedSingleton(UndoRedoEntity_1.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 core_2.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