"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StickAllChannelModelObjectsUC = void 0;
exports.makeStickAllChannelModelObjectsUC = makeStickAllChannelModelObjectsUC;
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const DispatchStateChangeUC_1 = require("../../Dispatcher/UC/DispatchStateChangeUC");
const Stickable_1 = require("../../Stick/Entities/Stickable");
const Transform3D_1 = require("../../Transform3D/Entities/Transform3D");
const UndoRedoEntity_1 = require("../../UndoRedo/Entities/UndoRedoEntity");
const Entities_1 = require("../Entities");
class StickAllChannelModelObjectsUC extends core_1.AppObjectUC {
    static get(id, appObjects) {
        return appObjects
            .get(id)
            ?.getComponent(StickAllChannelModelObjectsUC.type);
    }
}
exports.StickAllChannelModelObjectsUC = StickAllChannelModelObjectsUC;
StickAllChannelModelObjectsUC.type = "StickAllChannelModelObjectsUC";
function makeStickAllChannelModelObjectsUC(appObject) {
    return new StickAllChannelModelObjectsUCImp(appObject);
}
class StickAllChannelModelObjectsUCImp extends StickAllChannelModelObjectsUC {
    get model() {
        return this.getCachedLocalComponent(Entities_1.ChannelModelEntity.type);
    }
    get dispatchStateChange() {
        return this.getCachedSingleton(DispatchStateChangeUC_1.DispatchStateChangeUC.type)?.doDispatch;
    }
    get undo() {
        return this.getCachedSingleton(UndoRedoEntity_1.UndoRedoEntity.type);
    }
    constructor(appObject) {
        super(appObject, StickAllChannelModelObjectsUC.type);
        this.stickAll = () => {
            if (!this.model)
                return;
            const dispatch = this.dispatchStateChange;
            if (!dispatch)
                return;
            const stuckObjects = [];
            this.model.modelObjects().forEach((modelObj) => {
                const appObj = modelObj.appObject;
                const stick = Stickable_1.StickableEntity.get(modelObj.id, this.appObjects);
                if (!stick || stick.canBeStuck !== true)
                    return;
                const transform = Transform3D_1.Transform3D.get(appObj);
                if (!transform)
                    return;
                const undoDTO = {
                    transform,
                    undoPos: transform.localPosition,
                    undoRot: transform.localRotation,
                    undoScale: transform.localScale,
                    redoPos: stick.stickPosition,
                    redoRot: stick.stickRotation,
                    redoScale: new core_2.Vector3(stick.stickScale, stick.stickScale, stick.stickScale),
                };
                stick.doStick(dispatch);
                stuckObjects.push(undoDTO);
            });
            if (stuckObjects.length === 0)
                return;
            this.undo?.submitAction({
                undoAction: () => {
                    stuckObjects.forEach((po) => {
                        po.transform.localPosition = po.undoPos;
                        po.transform.localRotation = po.undoRot;
                        po.transform.localScale = po.undoScale;
                    });
                    dispatch();
                },
                redoAction: () => {
                    stuckObjects.forEach((po) => {
                        po.transform.localPosition = po.redoPos;
                        po.transform.localRotation = po.redoRot;
                        po.transform.localScale = po.redoScale;
                    });
                    dispatch();
                },
            });
        };
    }
}
//# sourceMappingURL=StickAllChannelModelObjectsUC.js.map