import type { Row, Column } from '@tanstack/react-table'
import {
  formatCPF,
  formatCNPJ,
  isValidCPF,
  isValidCNPJ
} from '@brazilian-utils/brazilian-utils'

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

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

  if (isValidCPF(value)) {
    return formatCPF(value)
  }

  if (isValidCNPJ(value)) {
    return formatCNPJ(value)
  }

  return <></>
}
