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

interface DataTableColumnDatetimeProps<TData, TValue>
  extends React.HTMLAttributes<HTMLDivElement> {
  row: Row<TData>
  column: Column<TData, TValue>
}

const formatted = new Intl.DateTimeFormat('pt-BR', {
  day: '2-digit',
  month: '2-digit',
  year: 'numeric',
  hour: '2-digit',
  minute: '2-digit'
})

export function DataTableColumnDatetime<TData, TValue>({
  row,
  column
}: DataTableColumnDatetimeProps<TData, TValue>) {
  // @ts-ignore
  const { accessorKey } = column.columnDef
  const value = row.getValue(accessorKey)

  if (value) {
    return formatted.format(new Date(value as string))
  }

  return <></>
}
