import { ROBOTICS_OBJECT_TYPES } from '../../../controllers/robotics-types'
import { ConnectionManager } from '@things-factory/integration-base'

export const Query = {
  async trackableObject(_: any, { name }, context: any) {
    const { domain } = context.state

    var connection = ConnectionManager.getConnectionInstanceByName(domain, name)
    if (!connection || connection.discriminator !== ROBOTICS_OBJECT_TYPES.OBJECT) {
      throw Error(`TrackableObject '${name}' Not Found`)
    }

    var { poiOffset } = connection.params || {}

    return {
      ...connection,
      poiOffset
    }
  },

  async trackableObjects(_: any, {}, context: any) {
    const { domain } = context.state

    var items = Object.keys(ConnectionManager.getConnectionInstances(domain))
      .map(name => ConnectionManager.getConnectionInstanceByName(domain, name))
      .filter(connection => connection.discriminator == ROBOTICS_OBJECT_TYPES.OBJECT)
      .map(connection => {
        var { poiOffset } = connection.params || {}

        return {
          ...connection,
          poiOffset
        }
      })

    return {
      items,
      total: items.length
    }
  }
}
