"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TableHeaderCellMenu = TableHeaderCellMenu;
var _react = _interopRequireDefault(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _faEllipsisVertical = require("@fortawesome/free-solid-svg-icons/faEllipsisVertical");
var _useTranslation = require("../../hooks/use-translation");
var _fontAwesomeIcon = require("../../font-awesome-icon");
var _dropdownMenu = require("../../dropdown-menu");
var _getHeaderColumnsActions = require("../utils/getHeaderColumnsActions");
var _types = require("../types");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function TableHeaderCellMenu({
  header,
  tableContainerWidth,
  isVisible = false,
  localizationText
}) {
  const {
    t
  } = (0, _useTranslation.useTranslation)();
  const defaultHeaderText = {
    sortAscendingText: t('Sort ascending'),
    sortDescendingText: t('Sort descending'),
    pinText: t('Pin column'),
    unPinText: t('Unpin column'),
    disabledPinText: t('Pinned columns will take too much space with current column pinned'),
    headerActionsAriaLabel: t('Column header actions dropdown')
  };
  const localizationTextWithDefault = localizationText ?? defaultHeaderText;
  const canSort = header.column.getCanSort();
  const canPin = header.column.getCanPin();
  const isPinned = !!header.column.getIsPinned();
  const limitSorting = header.column.columnDef?.meta?.limitSorting;
  const isPinDisabled = !isPinned && (header.column.getSize() + header.getContext().table.getLeftTotalSize()) / tableContainerWidth > _types.PINNED_WIDTH_THRESHOLD;
  const sorted = header.column.getIsSorted();
  const menuActions = (0, _getHeaderColumnsActions.getHeaderColumnsActions)({
    isPinned,
    canSort,
    canPin,
    isPinDisabled,
    limitSorting,
    customActions: header.column?.columnDef?.meta?.additionalColumnActions ?? [],
    localizationText: localizationTextWithDefault
  });
  if (!menuActions.length) {
    return null;
  }
  const actionsHandlers = {
    [_types.SORTING.DESC]: () => header.column.toggleSorting(true),
    [_types.SORTING.ASC]: () => header.column.toggleSorting(false),
    [_types.ACTIONS.PIN]: () => header.column.pin('left'),
    [_types.ACTIONS.UNPIN]: () => header.column.pin(false),
    ...(header.column.columnDef?.meta?.additionalColumnActions ?? []).reduce((acc, customAction) => {
      acc[customAction.key] = customAction.onOptionSelect;
      return acc;
    }, {})
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
    className: "menu-wrapper",
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_dropdownMenu.DropdownMenu, {
      options: menuActions,
      triggerClassName: (0, _classnames.default)('react-table-cell-menu-trigger', {
        'focused-visible': isVisible
      }),
      contentClassName: "react-table-cell-menu-content",
      selectedKey: sorted || undefined,
      onSelect: selectedOption => {
        const key = selectedOption?.key;
        const handler = actionsHandlers[key] || (() => {});
        handler();
      },
      triggerHideChevron: true,
      triggerDynamicContent: () => /*#__PURE__*/(0, _jsxRuntime.jsx)(_fontAwesomeIcon.FontAwesomeIcon, {
        icon: _faEllipsisVertical.faEllipsisVertical
      }),
      triggerIsIcon: true,
      triggerId: `header-menu-actions-${header.column.id}`,
      triggerTestId: "header-menu-actions",
      triggerAriaLabelText: localizationTextWithDefault?.headerActionsAriaLabel,
      contentTestId: "header-menu-actions-content"
    })
  });
}