import React, { useContext, useCallback } from 'react';
import { TableKeyboardNavigationContext } from '../../../hooks/use-table-keyboard-navigation';
import { Checkbox } from '../../../checkbox';
import { useTranslation } from '../../../hooks/use-translation';
import { jsx as _jsx } from "react/jsx-runtime";
export function CheckboxHeaderCell({
  table,
  keyboardNavCellId,
  selectAllRowsAriaLabel
}) {
  const {
    t
  } = useTranslation();
  const keyboardNavContext = useContext(TableKeyboardNavigationContext);
  keyboardNavContext?.registerCustomCellAction(keyboardNavCellId, e => {
    table.getToggleAllRowsSelectedHandler()(e);
  });
  return /*#__PURE__*/_jsx(Checkbox, {
    id: `select-all-${table.options.meta?.tableId}`,
    onChange: table.getToggleAllRowsSelectedHandler(),
    isChecked: table.getIsAllRowsSelected(),
    indeterminate: table.getIsSomeRowsSelected(),
    ariaLabel: selectAllRowsAriaLabel || t('Select all rows'),
    isDisabled: !table.options.enableMultiRowSelection,
    tabIndex: -1,
    testId: "select-all-rows-checkbox"
  });
}
export function CheckboxBodyCell({
  row,
  table,
  rowSelectionDisabled,
  keyboardNavCellId,
  selectRowAriaLabel
}) {
  const keyboardNavContext = useContext(TableKeyboardNavigationContext);
  const {
    t
  } = useTranslation();
  keyboardNavContext?.registerCustomCellAction(keyboardNavCellId, e => row.getToggleSelectedHandler()(e));
  const onContainerClickHandler = useCallback(e => {
    // we want to stop the event propagation to avoid the row click handler to be invoked
    // when checkbox is slightly misclicked
    e.stopPropagation();
  }, []);
  return (
    /*#__PURE__*/
    // eslint-disable-next-line jsx-a11y/click-events-have-key-events,jsx-a11y/no-static-element-interactions
    _jsx("div", {
      className: "select-checkbox-cell-wrapper",
      "test-id": "select-checkbox-cell-wrapper",
      onClick: onContainerClickHandler,
      children: /*#__PURE__*/_jsx(Checkbox, {
        id: `select-row-${table.options.meta?.tableId}-${row.id}`,
        testId: `select-row-${row.id}`,
        onChange: row.getToggleSelectedHandler(),
        isChecked: row.getIsSelected(),
        isDisabled: !!rowSelectionDisabled,
        tooltipText: typeof rowSelectionDisabled === 'string' ? rowSelectionDisabled : '',
        ariaLabel: selectRowAriaLabel?.(row) ?? t('Select row {{index}}', {
          index: row.index + 1
        }),
        tabIndex: -1
      })
    })
  );
}