const CC_DISPLAY_NAMES = {
  Bash: 'run command',
  WebSearch: 'web search',
  WebFetch: 'fetch',
  Read: 'read',
  Write: 'write',
  Edit: 'edit',
  MultiEdit: 'edit',
  NotebookEdit: 'edit notebook',
  Glob: 'find files',
  Grep: 'search files',
  LS: 'list',
  TodoWrite: 'plan',
  Task: 'subagent',
  Skill: 'skill',
  SlashCommand: 'command',
  ExitPlanMode: 'exit plan',
  AskUserQuestion: 'ask',
};

function basename(p) {
  if (!p) return '';
  const s = String(p);
  return s.split('/').filter(Boolean).pop() || s;
}

function hostname(u) {
  try { return new URL(String(u)).hostname; } catch { return String(u).slice(0, 52); }
}

export function toolStepDisplayName(name) {
  if (!name) return '';
  if (CC_DISPLAY_NAMES[name]) return CC_DISPLAY_NAMES[name];
  if (typeof name === 'string' && name.startsWith('mcp__')) {
    const parts = name.split('__');
    return parts[parts.length - 1].replace(/_/g, ' ');
  }
  return name.replace(/_/g, ' ');
}

export function toolStepSublabel(name, input) {
  if (!input) return null;
  // madarX orchestrator tools
  if (name === 'web_search' && input.query) return `"${String(input.query).slice(0, 52)}"`;
  if (name === 'web_fetch' && input.url) return hostname(input.url);
  if (name === 'browser_harness_run' && input.script) return 'browser script';
  if (name === 'create_artifact' && input.title) return `"${String(input.title).slice(0, 40)}"`;
  if (name === 'create_agent' && input.name) return `"${String(input.name).slice(0, 40)}"`;
  if ((name === 'file_read' || name === 'file_write') && input.path) {
    return basename(input.path).slice(0, 40);
  }
  // Claude Code native tools
  if (name === 'Bash' && input.command) return String(input.command).slice(0, 80);
  if (name === 'WebSearch' && input.query) return `"${String(input.query).slice(0, 52)}"`;
  if (name === 'WebFetch' && input.url) return hostname(input.url);
  if ((name === 'Read' || name === 'Write' || name === 'Edit' || name === 'MultiEdit' || name === 'NotebookEdit') && input.file_path) {
    return basename(input.file_path).slice(0, 40);
  }
  if ((name === 'Glob' || name === 'Grep') && (input.pattern || input.path)) {
    return String(input.pattern || input.path).slice(0, 52);
  }
  if (name === 'LS' && input.path) return basename(input.path).slice(0, 40) || '/';
  if (name === 'TodoWrite' && Array.isArray(input.todos)) return `${input.todos.length} item${input.todos.length === 1 ? '' : 's'}`;
  if (name === 'Task' && input.description) return `"${String(input.description).slice(0, 52)}"`;
  if (name === 'Skill' && input.skill) return `/${input.skill}`;
  if (name === 'SlashCommand' && input.command) return String(input.command).slice(0, 52);
  return null;
}
