import React from 'react';
import classnames from 'classnames';
import { faEllipsisVertical } from '@fortawesome/free-solid-svg-icons/faEllipsisVertical';
import { useTranslation } from '../../hooks/use-translation';
import { FontAwesomeIcon } from '../../font-awesome-icon';
import { DropdownMenu } from '../../dropdown-menu';
import { getHeaderColumnsActions } from '../utils/getHeaderColumnsActions';
import { ACTIONS, SORTING, PINNED_WIDTH_THRESHOLD } from '../types';
import { jsx as _jsx } from "react/jsx-runtime";
export function TableHeaderCellMenu({
  header,
  tableContainerWidth,
  isVisible = false,
  localizationText
}) {
  const {
    t
  } = 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 > PINNED_WIDTH_THRESHOLD;
  const sorted = header.column.getIsSorted();
  const menuActions = getHeaderColumnsActions({
    isPinned,
    canSort,
    canPin,
    isPinDisabled,
    limitSorting,
    customActions: header.column?.columnDef?.meta?.additionalColumnActions ?? [],
    localizationText: localizationTextWithDefault
  });
  if (!menuActions.length) {
    return null;
  }
  const actionsHandlers = {
    [SORTING.DESC]: () => header.column.toggleSorting(true),
    [SORTING.ASC]: () => header.column.toggleSorting(false),
    [ACTIONS.PIN]: () => header.column.pin('left'),
    [ACTIONS.UNPIN]: () => header.column.pin(false),
    ...(header.column.columnDef?.meta?.additionalColumnActions ?? []).reduce((acc, customAction) => {
      acc[customAction.key] = customAction.onOptionSelect;
      return acc;
    }, {})
  };
  return /*#__PURE__*/_jsx("div", {
    className: "menu-wrapper",
    children: /*#__PURE__*/_jsx(DropdownMenu, {
      options: menuActions,
      triggerClassName: classnames('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__*/_jsx(FontAwesomeIcon, {
        icon: faEllipsisVertical
      }),
      triggerIsIcon: true,
      triggerId: `header-menu-actions-${header.column.id}`,
      triggerTestId: "header-menu-actions",
      triggerAriaLabelText: localizationTextWithDefault?.headerActionsAriaLabel,
      contentTestId: "header-menu-actions-content"
    })
  });
}