import { ConnectionManager, TaskRegistry } from '@things-factory/integration-base'
import { getTrackingWorkspace } from './get-tracking-workspace'

async function TrackableObjectGetState(step, { logger, domain }) {
  var { connection } = step

  var object = ConnectionManager.getConnectionInstanceByName(domain, connection) || {}
  if (!object) {
    throw new Error(`no connection : ${connection}`)
  }

  var { name, endpoint } = object

  var workspace = getTrackingWorkspace(domain)
  var { engine } = workspace
  var { trackingStorage } = engine

  return {
    data: trackingStorage.getObjectState(endpoint)
  }
}

TrackableObjectGetState.parameterSpec = []
TrackableObjectGetState.help = 'robotics/task/trackable-object-get-state'

TaskRegistry.registerTaskHandler('trackable-object-get-state', TrackableObjectGetState)
