import type { Table } from '@tanstack/react-table';
import type { ReactNode } from 'react';
import { DataTableViewOptions } from '@/components/data-table/data-table-view-options';
import { Input } from '@/components/ui/input';

type Props<TData> = {
    table: Table<TData>;
    search?: {
        value: string;
        onChange: (value: string) => void;
        placeholder?: string;
    };
    filters?: ReactNode;
    actions?: ReactNode;
    /** Masque le bouton « Colonnes » (petits tableaux). */
    compact?: boolean;
};

export function DataTableToolbar<TData>({
    table,
    search,
    filters,
    actions,
    compact = false,
}: Props<TData>) {
    return (
        <div className="flex flex-col gap-3 sm:flex-row sm:items-center">
            {search ? (
                <Input
                    placeholder={search.placeholder ?? 'Rechercher…'}
                    value={search.value}
                    onChange={(event) => search.onChange(event.target.value)}
                    className="h-10 bg-card sm:max-w-sm sm:flex-1"
                />
            ) : (
                <div />
            )}
            <div className="flex flex-wrap items-center gap-2 sm:ml-auto">
                {filters}
                {!compact && <DataTableViewOptions table={table} />}
                {actions}
            </div>
        </div>
    );
}
