import { TaskRegistry } from '@things-factory/integration-base'
import {checkValidPose, getPositionFromAccessor, prunePose} from './util'

async function AdjustPose(step, { logger, data }) {
  var {
    params: { accessor, overrider, pose }
  } = step

  var position = getPositionFromAccessor('accessor', accessor, data, logger)
  checkValidPose('accessor', position)

  var overposition = overrider ? getPositionFromAccessor('overrider', overrider, data, logger) : {}

  return {
    data: {
      ...position,
      ...prunePose('overrider', overposition, logger),
      ...prunePose('pose', { ...pose }, logger)
    }
  }
}

AdjustPose.parameterSpec = [
  {
    type: 'scenario-step-input',
    label: 'accessor',
    name: 'accessor'
  },
  {
    type: 'scenario-step-input',
    label: 'overrider',
    name: 'overrider'
  },
  {
    type: 'pose',
    label: 'pose',
    name: 'pose'
  }
]

AdjustPose.connectorFree = true
AdjustPose.help = 'robotics/task/adjust-pose'

TaskRegistry.registerTaskHandler('adjust-pose', AdjustPose)
