const QUERY_CANDIDATE_ACTIONS = new Set(['delete', 'approve', 'reject', 'query'])

export function resolveWorkbenchIntentActionRoute(frame = null) {
  if (!frame) {
    return { nextStep: 'pass_through' }
  }
  if (frame.safetyLevel === 'blocked') {
    return { nextStep: 'blocked', reason: '高风险批量动作需要先选择具体单据。' }
  }
  if (frame.action === 'ask_policy') {
    return { nextStep: 'pass_through' }
  }
  if (frame.policyDecision === 'need_confirmation') {
    return {
      nextStep: 'open_context_confirm',
      riskLevel: frame.riskLevel,
      requiresSelection: Boolean(frame.requiresSelection),
      requiresConfirmation: Boolean(frame.requiresConfirmation)
    }
  }
  if (frame.policyDecision === 'query_candidates' && QUERY_CANDIDATE_ACTIONS.has(frame.action)) {
    return {
      nextStep: 'query_candidates',
      queryPrompt: frame.normalizedQuery || '',
      riskLevel: frame.riskLevel,
      requiresSelection: Boolean(frame.requiresSelection),
      requiresConfirmation: Boolean(frame.requiresConfirmation)
    }
  }
  return { nextStep: 'pass_through' }
}
