"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.evaluateActionCell = evaluateActionCell;
exports.useCardActions = useCardActions;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireWildcard(require("react"));
var _useExtensionMenuItems = require("../../hooks/useExtensionMenuItems");
var _designSystem = require("@wix/design-system");
var _getDefaultActionCellProps = require("../CollectionItemActions/getDefaultActionCellProps");
var _wixUiIconsCommon = require("@wix/wix-ui-icons-common");
var _DisabledActionCellMenu = require("../ActionCell/DisabledActionCellMenu");
var _KanbanCardActionsHelper = require("./KanbanCardActionsHelper");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/components/Kanban/useCardActions.tsx";
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function evaluateActionCell({
  actionCell,
  originalItem,
  itemIndex,
  itemActionsState
}) {
  if (!actionCell) {
    return {};
  }
  if (typeof actionCell === 'function') {
    return actionCell(originalItem, itemIndex, itemActionsState.actionCellAPI);
  }
  return actionCell;
}
function useCardActions({
  actionCell,
  originalItem,
  itemIndex,
  itemActionsState,
  stageKey,
  cardKey,
  state
}) {
  const actionCellConfig = evaluateActionCell({
    actionCell,
    originalItem,
    itemIndex,
    itemActionsState
  });
  const extensionMenuItems = (0, _useExtensionMenuItems.useExtensionMenuItems)({
    containerId: actionCellConfig.containerId
  });
  const secondaryActionCellPropsWithBI = (0, _KanbanCardActionsHelper.createKanbanSecondaryActionCellPropsWithBI)({
    state,
    actionCellProps: actionCellConfig,
    keyedItem: itemActionsState.keyedItem,
    extensionMenuItems,
    stageId: stageKey
  });
  const primaryActionCellPropsWithBI = (0, _KanbanCardActionsHelper.createKanbanPrimaryActionCellPropsWithBI)({
    state,
    actionCellProps: actionCellConfig,
    keyedItem: itemActionsState.keyedItem,
    stageId: stageKey
  });
  const popoverCloseRef = (0, _react.useRef)(null);
  (0, _react.useEffect)(() => {
    const closePopoverOnScroll = () => {
      if (popoverCloseRef.current) {
        const scrollEvent = new Event('scroll');
        popoverCloseRef.current(scrollEvent);
      }
    };
    const stageCardsContainer = document.querySelector(`[data-hook="kanban-stage-cards-container-${stageKey}"]`);
    if (stageCardsContainer && typeof stageCardsContainer.addEventListener === 'function') {
      stageCardsContainer.addEventListener('scroll', closePopoverOnScroll, {
        passive: true
      });
      return () => {
        if (typeof stageCardsContainer.removeEventListener === 'function') {
          stageCardsContainer.removeEventListener('scroll', closePopoverOnScroll);
        }
      };
    }
    return () => {};
  }, [stageKey]);
  let secondaryActions = null;
  if (actionCellConfig.secondaryMenuDisabled) {
    secondaryActions = /*#__PURE__*/_react.default.createElement(_DisabledActionCellMenu.DisabledActionCellMenu, {
      dataHook: `kanban-card-more-actions-trigger-disabled-${stageKey}-${cardKey}`,
      tooltipContent: actionCellConfig.secondaryMenuDisabledTooltip ?? state.container.translate('cairo.disabled'),
      size: "tiny",
      icon: /*#__PURE__*/_react.default.createElement(_wixUiIconsCommon.MoreSmall, {
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 137,
          columnNumber: 15
        }
      }),
      ariaLabel: state.container.translate('cairo.moreActions'),
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 130,
        columnNumber: 7
      }
    });
  } else if (secondaryActionCellPropsWithBI && secondaryActionCellPropsWithBI.length > 0) {
    secondaryActions = /*#__PURE__*/_react.default.createElement(_designSystem.PopoverMenu, (0, _extends2.default)({}, actionCellConfig.popoverMenuProps, (0, _getDefaultActionCellProps.getDefaultActionCellProps)(), {
      textSize: "small",
      minWidth: "223px",
      dataHook: `kanban-card-more-actions-${stageKey}-${cardKey}`,
      triggerElement: ({
        toggle,
        close
      }) => {
        popoverCloseRef.current = close;
        return /*#__PURE__*/_react.default.createElement(_designSystem.IconButton, {
          dataHook: `kanban-card-more-actions-trigger-${stageKey}-${cardKey}`,
          onClick: e => {
            e.stopPropagation();
            toggle();
            (0, _KanbanCardActionsHelper.reportOnKanbanPopoverMenuShow)({
              state,
              keyedItem: itemActionsState.keyedItem,
              stageId: stageKey
            });
          },
          priority: "secondary",
          size: "tiny",
          __self: this,
          __source: {
            fileName: _jsxFileName,
            lineNumber: 156,
            columnNumber: 13
          }
        }, /*#__PURE__*/_react.default.createElement(_wixUiIconsCommon.MoreSmall, {
          __self: this,
          __source: {
            fileName: _jsxFileName,
            lineNumber: 170,
            columnNumber: 15
          }
        }));
      },
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 146,
        columnNumber: 7
      }
    }), secondaryActionCellPropsWithBI.map((action, index) => {
      if (action.divider) {
        return /*#__PURE__*/_react.default.createElement(_designSystem.PopoverMenu.Divider, {
          key: index,
          __self: this,
          __source: {
            fileName: _jsxFileName,
            lineNumber: 177,
            columnNumber: 20
          }
        });
      }
      const {
        icon: prefixIcon,
        skin,
        ...rest
      } = action;
      return /*#__PURE__*/_react.default.createElement(_designSystem.PopoverMenu.MenuItem, (0, _extends2.default)({
        key: index,
        subtitle: action.disabledDescription,
        prefixIcon: prefixIcon,
        dataHook: action.dataHook || `kanban-card-more-actions-item-${stageKey}-${cardKey}-${index}`,
        skin: skin
      }, rest, {
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 181,
          columnNumber: 13
        }
      }));
    }));
  }
  return {
    secondaryActions,
    primaryActionProps: primaryActionCellPropsWithBI
  };
}
//# sourceMappingURL=useCardActions.js.map