import { combineLatestObj } from '../../utils/obsUtils'

export default function makeTypeInstanceMapping (instances$, types$) {
  // register type=> instance & vice versa
  // let base = {typeUidFromInstUid: {}, instUidFromTypeUid: {}}

  instances$ = instances$.map(function (instances) {
    let res = []
    Object.keys(instances).map(function (key) {
      res.push(instances[key])
    })
    return res
  })

  return combineLatestObj({instances$, types$})
    .map(function ({instances, types}) {
      let instUidFromTypeUid = instances
        .reduce(function (prev, instance) {
          if (!prev[instance.typeUid]) {
            prev[instance.typeUid] = [] // instance.id
          }

          prev[instance.typeUid].push(instance.id)
          return prev
        }, {})

      let typeUidFromInstUid = instances
        .reduce(function (prev, instance) {
          prev[instance.id] = instance.typeUid
          return prev
        }, {})

      return {instUidFromTypeUid, typeUidFromInstUid}
    })
}
