"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.ActionPopover = ActionPopover;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _designSystem = require("@wix/design-system");
var _react = _interopRequireWildcard(require("react"));
var _wixUiIconsCommon = require("@wix/wix-ui-icons-common");
var _DisabledTooltip = require("../DisabledTooltip");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/components/ActionControl/ActionPopover.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 isActionSubitemLabel(item) {
  return typeof item === 'object' && item !== null && 'sectionTitle' in item;
}
function ActionPopover(props) {
  const {
    actionItem,
    size,
    priority,
    onPopoverClick,
    fullWidth
  } = props;
  const {
    dataHook,
    subItems,
    popoverMenuProps
  } = actionItem;
  const createCombinedRef = (0, _react.useCallback)(wdsRef => {
    return wrapper => {
      const buttonEl = (wrapper == null ? void 0 : wrapper.querySelector('button')) ?? null;
      if (wdsRef) {
        wdsRef.current = buttonEl;
      }
      const externalRef = actionItem.ref;
      if (externalRef) {
        if (typeof externalRef === 'function') {
          externalRef(buttonEl);
        } else {
          externalRef.current = buttonEl;
        }
      }
    };
  }, [actionItem.ref]);
  const allActions = (0, _react.useMemo)(() => {
    return subItems == null ? void 0 : subItems.filter(subItemGroup => subItemGroup == null ? void 0 : subItemGroup.length).reduce((result, group, index, array) => {
      const nodes = [];
      group.forEach((item, itemIndex) => {
        if (isActionSubitemLabel(item)) {
          nodes.push(/*#__PURE__*/_react.default.createElement(_designSystem.PopoverMenu.SectionTitle, {
            key: `section-${index}-${itemIndex}`,
            title: item.sectionTitle,
            dataHook: item.dataHook,
            __self: this,
            __source: {
              fileName: _jsxFileName,
              lineNumber: 64,
              columnNumber: 15
            }
          }));
        } else {
          nodes.push(/*#__PURE__*/_react.default.createElement(_designSystem.PopoverMenu.MenuItem, {
            prefixIcon: item.prefixIcon,
            disabled: item.disabled,
            text: item.label,
            key: item.label,
            subtitle: item.subtitle,
            dataHook: item.dataHook,
            onClick: item.onClick,
            __self: this,
            __source: {
              fileName: _jsxFileName,
              lineNumber: 72,
              columnNumber: 15
            }
          }));
        }
      });
      if (index !== array.length - 1) {
        nodes.push(/*#__PURE__*/_react.default.createElement(_designSystem.PopoverMenu.Divider, {
          key: `divider-${index}`,
          __self: this,
          __source: {
            fileName: _jsxFileName,
            lineNumber: 86,
            columnNumber: 22
          }
        }));
      }
      return [...result, ...nodes];
    }, []);
  }, [subItems]);
  return /*#__PURE__*/_react.default.createElement(_designSystem.PopoverMenu, (0, _extends2.default)({}, popoverMenuProps, {
    dataHook: dataHook ? `${dataHook}-popover` : undefined,
    triggerElement: ({
      toggle,
      ref: wdsRef
    }) =>
    /*#__PURE__*/
    // Wrapper div needed due to type mismatch: PopoverMenu expects RefObject<HTMLElement>
    // but Button's ref returns a class instance, not an HTMLElement.
    _react.default.createElement("div", {
      ref: createCombinedRef(wdsRef),
      style: {
        display: 'contents'
      },
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 100,
        columnNumber: 9
      }
    }, /*#__PURE__*/_react.default.createElement(_DisabledTooltip.DisabledTooltip, {
      content: actionItem.tooltip,
      dataHook: dataHook ? `${dataHook}-tooltip` : undefined,
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 101,
        columnNumber: 11
      }
    }, /*#__PURE__*/_react.default.createElement(_designSystem.Button, {
      skin: "standard",
      priority: priority,
      size: size,
      fullWidth: fullWidth,
      dataHook: actionItem.dataHook,
      onClick: () => {
        onPopoverClick == null || onPopoverClick();
        toggle();
      },
      ellipsis: true,
      prefixIcon: actionItem.prefixIcon,
      disabled: actionItem.disabled,
      suffixIcon: /*#__PURE__*/_react.default.createElement(_wixUiIconsCommon.ChevronDown, {
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 118,
          columnNumber: 27
        }
      }),
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 105,
        columnNumber: 13
      }
    }, actionItem.label))),
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 94,
      columnNumber: 5
    }
  }), allActions);
}
//# sourceMappingURL=ActionPopover.js.map