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

export const Mutation = {
  async updateTrackingWorkspaceStatus(_: any, { name, status }, context: any) {
    const { domain } = context.state

    var workspace = ConnectionManager.getConnectionInstanceByName(domain, name)
    if (!workspace || workspace.discriminator !== ROBOTICS_OBJECT_TYPES.WORKSPACE) {
      throw Error(`Tracking Workspace '${name}' Not Found`)
    }

    var { engine, params } = workspace
    var storage = engine.trackingStorage
    var { objectStatus } = status

    objectStatus.forEach(objectState => {
      var { id, state } = objectState
      var { roi, pose } = state || {}

      storage.updateObjectState(id, roi, pose, params?.poseThreshold)
    })

    return true
  }
}
