'use client';

import { useLocale, useTranslations } from 'next-intl';
import { formatCaseSentLines } from '@/components/treatment/caseSendLabel';
import type { LabCaseSendInfo, LinkedOrganizationOption } from '@/types/treatment';

interface CaseSentLabelProps {
  treatmentCase: {
    sends?: LabCaseSendInfo[];
    sendToOrganizationIds?: string[];
    destinationOrganizationId?: string | null;
    sentAt?: string | null;
  };
  orgs?: LinkedOrganizationOption[];
  className?: string;
}

export function CaseSentLabel({ treatmentCase, orgs, className = 'text-xs text-text-muted' }: CaseSentLabelProps) {
  const locale = useLocale();
  const t = useTranslations('treatment');
  const organizationIds =
    treatmentCase.sendToOrganizationIds ??
    (treatmentCase.destinationOrganizationId ? [treatmentCase.destinationOrganizationId] : []);
  const lines = formatCaseSentLines(treatmentCase.sends, {
    organizationIds,
    sentAt: treatmentCase.sentAt ?? null,
    orgs,
  }, t, locale);

  if (lines.length === 0) return null;

  return (
    <div className={className}>
      {lines.map((line, i) => (
        <span key={`${line}-${i}`} className="block">
          {line}
        </span>
      ))}
    </div>
  );
}
