import { AppObjectUC } from "@vived/core";
import { Vector3 } from "@vived/core";
import { DispatchStateChangeUC } from "../../Dispatcher/UC/DispatchStateChangeUC";
import { StickableEntity } from "../../Stick/Entities/Stickable";
import { Transform3D } from "../../Transform3D/Entities/Transform3D";
import { UndoRedoEntity } from "../../UndoRedo/Entities/UndoRedoEntity";
import { ChannelModelEntity } from "../Entities";
export class StickAllChannelModelObjectsUC extends AppObjectUC {
    static get(id, appObjects) {
        return appObjects
            .get(id)
            ?.getComponent(StickAllChannelModelObjectsUC.type);
    }
}
StickAllChannelModelObjectsUC.type = "StickAllChannelModelObjectsUC";
export function makeStickAllChannelModelObjectsUC(appObject) {
    return new StickAllChannelModelObjectsUCImp(appObject);
}
class StickAllChannelModelObjectsUCImp extends StickAllChannelModelObjectsUC {
    get model() {
        return this.getCachedLocalComponent(ChannelModelEntity.type);
    }
    get dispatchStateChange() {
        return this.getCachedSingleton(DispatchStateChangeUC.type)?.doDispatch;
    }
    get undo() {
        return this.getCachedSingleton(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 = StickableEntity.get(modelObj.id, this.appObjects);
                if (!stick || stick.canBeStuck !== true)
                    return;
                const transform = 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 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