import { LocaleTabs } from '@/components/admin/locale-tabs';
import { Button } from '@/components/ui/button';
import { Spinner } from '@/components/ui/spinner';
import { localesWithErrors } from '@/lib/translations';
import type { FormErrors, LocaleOption } from '@/types';

type Props = {
    locales: LocaleOption[];
    /** The language being edited. */
    locale: LocaleOption;
    onLocaleChange: (code: string) => void;
    /** Languages in which a required translation is still missing. */
    missing?: string[];
    /** Validation errors of the last save, keyed by field path. */
    errors: FormErrors;
    processing: boolean;
    /** Whether the form holds changes that are not saved yet. */
    dirty: boolean;
    saveLabel?: string;
};

/**
 * The bar on top of an edit form. It stays in view while the form scrolls
 * and carries what concerns the whole form: the language switch, the save
 * button and whether the last save went through.
 *
 * On a phone the switch and the button share one row and the status moves
 * below them, so the bar stays small.
 */
export function SaveBar({
    locales,
    locale,
    onLocaleChange,
    missing,
    errors,
    processing,
    dirty,
    saveLabel = 'Kaydet',
}: Props) {
    const errorCount = Object.keys(errors).length;

    return (
        <div className="sticky top-0 z-20 -mx-4 flex flex-wrap items-center gap-x-3 gap-y-2 border-b bg-background/95 px-4 py-3 backdrop-blur supports-[backdrop-filter]:bg-background/80 md:-mx-6 md:px-6">
            <LocaleTabs
                locales={locales}
                value={locale.code}
                onChange={onLocaleChange}
                missing={missing}
                invalid={localesWithErrors(locales, errors)}
                className="order-1"
            />

            <p className="order-2 hidden text-xs text-muted-foreground lg:block">
                <span className="font-medium text-foreground">
                    {locale.name}
                </span>{' '}
                çevirisini düzenliyorsunuz. Dil etiketi taşıyan alanlar her dil
                için ayrı tutulur.
            </p>

            <p
                aria-live="polite"
                className="order-4 w-full text-right text-xs sm:order-3 sm:ms-auto sm:w-auto"
            >
                {errorCount > 0 ? (
                    <span className="text-red-600 dark:text-red-400">
                        Kaydedilmedi: {errorCount} alanda hata var
                    </span>
                ) : (
                    dirty &&
                    !processing && (
                        <span className="text-amber-700 dark:text-amber-400">
                            Kaydedilmemiş değişiklikler var
                        </span>
                    )
                )}
            </p>

            <Button
                type="submit"
                disabled={processing}
                className="order-3 ms-auto sm:order-4 sm:ms-0"
            >
                {processing && <Spinner />}
                {saveLabel}
            </Button>
        </div>
    );
}
