import {
  canonicalToolName,
  isDelegateToolName,
  isExecLikeToolName,
  toolDisplayLabel as canonicalToolDisplayLabel,
} from '#cli/core/tool-metadata'
import { t } from '#cli/i18n'

const WORKSPACE_NAMESPACE_CHILD_TOOL_NAMES = new Set([
  'workspace-search',
  'content-search',
  'read-file',
  'read-file-range',
  'write-workspace',
  'write-workspace-batch',
  'edit-workspace',
  'multiedit-workspace',
  'verify-swift-package',
])

export function normalizeToolName(toolName: string): string {
  return canonicalToolName(toolName)
}

export function isBashTool(toolName: string): boolean {
  return isExecLikeToolName(toolName)
}

export function isInlinePlanMarkdownToolName(toolName: string): boolean {
  const n = normalizeToolName(toolName)
  return n === 'todoplan' || n === 'plan_mode_todoplan' || n === 'plan_write'
}

export function isChecklistPlanToolName(toolName: string): boolean {
  const n = normalizeToolName(toolName)
  return n === 'todoplan' || n === 'plan_mode_todoplan'
}

export function isWorkspaceNamespaceChildToolName(toolName: string): boolean {
  return WORKSPACE_NAMESPACE_CHILD_TOOL_NAMES.has(normalizeToolName(toolName))
}

export function toolDisplayName(toolName: string): string {
  const normalized = normalizeToolName(toolName)
  if (isDelegateToolName(normalized)) return t('chatRender.toolName.subagent')
  return canonicalToolDisplayLabel(normalized)
}
