"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TableRowActionsCell = TableRowActionsCell;
var _react = _interopRequireWildcard(require("react"));
var _faEllipsisVertical = require("@fortawesome/free-solid-svg-icons/faEllipsisVertical");
var _dropdownMenu = require("../../../dropdown-menu");
var _fontAwesomeIcon = require("../../../font-awesome-icon");
var _useTableKeyboardNavigation = require("../../../hooks/use-table-keyboard-navigation");
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 TableRowActionsCell({
  row,
  getRowActions,
  getRowFooterAction,
  rowActionsTriggerAriaLabel,
  keyboardNavCellId
}) {
  const {
    t
  } = (0, _useTranslation.useTranslation)();
  const customTriggerRef = (0, _react.useRef)(null);
  const actions = getRowActions(row);
  const footerOption = getRowFooterAction != null ? getRowFooterAction(row) : undefined;
  const keyboardNavContext = (0, _react.useContext)(_useTableKeyboardNavigation.TableKeyboardNavigationContext);
  keyboardNavContext?.registerCustomCellAction(keyboardNavCellId, () => {
    // we have to trigger press manually, in imperative way cause dropdown-menu does not have a possibility to pass ref or
    // control open/closed state from the outside.
    customTriggerRef.current?.dispatchEvent(new KeyboardEvent('keydown', {
      key: 'Enter',
      bubbles: true,
      cancelable: true
    }));
  });
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_dropdownMenu.DropdownMenu, {
    options: actions,
    footerOption: footerOption,
    triggerClassName: "react-table-row-actions-trigger",
    selectedKey: undefined,
    onSelect: selectedOption => {
      selectedOption?.onOptionSelect?.();
    },
    triggerHideChevron: true,
    triggerDynamicContent: () => /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      ref: customTriggerRef,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_fontAwesomeIcon.FontAwesomeIcon, {
        icon: _faEllipsisVertical.faEllipsisVertical
      })
    }),
    triggerIsIcon: true,
    triggerId: `row-actions-dropdown-${row.id}`,
    triggerTestId: "row-actions-dropdown",
    contentTestId: "row-actions-dropdown-content",
    triggerTabIndex: -1,
    triggerAriaLabelText: rowActionsTriggerAriaLabel ?? t('Table row actions dropdown')
  });
}