From f62d231a79e6709fedd6ee94034ac13c53091866 Mon Sep 17 00:00:00 2001
From: Poovizhi99 <poovizhi@hexrfactory.com>
Date: Tue, 15 Apr 2025 15:39:05 +0530
Subject: [PATCH] integrated the removeConnections in path connector

---
 .../modules/simulation/path/pathConnector.tsx | 158 +++++++++++++++---
 1 file changed, 139 insertions(+), 19 deletions(-)

diff --git a/app/src/modules/simulation/path/pathConnector.tsx b/app/src/modules/simulation/path/pathConnector.tsx
index 1e93bd5..fe3bb1b 100644
--- a/app/src/modules/simulation/path/pathConnector.tsx
+++ b/app/src/modules/simulation/path/pathConnector.tsx
@@ -1133,26 +1133,27 @@ function PathConnector({
           (state.modeluuid === connection2.model &&
             state.points.uuid === connection2.point)
         ) {
-          const updatedStaticMachine: SimulationTypes.StaticMachineEventsSchema = {
-            ...state,
-            points: {
-              ...state.points,
-              connections: {
-                ...state.points.connections,
-                targets: state.points.connections.targets.filter((target) => {
-                  return !(
-                    (target.modelUUID === connection1.model &&
-                      target.pointUUID === connection1.point) ||
-                    (target.modelUUID === connection2.model &&
-                      target.pointUUID === connection2.point)
-                  );
-                }),
+          const updatedStaticMachine: SimulationTypes.StaticMachineEventsSchema =
+            {
+              ...state,
+              points: {
+                ...state.points,
+                connections: {
+                  ...state.points.connections,
+                  targets: state.points.connections.targets.filter((target) => {
+                    return !(
+                      (target.modelUUID === connection1.model &&
+                        target.pointUUID === connection1.point) ||
+                      (target.modelUUID === connection2.model &&
+                        target.pointUUID === connection2.point)
+                    );
+                  }),
+                },
+                // Ensure all required StaticMachine point properties are included
+                actions: state.points.actions,
+                triggers: state.points.triggers,
               },
-              // Ensure all required StaticMachine point properties are included
-              actions: state.points.actions,
-              triggers: state.points.triggers,
-            },
-          };
+            };
           return updatedStaticMachine;
         }
       }
@@ -1211,6 +1212,125 @@ function PathConnector({
 
     setSimulationStates(updatedStates);
   };
+  const removeConnection = (deletedModelUUIDs: string[]) => {
+    const updatedStates = simulationStates.map((state) => {
+      // Handle Conveyor
+      if (state.type === "Conveyor") {
+        const updatedConveyor: SimulationTypes.ConveyorEventsSchema = {
+          ...state,
+          points: state.points.map((point) => {
+            return {
+              ...point,
+              connections: {
+                ...point.connections,
+                targets: point.connections.targets.filter(
+                  (target) => !deletedModelUUIDs.includes(target.modelUUID)
+                ),
+              },
+            };
+          }),
+        };
+        return updatedConveyor;
+      }
+
+      // Handle Vehicle
+      else if (state.type === "Vehicle") {
+        const updatedVehicle: SimulationTypes.VehicleEventsSchema = {
+          ...state,
+          points: {
+            ...state.points,
+            connections: {
+              ...state.points.connections,
+              targets: state.points.connections.targets.filter(
+                (target) => !deletedModelUUIDs.includes(target.modelUUID)
+              ),
+            },
+          },
+        };
+        return updatedVehicle;
+      }
+
+      // Handle StaticMachine
+      else if (state.type === "StaticMachine") {
+        const updatedStaticMachine: SimulationTypes.StaticMachineEventsSchema =
+          {
+            ...state,
+            points: {
+              ...state.points,
+              connections: {
+                ...state.points.connections,
+                targets: state.points.connections.targets.filter(
+                  (target) => !deletedModelUUIDs.includes(target.modelUUID)
+                ),
+              },
+            },
+          };
+        return updatedStaticMachine;
+      }
+
+      // Handle ArmBot
+      else if (state.type === "ArmBot") {
+        const updatedArmBot: SimulationTypes.ArmBotEventsSchema = {
+          ...state,
+          points: {
+            ...state.points,
+            connections: {
+              ...state.points.connections,
+
+              targets: state.points.connections.targets.filter(
+                (target: any) => !deletedModelUUIDs.includes(target.modelUUID)
+              ),
+            },
+            actions: {
+              ...state.points.actions,
+              processes: (state.points.actions.processes =
+                state.points.actions.processes?.filter((process) => {
+                  const matchedStates = simulationStates.filter((s) =>
+                    deletedModelUUIDs.includes(s.modeluuid)
+                  );
+
+                  if (matchedStates.length > 0) {
+                    if (matchedStates[0]?.type === "StaticMachine") {
+                      const trigPoints = matchedStates[0]?.points;
+
+                      return !(
+                        process.triggerId === trigPoints?.triggers?.uuid
+                      );
+                    } else if (matchedStates[0]?.type === "Conveyor") {
+                      const trigPoints = matchedStates[0]?.points;
+
+                      if (Array.isArray(trigPoints)) {
+                        const nonEmptyTriggers = trigPoints.filter(
+                          (point) =>
+                            point && point.triggers && point.triggers.length > 0
+                        );
+
+                        const allTriggerUUIDs = nonEmptyTriggers
+                          .flatMap((point) => point.triggers)
+                          .map((trigger) => trigger.uuid);
+
+                        return !allTriggerUUIDs.includes(process.triggerId);
+                      }
+                    }
+                  }
+                  return true;
+                })),
+            },
+          },
+        };
+        return updatedArmBot;
+      }
+
+      return state;
+    });
+
+    const filteredStates = updatedStates.filter(
+      (state) => !deletedModelUUIDs.includes(state.modeluuid)
+    );
+
+    updateBackend(filteredStates);
+    setSimulationStates(filteredStates);
+  };
 
   return (
     <group name="simulationConnectionGroup" visible={!isPlaying}>