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

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

  var position = getPositionFromAccessor('accessor', accessor, data, logger)
  checkValidPose('accessor', position)
  var positionAdjusted = { ...position }

  let offsetArranged = arrangeOffset(pose, logger)

  for (var idx in positionAdjusted) {
    positionAdjusted[idx] += offsetArranged[idx]
  }

  return {
    data: {
      ...positionAdjusted
    }
  }
}

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

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

TaskRegistry.registerTaskHandler('adjust-pose-offset', AdjustPoseOffset)
