import React, { useMemo } from 'react';
import { MessageSquare } from 'lucide-react-native';

import type { SuperagentToolRendererProps } from '../../types';
import { ToolWidgetRow } from '../primitives/ToolWidgetRow';
import { getChannelConnectionOptionsTitle, getChannelDisplayName, getFailureReason } from '../skillsAndConnectionsUtils';
import { getSettledWidgetStatus, parseToolArgs } from '../toolWidgetUtils';

/**
 * show_channel_connection_options — superagent-only (channel_tools.py). The
 * actual QR/deep-link widget renders in the builder web chat; here we show
 * which channel(s) the agent offered, in the spirit of the web's
 * SetupChannelConnection header.
 */
export function ShowChannelConnectionOptionsWidget({ isLastAssistantMessage, toolCall }: SuperagentToolRendererProps) {
  const args = useMemo(() => parseToolArgs(toolCall), [toolCall]);
  // Settled status keeps stale rows on old turns from spinning forever;
  // stopped renders as an error row so it never reads as completed.
  const status = getSettledWidgetStatus(toolCall, isLastAssistantMessage);
  const channelName = getChannelDisplayName(args);
  const title = getChannelConnectionOptionsTitle(status);

  return (
    <ToolWidgetRow
      chipText={channelName ?? 'all channels'}
      failureText={getFailureReason(status, toolCall)}
      icon={MessageSquare}
      status={status === 'stopped' ? 'error' : status}
      title={title}
    />
  );
}
