"use strict";

exports.__esModule = true;
exports.reportOnPopoverMenuShow = exports.createSecondaryActionCellPropWithBi = exports.createPrimaryActionCellPropsWithBI = void 0;
var _mobx = require("mobx");
var _react = require("react");
const calcCtaCount = actionCellProps => {
  const {
    primaryAction,
    secondaryActions
  } = actionCellProps;
  const primaryActionLength = Array.isArray(primaryAction) ? primaryAction.length : primaryAction ? 1 : 0;
  return ((secondaryActions == null ? void 0 : secondaryActions.length) || 0) + primaryActionLength;
};
const createSecondaryActionCellPropWithBi = ({
  toolbarBIReporter,
  actionCellProps,
  keyedItem,
  extensionMenuItems
}) => {
  const {
    primaryAction,
    secondaryActions = []
  } = actionCellProps;
  const primaryActionLength = Array.isArray(primaryAction) ? primaryAction.length : primaryAction ? 1 : 0;
  const moreSecondaryItems = extensionMenuItems.menuItems.map(menuItem => {
    return {
      biName: menuItem.biName,
      dataHook: menuItem.dataHook,
      onClick: () => {
        menuItem.onClick({
          selectedId: keyedItem.id
        });
      },
      text: menuItem.text,
      icon: menuItem.prefixIcon || /*#__PURE__*/(0, _react.createElement)('div'),
      componentId: menuItem.componentId,
      appId: menuItem.appId
    };
  }) || [];
  const allActions = [...secondaryActions, ...moreSecondaryItems];
  return allActions.map((_action, index) => {
    const biIndex = index + 1;
    if (_action.divider) {
      return _action;
    }
    const onClick = (0, _mobx.action)(() => {
      toolbarBIReporter.ctaClicked({
        location: 'actionCell',
        ctaIndex: primaryActionLength + biIndex,
        ctaName: (_action == null ? void 0 : _action.biName) || (_action == null ? void 0 : _action.text) || 'secondaryAction',
        moreActionsIndex: biIndex,
        numCtas: calcCtaCount(actionCellProps),
        itemIndex: keyedItem.index,
        itemId: keyedItem.id,
        componentId: _action.componentId,
        appId: _action.appId
      });
      _action.onClick == null || _action.onClick();
    });
    const {
      biName: _biName,
      ...wsrSecondaryAction
    } = _action;
    return {
      ...wsrSecondaryAction,
      onClick
    };
  }) || [];
};
exports.createSecondaryActionCellPropWithBi = createSecondaryActionCellPropWithBi;
const createPrimaryActionCellPropsWithBI = (toolbarBIReporter, actionCellProps, keyedItem) => {
  const {
    primaryAction
  } = actionCellProps;
  if (primaryAction) {
    const addBiReport = _primaryAction => {
      if (_primaryAction) {
        const onClick = (0, _mobx.action)(() => {
          toolbarBIReporter.ctaClicked({
            location: 'actionCell',
            ctaIndex: 1,
            ctaName: (_primaryAction == null ? void 0 : _primaryAction.biName) || (_primaryAction == null ? void 0 : _primaryAction.text) || 'primaryAction',
            moreActionsIndex: 0,
            numCtas: calcCtaCount(actionCellProps),
            itemIndex: keyedItem.index,
            itemId: keyedItem.id
          });
          _primaryAction == null || _primaryAction.onClick();
        });
        const {
          biName,
          ...wsrPrimaryAction
        } = _primaryAction;
        return {
          ...wsrPrimaryAction,
          onClick
        };
      }
      return _primaryAction;
    };
    if (Array.isArray(primaryAction)) {
      return primaryAction.map(addBiReport);
    } else {
      return addBiReport(primaryAction);
    }
  }
  return primaryAction;
};
exports.createPrimaryActionCellPropsWithBI = createPrimaryActionCellPropsWithBI;
const reportOnPopoverMenuShow = (toolbarBIReporter, actionCellProps, keyedItem) => {
  toolbarBIReporter.ctaClicked({
    location: 'actionCell',
    ctaIndex: 0,
    ctaName: 'More Actions Menu',
    numCtas: calcCtaCount(actionCellProps),
    itemId: keyedItem.id,
    itemIndex: keyedItem.index
  });
};
exports.reportOnPopoverMenuShow = reportOnPopoverMenuShow;
//# sourceMappingURL=CollectionItemActionsHelper.js.map