import _specs from './system/_specs'
import { BaseSpec, GraphSpec, Spec } from './types'
import { BundleSpec } from './types/BundleSpec'

export function buildIdSet(
  spec: Spec,
  idSet: Set<string> = new Set()
): Set<string> {
  const { units = {} } = spec as GraphSpec

  for (const unitId in units) {
    const unit = units[unitId]

    const { id: unit_id } = unit

    const unit_spec = _specs[unit_id]

    if (unit_spec) {
      idSet.add(unit_id)

      buildIdSet(unit_spec, idSet)
    } else {
      //
    }
  }

  ;(spec as BaseSpec).deps?.forEach((dep_id) => {
    const dep_spec = _specs[dep_id]

    idSet.add(dep_id)

    buildIdSet(dep_spec, idSet)
  })

  return idSet
}

export function buildIdSetFromBundle(
  bundle: BundleSpec,
  idSet: Set<string> = new Set()
): Set<string> {
  const { spec = {}, specs = {} } = bundle

  buildIdSet(spec, idSet)

  for (const specId in specs) {
    const spec = specs[specId]

    buildIdSet(spec, idSet)
  }

  return idSet
}
