import { GraphSpec, GraphUnitSpec } from '../types'
import { stringifyDataObj } from '../types/stringifyPinData'
import { UnitBundleSpec } from '../types/UnitBundleSpec'

export function stringifyGraphSpecData(spec: GraphSpec): void {
  for (const unitId in spec.units) {
    stringifyGraphUnitSpecData(spec.units[unitId])
  }
}

export function stringifyGraphUnitSpecData(unit: GraphUnitSpec): void {
  const { memory } = unit

  if (memory) {
    for (const inputId in memory.input) {
      const input = memory.input[inputId]

      memory.input[inputId] = stringifyDataObj(input)
    }

    for (const outputId in memory.output) {
      const output = memory.output[outputId]

      memory.output[outputId] = stringifyDataObj(output)
    }

    for (const stateId in memory.memory) {
      const state = memory.memory[stateId]

      memory.memory[stateId] = stringifyDataObj(state)
    }
  }
}

export const stringifyBundleSpec = (spec: UnitBundleSpec): void => {
  const { unit, specs } = spec

  stringifyGraphUnitSpecData(unit)
}
