import type { ReactNode } from 'react';
import { useSidebar } from '@/components/ui/sidebar';
import { cn } from '@/lib/utils';

type Props = {
    count: number;
    label?: string;
    children: ReactNode;
    className?: string;
};

export function DataTableBulkBar({
    count,
    label = 'élément',
    children,
    className,
}: Props) {
    if (count === 0) {
        return null;
    }

    const plural = count > 1 ? `${label}s` : label;
    const { isMobile, state } = useSidebar();

    return (
        <div
            className={cn(
                'pointer-events-none fixed right-0 bottom-6 z-50 flex justify-center px-4 transition-[left] duration-200 ease-linear',
                isMobile
                    ? 'left-0'
                    : state === 'collapsed'
                      ? 'left-[calc(var(--sidebar-width-icon)+(--spacing(4)))]'
                      : 'left-(--sidebar-width)',
                className,
            )}
        >
            <div className="pointer-events-auto flex flex-wrap items-center gap-2 rounded-full bg-primary px-4 py-2.5 text-primary-foreground shadow-lg">
                <span className="text-sm font-medium whitespace-nowrap">
                    {count} {plural} sélectionné{count > 1 ? 's' : ''}
                </span>
                <span className="mx-1 hidden h-5 w-px bg-primary-foreground/20 sm:block" />
                <div className="flex flex-wrap items-center gap-1">
                    {children}
                </div>
            </div>
        </div>
    );
}
