"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.evaluateActionCell = evaluateActionCell;
exports.useGridItemActions = useGridItemActions;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireDefault(require("react"));
var _designSystem = require("@wix/design-system");
var _wixUiIconsCommon = require("@wix/wix-ui-icons-common");
var _CollectionItemActionsHelper = require("../CollectionItemActions/CollectionItemActionsHelper");
var _getDefaultActionCellProps = require("../CollectionItemActions/getDefaultActionCellProps");
var _useExtensionMenuItems = require("../../hooks/useExtensionMenuItems");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/components/Grid/useGridItemActions.tsx";
function evaluateActionCell({
  actionCell,
  item,
  index,
  gridState,
  keyedItem,
  itemActionsState
}) {
  if (!actionCell) {
    return {};
  }
  if (typeof actionCell === 'function') {
    return actionCell(item, index, {
      openConfirmDeleteModal: itemActionsState.openConfirmDeleteModal,
      openConfirmModal: itemActionsState.openConfirmModal,
      collection: gridState.collection,
      keyedItem
    });
  }
  return actionCell;
}
function useGridItemActions(actionCell) {
  const actions = evaluateActionCell(actionCell);
  let settingsMenu = null;
  let primaryAction = null;
  const extensionMenuItems = (0, _useExtensionMenuItems.useExtensionMenuItems)({
    containerId: actions.containerId
  });
  const secondaryActionCellPropsWithBI = (0, _CollectionItemActionsHelper.createSecondaryActionCellPropWithBi)({
    toolbarBIReporter: actionCell.gridState.toolbar.toolbarBIReporter,
    actionCellProps: actions,
    keyedItem: actionCell.keyedItem,
    extensionMenuItems
  });
  const primaryActionCellPropsWithBI = (0, _CollectionItemActionsHelper.createPrimaryActionCellPropsWithBI)(actionCell.gridState.toolbar.toolbarBIReporter, actions, actionCell.keyedItem);
  if (primaryActionCellPropsWithBI) {
    primaryAction = {
      label: primaryActionCellPropsWithBI.text,
      onClick: primaryActionCellPropsWithBI.onClick,
      disabled: primaryActionCellPropsWithBI.disabled,
      disabledMessage: primaryActionCellPropsWithBI.disabledMessage
    };
  }
  if (secondaryActionCellPropsWithBI && secondaryActionCellPropsWithBI.length > 0) {
    settingsMenu = /*#__PURE__*/_react.default.createElement(_designSystem.PopoverMenu, (0, _extends2.default)({}, actions.popoverMenuProps, (0, _getDefaultActionCellProps.getDefaultActionCellProps)(), {
      dataHook: "popover",
      triggerElement: ({
        toggle,
        close
      }) => /*#__PURE__*/_react.default.createElement(_designSystem.IconButton, {
        dataHook: "popover-icon-button-trigger",
        onClick: e => {
          e.stopPropagation();
          toggle();
          (0, _CollectionItemActionsHelper.reportOnPopoverMenuShow)(actionCell.gridState.toolbar.toolbarBIReporter, actions, actionCell.keyedItem);
        },
        onPointerDown: actionCell.gridState._dnd ? e => e.stopPropagation() : undefined,
        onMouseLeave: close,
        skin: actionCell.imagePlacement === 'top' ? 'light' : 'standard',
        size: "small",
        priority: "secondary",
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 101,
          columnNumber: 11
        }
      }, /*#__PURE__*/_react.default.createElement(_wixUiIconsCommon.More, {
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 120,
          columnNumber: 13
        }
      })),
      children: secondaryActionCellPropsWithBI.map((action, index) => {
        if (action.divider) {
          return /*#__PURE__*/_react.default.createElement(_designSystem.PopoverMenu.Divider, {
            key: index,
            __self: this,
            __source: {
              fileName: _jsxFileName,
              lineNumber: 125,
              columnNumber: 20
            }
          });
        }
        const {
          icon: prefixIcon,
          skin,
          ...rest
        } = action;
        return /*#__PURE__*/_react.default.createElement(_designSystem.PopoverMenu.MenuItem, (0, _extends2.default)({
          key: index,
          prefixIcon: prefixIcon,
          skin: skin
        }, rest, {
          __self: this,
          __source: {
            fileName: _jsxFileName,
            lineNumber: 131,
            columnNumber: 13
          }
        }));
      }),
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 96,
        columnNumber: 7
      }
    }));
  }
  return {
    primaryAction,
    settingsMenu
  };
}
//# sourceMappingURL=useGridItemActions.js.map