"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.MoreActionsBase = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireWildcard(require("react"));
var _designSystem = require("@wix/design-system");
var _bi = require("@wix/bex-core/bi");
var _react2 = require("@wix/bex-core/react");
var _mobxReactLite = require("mobx-react-lite");
var _useExtensionMenuItems = require("../../hooks/useExtensionMenuItems");
var _usePageButtonBiParams = require("../usePageButtonBiParams");
var _useExploreAppsState = require("./useExploreAppsState");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/components/MoreActions/MoreActionsBase.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 _MoreActionsBase(params) {
  const {
    items = [],
    containerId,
    containerProps,
    dataHook = 'more-actions',
    triggerElement,
    exploreAppsModalProps,
    biAdditionalInfo
  } = params;
  const {
    pageType,
    location
  } = (0, _usePageButtonBiParams.usePageButtonBiParams)();
  const state = (0, _useExtensionMenuItems.useExtensionMenuItems)({
    containerId,
    containerProps
  });
  const container = (0, _react2.useWixPatternsContainer)();
  const pageState = (0, _react2.useCollectionPageContext)();
  const {
    reportBi,
    environment
  } = container;
  (0, _react.useEffect)(() => {
    const notifyLoad = async () => {
      const {
        loadStart,
        loadEnd
      } = await Promise.resolve().then(() => _interopRequireWildcard(require('@wix/bex-core/bi')));
      reportBi(loadStart({
        componentType: 'More actions',
        componentName: environment.componentName,
        additionalFeaturesAvailability: `IsUsingExploreApps: ${!!exploreAppsModalProps}`
      }));
      reportBi(
      // We dont care about loading time, its just needed for usage purposes
      loadEnd({
        componentType: 'More actions',
        url: window.location.href,
        route: window.location.pathname,
        componentName: environment.componentName
      }));
    };
    notifyLoad();
  }, []);
  const groupedItems = Array.isArray(items == null ? void 0 : items[0]) ? items : [items];
  const exploreAppsState = (0, _useExploreAppsState.useExploreAppsState)(exploreAppsModalProps);
  const allActions = (0, _react.useMemo)(() => {
    const groups = [...groupedItems, state.menuItems];
    if (exploreAppsState) {
      groups.push([exploreAppsState.moreActionsMenuItem]);
    }
    const numCtas = groups.reduce((result, group) => result + group.length, 0);
    const flatActions = groups.flat();
    return groups.filter(actions => actions.length !== 0).reduce((result, group, index, array) => {
      return [...result, ...group.map(item => {
        const {
          prefixIcon,
          text,
          onClick,
          biName,
          ...rest
        } = typeof item === 'function' ? item({
          pageState,
          container
        }) : item;
        const isTPA = 'type' in item && item.type === 'TPA';
        const itemAdditionalInfo = 'biAdditionalInfo' in item ? item.biAdditionalInfo : undefined;
        return /*#__PURE__*/_react.default.createElement(_designSystem.PopoverMenu.MenuItem, (0, _extends2.default)({
          key: index,
          prefixIcon: prefixIcon,
          text: text,
          onClick: () => {
            reportBi((0, _bi.cairoPageCtaClicked)({
              ctaName: biName,
              ctaType: 'More Actions Option',
              location,
              moreActionsIndex: flatActions.indexOf(item),
              numCtas,
              section: isTPA ? 'TPA' : 'Vertical',
              componentType: 'Page',
              pageType,
              additionalInfo: itemAdditionalInfo
            }));
            if (isTPA) {
              reportBi((0, _bi.cairoTpaSCtaClicked)({
                appId: item.appId,
                componentType: 'Page',
                numCtas,
                ctaIndex: flatActions.indexOf(item),
                ctaName: biName,
                location,
                tpaComponentId: item.componentId
              }));
            }
            onClick(isTPA ? state.containerProps : undefined);
          }
        }, rest, {
          __self: this,
          __source: {
            fileName: _jsxFileName,
            lineNumber: 99,
            columnNumber: 15
          }
        }));
      }), ...(index !== array.length - 1 ? [/*#__PURE__*/_react.default.createElement(_designSystem.PopoverMenu.Divider, {
        key: index,
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 139,
          columnNumber: 16
        }
      })] : [])];
    }, []);
  }, [items, state.menuItems, exploreAppsState]);
  return allActions.length === 0 ? null : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_designSystem.PopoverMenu, {
    dataHook: dataHook,
    onShow: () => {
      reportBi((0, _bi.cairoPageCtaClicked)({
        ctaName: 'More Actions',
        ctaType: 'secondary page dropdown',
        location,
        componentType: 'Page',
        pageType,
        additionalInfo: biAdditionalInfo
      }));
    },
    minWidth: 240,
    textSize: "medium",
    triggerElement: triggerElement,
    appendTo: "parent",
    zIndex: 1101,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 147,
      columnNumber: 7
    }
  }, allActions), exploreAppsState && /*#__PURE__*/_react.default.createElement(exploreAppsState.Modal, {
    state: exploreAppsState,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 169,
      columnNumber: 28
    }
  }));
}
const MoreActionsBase = exports.MoreActionsBase = (0, _mobxReactLite.observer)(_MoreActionsBase);
//# sourceMappingURL=MoreActionsBase.js.map