import React, { useContext, useRef } from 'react';
import { faEllipsisVertical } from '@fortawesome/free-solid-svg-icons/faEllipsisVertical';
import { DropdownMenu } from '../../../dropdown-menu';
import { FontAwesomeIcon } from '../../../font-awesome-icon';
import { TableKeyboardNavigationContext } from '../../../hooks/use-table-keyboard-navigation';
import { useTranslation } from '../../../hooks/use-translation';
import { jsx as _jsx } from "react/jsx-runtime";
export function TableRowActionsCell({
  row,
  getRowActions,
  getRowFooterAction,
  rowActionsTriggerAriaLabel,
  keyboardNavCellId
}) {
  const {
    t
  } = useTranslation();
  const customTriggerRef = useRef(null);
  const actions = getRowActions(row);
  const footerOption = getRowFooterAction != null ? getRowFooterAction(row) : undefined;
  const keyboardNavContext = useContext(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__*/_jsx(DropdownMenu, {
    options: actions,
    footerOption: footerOption,
    triggerClassName: "react-table-row-actions-trigger",
    selectedKey: undefined,
    onSelect: selectedOption => {
      selectedOption?.onOptionSelect?.();
    },
    triggerHideChevron: true,
    triggerDynamicContent: () => /*#__PURE__*/_jsx("div", {
      ref: customTriggerRef,
      children: /*#__PURE__*/_jsx(FontAwesomeIcon, {
        icon: 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')
  });
}