import type { Table, Row } from '@tanstack/react-table'

import { Checkbox } from '../ui/checkbox'

interface DataTableColumnHeaderSelectProps<TData>
  extends React.HTMLAttributes<HTMLDivElement> {
  table: Table<TData>
}

export function DataTableColumnHeaderSelect<TData>({
  table
}: DataTableColumnHeaderSelectProps<TData>) {
  return (
    <Checkbox
      checked={
        table.getIsAllPageRowsSelected() ||
        (table.getIsSomePageRowsSelected() && 'indeterminate')
      }
      onCheckedChange={(value) => table.toggleAllPageRowsSelected(!!value)}
      className="cursor-pointer"
      aria-label="Selecionar tudo"
    />
  )
}

interface DataTableColumnSelectProps<TData>
  extends React.HTMLAttributes<HTMLDivElement> {
  row: Row<TData>
}

export function DataTableColumnSelect<TData>({
  row
}: DataTableColumnSelectProps<TData>) {
  return (
    <Checkbox
      checked={row.getIsSelected()}
      disabled={!row.getCanSelect()}
      onCheckedChange={(value) => row.toggleSelected(!!value)}
      className="cursor-pointer"
      aria-label="Selecionar linha"
    />
  )
}
