import {
  DEFAULT_SUPER_RESOLUTION_EXPECTED_DURATION_MS,
  useSmoothedProgress,
  type ProgressSource,
} from "../../hooks/useSmoothedProgress";

type MessageStatus = "thinking" | "completed" | "failed" | string;

interface SmoothedProgressBarProps {
  progress: number;
  status: MessageStatus;
  label?: string;
  progressSource?: ProgressSource;
  startedAt?: number | string | Date | null;
  expectedDurationMs?: number | null;
}

function mapMessageStatus(status: MessageStatus) {
  if (status === "thinking") return "running" as const;
  if (status === "completed") return "completed" as const;
  if (status === "failed") return "failed" as const;
  return "running" as const;
}

export function SmoothedProgressBar({
  progress,
  status,
  label,
  progressSource = "estimated",
  startedAt,
  expectedDurationMs = DEFAULT_SUPER_RESOLUTION_EXPECTED_DURATION_MS,
}: SmoothedProgressBarProps) {
  const smoothed = useSmoothedProgress(progress, mapMessageStatus(status), {
    progressSource,
    startedAt,
    expectedDurationMs,
  });
  return (
    <>
      <span>{label || "超分处理中..."}</span>
      <strong>{smoothed}%</strong>
      <i style={{ width: `${smoothed}%` }} />
    </>
  );
}
