import { cn } from '@/lib/utils';

type Props = {
    /** Number of characters counted. */
    count: number;
    /** Upper end of the recommended length. */
    max: number;
    /** Lower end of the recommended length, when there is one. */
    min?: number;
    className?: string;
};

/**
 * Live character counter of a text with a recommended length. It only
 * advises: amber while the text is shorter than recommended, red once it
 * is longer.
 */
export function CharCount({ count, max, min = 0, className }: Props) {
    const tooLong = count > max;
    const tooShort = count > 0 && count < min;

    return (
        <span
            className={cn(
                'text-xs whitespace-nowrap text-muted-foreground tabular-nums',
                tooShort && 'text-amber-600 dark:text-amber-400',
                tooLong && 'text-red-600 dark:text-red-400',
                className,
            )}
        >
            {count} / {max}
            <span className="sr-only">
                {' '}
                karakter
                {tooLong && ', önerilenden uzun'}
                {tooShort && ', önerilenden kısa'}
            </span>
        </span>
    );
}
