From cddb050dfe859326c4e295ec3ff074a360367a30 Mon Sep 17 00:00:00 2001
From: Matei Adriel <rafaeladriel11@gmail.com>
Date: Thu, 25 Jul 2019 15:14:54 +0300
Subject: [PATCH] automatic cleaning of integrated circuits

---
 .../integrated-circuits/helpers/compileIc.ts     | 16 +++++++++++++---
 src/modules/simulation/classes/Gate.ts           | 10 ++++++++--
 .../classes/SimulationRenderer.ts                |  4 +---
 3 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/src/modules/integrated-circuits/helpers/compileIc.ts b/src/modules/integrated-circuits/helpers/compileIc.ts
index 0492b2f..92d327d 100644
--- a/src/modules/integrated-circuits/helpers/compileIc.ts
+++ b/src/modules/integrated-circuits/helpers/compileIc.ts
@@ -9,20 +9,30 @@ import { templateStore } from '../../saving/stores/templateStore'
 import { toast } from 'react-toastify'
 import { createToastArguments } from '../../toasts/helpers/createToastArguments'
 import { CurrentLanguage } from '../../internalisation/stores/currentLanguage'
+import { fromSimulationState } from '../../saving/helpers/fromState'
+import { cleanSimulation } from '../../simulation-actions/helpers/clean'
+import { getSimulationState } from '../../saving/helpers/getState'
 
 /**
  * Compiles a simulation into a logicGate
  *
  * @param simulaton The simulation to compile
  */
-export const compileIc = ({ mode, name, gates }: SimulationState) => {
+export const compileIc = (state: SimulationState) => {
+    const { mode, name, gates } = state
+
     if (mode === 'project') {
         throw new SimulationError('Cannot compile project')
     }
 
     const translation = CurrentLanguage.getTranslation()
-    const inputCount = simulationInputCount(gates)
-    const outputCount = simulationOutputCount(gates)
+
+    const simulation = fromSimulationState(state)
+    cleanSimulation(simulation)
+    const cleanState = getSimulationState(simulation)
+
+    const inputCount = simulationInputCount(cleanState.gates)
+    const outputCount = simulationOutputCount(cleanState.gates)
 
     const result: DeepPartial<GateTemplate> = {
         metadata: {
diff --git a/src/modules/simulation/classes/Gate.ts b/src/modules/simulation/classes/Gate.ts
index d6c3d5a..c3c7456 100644
--- a/src/modules/simulation/classes/Gate.ts
+++ b/src/modules/simulation/classes/Gate.ts
@@ -13,6 +13,7 @@ import { Simulation, SimulationEnv } from './Simulation'
 import { fromSimulationState } from '../../saving/helpers/fromState'
 import { saveStore } from '../../saving/stores/saveStore'
 import { Wire } from './Wire'
+import { cleanSimulation } from '../../simulation-actions/helpers/clean'
 
 export interface GatePins {
     inputs: Pin[]
@@ -116,6 +117,7 @@ export class Gate {
             }
 
             this.ghostSimulation = fromSimulationState(state.simulation, 'gate')
+            cleanSimulation(this.ghostSimulation)
 
             const sortByPosition = (x: Gate, y: Gate) =>
                 x.transform.position[1] - y.transform.position[1]
@@ -136,13 +138,17 @@ export class Gate {
 
             if (inputs.length !== this._pins.inputs.length) {
                 throw new SimulationError(
-                    `Input count needs to match with the container gate`
+                    `Input count needs to match with the container gate: ${
+                        inputs.length
+                    } !== ${this._pins.inputs.length}`
                 )
             }
 
             if (outputs.length !== this._pins.outputs.length) {
                 throw new SimulationError(
-                    `Output count needs to match with the container gate`
+                    `Output count needs to match with the container gate: ${
+                        outputs.length
+                    } !== ${this._pins.outputs.length}`
                 )
             }
 
diff --git a/src/modules/simulationRenderer/classes/SimulationRenderer.ts b/src/modules/simulationRenderer/classes/SimulationRenderer.ts
index baaee2e..eaf286c 100644
--- a/src/modules/simulationRenderer/classes/SimulationRenderer.ts
+++ b/src/modules/simulationRenderer/classes/SimulationRenderer.ts
@@ -318,9 +318,7 @@ export class SimulationRenderer {
             this.loadSave(save)
         } catch (e) {
             throw new Error(
-                `An error occured while loading the save: ${
-                    (e as Error).message
-                }`
+                `An error occured while loading the save: ${e as Error}`
             )
         }
     }