'use client';

import { Text } from '@/components/ui/typography';

import { BarList, formatNumber, formatPercent } from './charts';
import type { TitleMotifs as TitleMotifsData } from './types';

type Topic = 'seasons' | 'animals';

/**
 * The overall ranking of a title motif; each count is a creation record, not a unique title.
 * The page sends the rankings only, without the generated example titles and branch winners.
 */
export default function TitleMotifs({ motifs, topic }: { motifs: TitleMotifsData; topic: Topic }) {
  const { ranking, matched } = motifs[topic];
  const total = motifs.total;

  return (
    <div>
      {ranking.length ? (
        <BarList items={ranking.slice(0, topic === 'seasons' ? 4 : 12)} />
      ) : (
        <Text tone="muted">Nincs beszámított találat.</Text>
      )}

      <Text variant="caption" tone="muted" className="mt-4">
        {formatNumber(matched)} alkotás címében, az összes alkotásainak{' '}
        {formatPercent(matched / (total || 1))}-ában.
      </Text>
    </div>
  );
}
