"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.CheckboxBodyCell = CheckboxBodyCell;
exports.CheckboxHeaderCell = CheckboxHeaderCell;
var _react = _interopRequireWildcard(require("react"));
var _useTableKeyboardNavigation = require("../../../hooks/use-table-keyboard-navigation");
var _checkbox = require("../../../checkbox");
var _useTranslation = require("../../../hooks/use-translation");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function CheckboxHeaderCell({
  table,
  keyboardNavCellId,
  selectAllRowsAriaLabel
}) {
  const {
    t
  } = (0, _useTranslation.useTranslation)();
  const keyboardNavContext = (0, _react.useContext)(_useTableKeyboardNavigation.TableKeyboardNavigationContext);
  keyboardNavContext?.registerCustomCellAction(keyboardNavCellId, e => {
    table.getToggleAllRowsSelectedHandler()(e);
  });
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_checkbox.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"
  });
}
function CheckboxBodyCell({
  row,
  table,
  rowSelectionDisabled,
  keyboardNavCellId,
  selectRowAriaLabel
}) {
  const keyboardNavContext = (0, _react.useContext)(_useTableKeyboardNavigation.TableKeyboardNavigationContext);
  const {
    t
  } = (0, _useTranslation.useTranslation)();
  keyboardNavContext?.registerCustomCellAction(keyboardNavCellId, e => row.getToggleSelectedHandler()(e));
  const onContainerClickHandler = (0, _react.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
    (0, _jsxRuntime.jsx)("div", {
      className: "select-checkbox-cell-wrapper",
      "test-id": "select-checkbox-cell-wrapper",
      onClick: onContainerClickHandler,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_checkbox.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
      })
    })
  );
}