'use strict';

var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
require('core-js/modules/es.array.concat.js');
require('core-js/modules/es.array.filter.js');
require('core-js/modules/es.array.map.js');
require('core-js/modules/es.object.to-string.js');
var React = require('react');
var react = require('@carbon/react');
var tableUtilities = require('../components/Table/tableUtilities.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var _slicedToArray__default = /*#__PURE__*/_interopDefault(_slicedToArray);
var React__default = /*#__PURE__*/_interopDefault(React);

/**
 * A helper hook to render overflow menu items from an array of pre-defined actions,
 * or to render them dynamically from a callback on open. This can be used in the TableToolbar or
 * eventually added to rowActions to render options dynamically there.
 * @param {object} An object containing the follow properties
 *     className To applied to the OverflowMenuItem
 *     testId The testId prepended to the OverflowMenuItem
 *     isDisabled The isDisabled boolean for the whole toolbar
 *     onClick The callback to fire when the action is clicked
 *     actions The array of objects used to supply props to the OverflowMenuItem
 * @returns Array An array containing the isOpen state, setIsOpen function and renderOverflowMenuItems function
 */
var useDynamicOverflowMenuItems = function useDynamicOverflowMenuItems(_ref) {
  var className = _ref.className,
    actions = _ref.actions,
    testId = _ref.testId,
    isDisabled = _ref.isDisabled,
    _onClick = _ref.onClick;
  var _useState = React.useState(false),
    _useState2 = _slicedToArray__default.default(_useState, 2),
    isOpen = _useState2[0],
    setIsOpen = _useState2[1];

  /**
   * allow dynamically rendering the extra items from a callback when the overflow menu is opened.
   */
  var renderOverflowMenuItems = React.useCallback(function () {
    var actionsArray = typeof actions === 'function' ? actions() : actions;
    if (!(actionsArray !== null && actionsArray !== void 0 && actionsArray.length)) {
      return null;
    }
    return actionsArray.filter(function (_ref2) {
      var hidden = _ref2.hidden,
        isOverflow = _ref2.isOverflow;
      return hidden !== true && isOverflow === true;
    }).map(function (action) {
      return /*#__PURE__*/React__default.default.createElement(react.OverflowMenuItem, {
        "data-testid": "".concat(testId, "-toolbar-overflow-menu-item-").concat(action.id),
        itemText: tableUtilities.renderTableOverflowItemText({
          action: action,
          className: className
        }),
        key: "table-aggregations-overflow-item-".concat(action.id),
        onClick: function onClick() {
          setIsOpen(false);
          _onClick(action);
        },
        requireTitle: !action.renderIcon,
        disabled: isDisabled || action.disabled,
        hasDivider: action.hasDivider,
        isDelete: action.isDelete,
        "aria-label": action.labelText
      });
    });
  }, [actions, className, isDisabled, _onClick, testId]);
  return [isOpen, setIsOpen, renderOverflowMenuItems];
};
var useDynamicOverflowMenuItems$1 = useDynamicOverflowMenuItems;

module.exports = useDynamicOverflowMenuItems$1;
