Liferay.Loader.define("commerce-frontend-js@3.0.43/components/data_renderers/ActionsDropdownRenderer", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/button', '@frontend-taglib-clay$clayui/drop-down', '@frontend-taglib-clay$clayui/icon', '@frontend-taglib-clay$clayui/link', '@frontend-taglib-clay$clayui/loading-indicator', 'frontend-js-react-web$prop-types', 'frontend-js-react-web$react', '../../utilities/actionItems/constants', '../../utilities/index', '../../utilities/modals/index', '../dataset_display/DatasetDisplayContext'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    function _typeof(obj) {
      "@babel/helpers - typeof";
      if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
        _typeof = function _typeof(obj) {
          return typeof obj;
        };
      } else {
        _typeof = function _typeof(obj) {
          return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
        };
      }return _typeof(obj);
    }

    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.default = void 0;

    var _button = _interopRequireDefault(require("@frontend-taglib-clay$clayui/button"));

    var _dropDown = _interopRequireDefault(require("@frontend-taglib-clay$clayui/drop-down"));

    var _icon = _interopRequireDefault(require("@frontend-taglib-clay$clayui/icon"));

    var _link = _interopRequireDefault(require("@frontend-taglib-clay$clayui/link"));

    var _loadingIndicator = _interopRequireDefault(require("@frontend-taglib-clay$clayui/loading-indicator"));

    var _propTypes = _interopRequireDefault(require("frontend-js-react-web$prop-types"));

    var _react = _interopRequireWildcard(require("frontend-js-react-web$react"));

    var _constants = require("../../utilities/actionItems/constants");

    var _index = require("../../utilities/index");

    var _index2 = require("../../utilities/modals/index");

    var _DatasetDisplayContext = _interopRequireDefault(require("../dataset_display/DatasetDisplayContext"));

    function _getRequireWildcardCache() {
      if (typeof WeakMap !== "function") return null;var cache = new WeakMap();_getRequireWildcardCache = function _getRequireWildcardCache() {
        return cache;
      };return cache;
    }

    function _interopRequireWildcard(obj) {
      if (obj && obj.__esModule) {
        return obj;
      }if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") {
        return { default: obj };
      }var cache = _getRequireWildcardCache();if (cache && cache.has(obj)) {
        return cache.get(obj);
      }var newObj = {};var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;for (var key in obj) {
        if (Object.prototype.hasOwnProperty.call(obj, key)) {
          var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;if (desc && (desc.get || desc.set)) {
            Object.defineProperty(newObj, key, desc);
          } else {
            newObj[key] = obj[key];
          }
        }
      }newObj.default = obj;if (cache) {
        cache.set(obj, newObj);
      }return newObj;
    }

    function _interopRequireDefault(obj) {
      return obj && obj.__esModule ? obj : { default: obj };
    }

    function _extends() {
      _extends = Object.assign || function (target) {
        for (var i = 1; i < arguments.length; i++) {
          var source = arguments[i];for (var key in source) {
            if (Object.prototype.hasOwnProperty.call(source, key)) {
              target[key] = source[key];
            }
          }
        }return target;
      };return _extends.apply(this, arguments);
    }

    function ownKeys(object, enumerableOnly) {
      var keys = Object.keys(object);if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);if (enumerableOnly) symbols = symbols.filter(function (sym) {
          return Object.getOwnPropertyDescriptor(object, sym).enumerable;
        });keys.push.apply(keys, symbols);
      }return keys;
    }

    function _objectSpread(target) {
      for (var i = 1; i < arguments.length; i++) {
        var source = arguments[i] != null ? arguments[i] : {};if (i % 2) {
          ownKeys(Object(source), true).forEach(function (key) {
            _defineProperty(target, key, source[key]);
          });
        } else if (Object.getOwnPropertyDescriptors) {
          Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
        } else {
          ownKeys(Object(source)).forEach(function (key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
          });
        }
      }return target;
    }

    function _defineProperty(obj, key, value) {
      if (key in obj) {
        Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });
      } else {
        obj[key] = value;
      }return obj;
    }

    function _toConsumableArray(arr) {
      return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread();
    }

    function _nonIterableSpread() {
      throw new TypeError("Invalid attempt to spread non-iterable instance");
    }

    function _iterableToArray(iter) {
      if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
    }

    function _arrayWithoutHoles(arr) {
      if (Array.isArray(arr)) {
        for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) {
          arr2[i] = arr[i];
        }return arr2;
      }
    }

    function _slicedToArray(arr, i) {
      return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
    }

    function _nonIterableRest() {
      throw new TypeError("Invalid attempt to destructure non-iterable instance");
    }

    function _iterableToArrayLimit(arr, i) {
      if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) {
        return;
      }var _arr = [];var _n = true;var _d = false;var _e = undefined;try {
        for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
          _arr.push(_s.value);if (i && _arr.length === i) break;
        }
      } catch (err) {
        _d = true;_e = err;
      } finally {
        try {
          if (!_n && _i["return"] != null) _i["return"]();
        } finally {
          if (_d) throw _e;
        }
      }return _arr;
    }

    function _arrayWithHoles(arr) {
      if (Array.isArray(arr)) return arr;
    }

    var MODAL_PERMISSIONS = _constants.ACTION_ITEM_TARGETS.MODAL_PERMISSIONS;

    function isNotALink(target, onClick) {
      return Boolean(target && target !== 'link' || onClick);
    }

    function ActionItem(props) {

      return (/*#__PURE__*/_react.default.createElement(_dropDown.default.Item, {
          "data-senna-off": true,
          href: props.href || '#',
          onClick: isNotALink(props.target, props.onClick) ? function handleClickOnLink(e) {
            e.preventDefault();
            props.handleAction({
              method: props.method,
              onClick: props.onClick,
              size: props.size || 'lg',
              target: props.target,
              title: props.title,
              url: props.href
            });
            props.closeMenu();
          } : null
        }, props.icon && /*#__PURE__*/_react.default.createElement("span", {
          className: "pr-2"
        }, /*#__PURE__*/_react.default.createElement(_icon.default, {
          symbol: props.icon
        })), props.label)
      );
    }

    function ActionsDropdownRenderer(props) {
      var _useContext = (0, _react.useContext)(_DatasetDisplayContext.default),
          actionLoading = _useContext.actionLoading,
          executeAsyncItemAction = _useContext.executeAsyncItemAction,
          highlightItems = _useContext.highlightItems,
          openModal = _useContext.openModal,
          openSidePanel = _useContext.openSidePanel;

      var _useState = (0, _react.useState)(false),
          _useState2 = _slicedToArray(_useState, 2),
          active = _useState2[0],
          setActive = _useState2[1];

      function handleAction(_ref) {
        var _ref$method = _ref.method,
            method = _ref$method === void 0 ? '' : _ref$method,
            _ref$onClick = _ref.onClick,
            onClick = _ref$onClick === void 0 ? '' : _ref$onClick,
            _ref$size = _ref.size,
            size = _ref$size === void 0 ? '' : _ref$size,
            _ref$target = _ref.target,
            target = _ref$target === void 0 ? '' : _ref$target,
            _ref$title = _ref.title,
            title = _ref$title === void 0 ? '' : _ref$title,
            _ref$url = _ref.url,
            url = _ref$url === void 0 ? '' : _ref$url;

        if (!!target && target.includes('modal')) {
          switch (target) {
            case MODAL_PERMISSIONS:
              (0, _index2.openPermissionsModal)(url);
              break;

            default:
              openModal({
                size: (0, _index2.resolveModalSize)(target),
                title: title,
                url: url
              });
              break;
          }
        }

        if (target === 'sidePanel') {
          highlightItems([props.itemId]);
          openSidePanel({
            size: size || 'lg',
            title: title,
            url: url
          });
        }

        if (target === 'async' || target === 'headless') {
          executeAsyncItemAction(url, method);
        }

        if (onClick) {
          eval(onClick);
        }
      }

      var formattedActions = props.actions ? props.actions.reduce(function (actions, action) {
        if (action.permissionKey) {
          if (props.itemData.actions[action.permissionKey]) {
            if (action.target === 'headless') {
              return [].concat(_toConsumableArray(actions), [_objectSpread(_objectSpread({}, action), props.itemData.actions[action.permissionKey])]);
            } else {
              return [].concat(_toConsumableArray(actions), [action]);
            }
          }

          return actions;
        }

        return [].concat(_toConsumableArray(actions), [action]);
      }, []) : [];

      if (!formattedActions || !formattedActions.length) {
        return null;
      }

      if (formattedActions.length === 1) {
        var action = formattedActions[0];

        if (action.id && !action.href) {
          return null;
        }

        var formattedHref = (0, _index.formatActionUrl)(action.href, props.itemData);

        if (actionLoading) {
          return (/*#__PURE__*/_react.default.createElement(_button.default, {
              className: "btn-sm",
              disabled: true,
              displayType: "secondary",
              monospaced: true
            }, /*#__PURE__*/_react.default.createElement(_loadingIndicator.default, {
              small: true
            }))
          );
        }

        var content = action.icon ? /*#__PURE__*/_react.default.createElement(_icon.default, {
          symbol: action.icon
        }) : action.label;
        return isNotALink(action.target, action.onClick) ? /*#__PURE__*/_react.default.createElement(_button.default, {
          displayType: "secondary",
          monospaced: Boolean(action.icon),
          onClick: function onClick(e) {
            e.preventDefault();
            return handleAction({
              method: action.method,
              onClick: action.onClick,
              size: action.size,
              target: action.target,
              title: action.title,
              url: formattedHref
            });
          },
          small: true
        }, content) : /*#__PURE__*/_react.default.createElement(_link.default, {
          className: "btn btn-secondary btn-sm",
          href: formattedHref,
          monospaced: Boolean(action.icon)
        }, content);
      }

      if (actionLoading) {
        return (/*#__PURE__*/_react.default.createElement(_button.default, {
            disabled: true,
            displayType: "secondary",
            monospaced: true,
            small: true
          }, /*#__PURE__*/_react.default.createElement(_loadingIndicator.default, {
            small: true
          }))
        );
      }

      return (/*#__PURE__*/_react.default.createElement(_dropDown.default, {
          active: active,
          onActiveChange: setActive,
          trigger: /*#__PURE__*/_react.default.createElement(_button.default, {
            className: "component-action dropdown-toggle",
            displayType: "unstyled"
          }, /*#__PURE__*/_react.default.createElement(_icon.default, {
            symbol: "ellipsis-v"
          }))
        }, /*#__PURE__*/_react.default.createElement(_dropDown.default.ItemList, null, /*#__PURE__*/_react.default.createElement(_dropDown.default.Group, null, formattedActions.map(function (action, i) {
          return (/*#__PURE__*/_react.default.createElement(ActionItem, _extends({
              key: i
            }, action, {
              closeMenu: function closeMenu() {
                return setActive(false);
              },
              handleAction: handleAction,
              href: action.href && (0, _index.formatActionUrl)(action.href, props.itemData)
            }))
          );
        }))))
      );
    }

    var _default = ActionsDropdownRenderer;
    exports.default = _default;
    //# sourceMappingURL=ActionsDropdownRenderer.js.map
  }
});
//# sourceMappingURL=ActionsDropdownRenderer.js.map