import {
  DataTablaRowActionFunction,
  DataTableRowActions,
  DataTableRowDragHandleCell,
} from "@/components";
import { Checkbox } from "@/ui";
import { ColumnDef, Row } from "@tanstack/react-table";

import { useMemo } from "react";

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function useDetailColumns<TData = unknown, TValue = unknown>(
  columns: ColumnDef<TData>[],
  options: {
    enableDragHandleColumn?: boolean;
    enableSelectionColumn?: boolean;
    enableActionsColumn?: boolean;
    rowActionFn?: DataTablaRowActionFunction<TData>;
  } = {}
): ColumnDef<TData, TValue>[] {
  const {
    enableDragHandleColumn = false,
    enableSelectionColumn = false,
    enableActionsColumn = false,
    rowActionFn = undefined,
  } = options;

  // const lastSelectedId = "";

  return useMemo(() => {
    if (enableDragHandleColumn) {
      columns.unshift({
        id: "row_drag_handle",
        /*header: () => (
          <UnfoldVertical aria-label='Mover fila' className='items-center justify-center w-4 h-4' />
        ),*/
        header: () => null,
        cell: (info) => <DataTableRowDragHandleCell rowId={info.row.id} />,
        enableSorting: false,
        enableHiding: false,
        size: 40,
      });
    }

    if (enableSelectionColumn) {
      columns.unshift({
        id: "select",
        header: ({ table }) => (
          <Checkbox
            id='select-all'
            checked={
              table.getIsAllPageRowsSelected() ||
              (table.getIsSomePageRowsSelected() && "indeterminate")
            }
            onCheckedChange={(value) => table.toggleAllPageRowsSelected(!!value)}
            aria-label='Seleccionar todo'
            className='translate-y-[0px]'
          />
        ),

        cell: ({ row }: { row: Row<TData> }) => (
          <Checkbox
            id={`select-row-${row.id}`}
            checked={row.getIsSelected()}
            disabled={!row.getCanSelect()}
            onCheckedChange={row.getToggleSelectedHandler()}
            aria-label='Seleccionar fila'
            className='mt-2'
          />
        ),
        enableSorting: false,
        enableHiding: false,
        size: 40,
      });
    }

    if (enableActionsColumn) {
      columns.push({
        id: "row_actions",
        /*header: () => (
          <MoreHorizontalIcon
            aria-label='Acciones'
            className='items-center justify-center w-4 h-4'
          />
        ),*/
        cell: (props) => {
          return <DataTableRowActions rowContext={props} actions={rowActionFn} />;
        },
        enableSorting: false,
        enableHiding: false,
        size: 48,
      });
    }

    return columns;
  }, [enableActionsColumn, enableDragHandleColumn, enableSelectionColumn]);
}
