import { ToggleGroup, ToggleGroupItem } from '@/components/ui/toggle-group';
import { cn } from '@/lib/utils';
import type { LocaleOption } from '@/types';

type Props = {
    locales: LocaleOption[];
    /** Code of the language being edited. */
    value: string;
    onChange: (code: string) => void;
    /** Languages in which a required translation is still missing. */
    missing?: string[];
    /** Languages that contain a validation error. */
    invalid?: string[];
    className?: string;
};

/**
 * The language switch of an edit form (TR | EN). It switches every
 * translatable field of the page at once. A dot on a tab tells that the
 * language needs attention: red for a validation error, amber for a
 * required translation that is still missing.
 */
export function LocaleTabs({
    locales,
    value,
    onChange,
    missing = [],
    invalid = [],
    className,
}: Props) {
    return (
        <ToggleGroup
            type="single"
            variant="outline"
            size="sm"
            value={value}
            onValueChange={(code) => {
                // Radix reports '' when the active item is pressed again:
                // one language always stays selected.
                if (code !== '') {
                    onChange(code);
                }
            }}
            aria-label="Düzenlenen dil"
            // As wide as its tabs, also where its parent would stretch it.
            className={cn('w-fit', className)}
        >
            {locales.map((locale) => {
                const marker = invalid.includes(locale.code)
                    ? { color: 'bg-red-500', text: 'hatalı alan var' }
                    : missing.includes(locale.code)
                      ? { color: 'bg-amber-500', text: 'çeviri eksik' }
                      : null;

                return (
                    <ToggleGroupItem
                        key={locale.code}
                        value={locale.code}
                        title={
                            marker
                                ? `${locale.name}: ${marker.text}`
                                : locale.name
                        }
                        className="gap-1.5 px-3"
                    >
                        {locale.short}
                        {marker && (
                            <>
                                <span
                                    aria-hidden="true"
                                    className={cn(
                                        'size-1.5 rounded-full',
                                        marker.color,
                                    )}
                                />
                                <span className="sr-only">({marker.text})</span>
                            </>
                        )}
                    </ToggleGroupItem>
                );
            })}
        </ToggleGroup>
    );
}

/** The small language tag shown next to the label of a translatable field. */
export function LocaleBadge({ locale }: { locale: LocaleOption }) {
    return (
        <span
            title={`${locale.name} çevirisi`}
            className="ml-1.5 rounded border px-1 py-px align-middle text-[0.65rem] leading-none font-medium text-muted-foreground"
        >
            {locale.short}
        </span>
    );
}
