import { useHeaderRowSelection, useRowSelection } from './hooks';
import type {
  Column,
  RenderCellContentProps,
  RenderGroupCellContentProps,
  RenderHeaderCellContentProps
} from './types';
import { SelectCheckbox } from './cellRenderers';

export const SELECT_COLUMN_KEY = 'rdg-select-column';

function SelectAllCell({ tabIndex }: RenderHeaderCellContentProps<unknown>) {
  const { isIndeterminate, isRowSelected, onRowSelectionChange } = useHeaderRowSelection();

  return (
    <SelectCheckbox
      aria-label="Select All"
      tabIndex={tabIndex}
      indeterminate={isIndeterminate}
      checked={isRowSelected}
      onChange={(checked) => {
        onRowSelectionChange({ checked: isIndeterminate ? false : checked });
      }}
    />
  );
}

function RowSelectCell({ row, tabIndex }: RenderCellContentProps<unknown>) {
  const { isRowSelectionDisabled, isRowSelected, onRowSelectionChange } = useRowSelection();

  return (
    <SelectCheckbox
      aria-label="Select"
      tabIndex={tabIndex}
      disabled={isRowSelectionDisabled}
      checked={isRowSelected}
      onChange={(checked, isShiftClick) => {
        onRowSelectionChange({ row, checked, isShiftClick });
      }}
    />
  );
}

function GroupSelectCell({ row, tabIndex }: RenderGroupCellContentProps<unknown>) {
  const { isRowSelected, onRowSelectionChange } = useRowSelection();

  return (
    <SelectCheckbox
      aria-label="Select Group"
      tabIndex={tabIndex}
      checked={isRowSelected}
      onChange={(checked) => {
        onRowSelectionChange({ row, checked, isShiftClick: false });
      }}
    />
  );
}

// oxlint-disable-next-line typescript/no-explicit-any
export const SelectColumn: Column<any, any> = {
  key: SELECT_COLUMN_KEY,
  name: '',
  width: 35,
  minWidth: 35,
  maxWidth: 35,
  resizable: false,
  sortable: false,
  frozen: true,
  renderHeaderCell(props) {
    return <SelectAllCell {...props} />;
  },
  renderCell(props) {
    return <RowSelectCell {...props} />;
  },
  renderGroupCell(props) {
    return <GroupSelectCell {...props} />;
  }
};
