Liferay.Loader.define("product-navigation-applications-menu@1.0.18/js/ApplicationsMenu", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/button', '@frontend-taglib-clay$clayui/icon', '@frontend-taglib-clay$clayui/label', '@frontend-taglib-clay$clayui/layout', '@frontend-taglib-clay$clayui/modal', '@frontend-taglib-clay$clayui/sticker', '@frontend-taglib-clay$clayui/tabs', 'frontend-js-react-web$classnames', 'frontend-js-react-web', 'frontend-js-web', 'frontend-js-react-web$prop-types', 'frontend-js-react-web$react', '../css/ApplicationsMenu.scss'], 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 = _interopRequireWildcard(require("@frontend-taglib-clay$clayui/button"));

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

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

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

    var _modal = _interopRequireWildcard(require("@frontend-taglib-clay$clayui/modal"));

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

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

    var _classnames = _interopRequireDefault(require("frontend-js-react-web$classnames"));

    var _frontendJsReactWeb = require("frontend-js-react-web");

    var _frontendJsWeb = require("frontend-js-web");

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

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

    require("../css/ApplicationsMenu.scss");

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

    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 _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 _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 getOpenMenuTooltip = function getOpenMenuTooltip(keyLabel) {
      return "<div>".concat(Liferay.Language.get('open-menu'), "</div>") + '<kbd class="c-kbd c-kbd-dark">' + "<kbd class=\"c-kbd\">".concat(keyLabel, "</kbd>") + '<span class="c-kbd-separator">+</span>' + '<kbd class="c-kbd">⇧</kbd>' + '<span class="c-kbd-separator">+</span>' + '<kbd class="c-kbd">M</kbd>' + '</kbd>';
    };

    var SitesPanel = function SitesPanel(_ref) {
      var portletNamespace = _ref.portletNamespace,
          sites = _ref.sites,
          virtualInstance = _ref.virtualInstance;
      return _react["default"].createElement("div", {
        className: "applications-menu-sites c-p-3 c-px-md-4"
      }, _react["default"].createElement("h2", {
        className: "applications-menu-sites-label c-mt-2 c-mt-md-0 text-uppercase"
      }, Liferay.Language.get('sites')), _react["default"].createElement("div", {
        className: "c-mt-2"
      }, _react["default"].createElement("ul", {
        className: "c-mb-0 list-unstyled"
      }, virtualInstance && _react["default"].createElement("li", {
        className: "applications-menu-virtual-instance c-mt-2"
      }, _react["default"].createElement("a", {
        className: "applications-menu-nav-link",
        href: virtualInstance.url
      }, _react["default"].createElement(_layout["default"].ContentRow, {
        verticalAlign: "center"
      }, _react["default"].createElement(_layout["default"].ContentCol, null, _react["default"].createElement(_sticker["default"], null, _react["default"].createElement("img", {
        alt: "",
        height: "32px",
        src: virtualInstance.logoURL
      }))), _react["default"].createElement(_layout["default"].ContentCol, {
        className: "applications-menu-shrink c-ml-2"
      }, _react["default"].createElement("span", {
        className: "text-truncate"
      }, virtualInstance.label))))))), _react["default"].createElement("div", {
        className: "applications-menu-nav-divider c-my-3"
      }), _react["default"].createElement("div", {
        className: "applications-menu-sites c-my-2"
      }, _react["default"].createElement("ul", {
        className: "list-unstyled"
      }, sites && _react["default"].createElement(Sites, {
        mySites: sites.mySites,
        portletNamespace: portletNamespace,
        recentSites: sites.recentSites,
        viewAllURL: sites.viewAllURL
      }))));
    };

    var Site = function Site(_ref2) {
      var current = _ref2.current,
          label = _ref2.label,
          logoURL = _ref2.logoURL,
          url = _ref2.url;
      return _react["default"].createElement("li", {
        className: "c-mt-3"
      }, _react["default"].createElement("a", {
        className: "applications-menu-nav-link",
        href: url
      }, _react["default"].createElement(_layout["default"].ContentRow, {
        verticalAlign: "center"
      }, _react["default"].createElement(_layout["default"].ContentCol, null, _react["default"].createElement(_sticker["default"], {
        size: "sm"
      }, logoURL ? _react["default"].createElement("img", {
        alt: "",
        height: "20px",
        src: logoURL
      }) : _react["default"].createElement(_icon["default"], {
        symbol: "sites"
      }))), _react["default"].createElement(_layout["default"].ContentCol, {
        className: "applications-menu-shrink c-ml-2"
      }, _react["default"].createElement("span", {
        className: "text-truncate"
      }, label)), current && _react["default"].createElement(_layout["default"].ContentCol, {
        className: "c-ml-2"
      }, _react["default"].createElement(_label["default"], {
        displayType: "info"
      }, Liferay.Language.get('current'))))));
    };

    var Sites = function Sites(_ref3) {
      var mySites = _ref3.mySites,
          portletNamespace = _ref3.portletNamespace,
          recentSites = _ref3.recentSites,
          viewAllURL = _ref3.viewAllURL;
      return _react["default"].createElement(_react["default"].Fragment, null, (recentSites === null || recentSites === void 0 ? void 0 : recentSites.length) > 0 && recentSites.map(function (_ref4) {
        var current = _ref4.current,
            key = _ref4.key,
            label = _ref4.label,
            logoURL = _ref4.logoURL,
            url = _ref4.url;
        return _react["default"].createElement(Site, {
          current: current,
          key: key,
          label: label,
          logoURL: logoURL,
          url: url
        });
      }), (recentSites === null || recentSites === void 0 ? void 0 : recentSites.length) > 0 && (mySites === null || mySites === void 0 ? void 0 : mySites.length) > 0 && _react["default"].createElement("li", {
        className: "applications-menu-nav-divider c-mt-3"
      }), (mySites === null || mySites === void 0 ? void 0 : mySites.length) > 0 && mySites.map(function (_ref5) {
        var current = _ref5.current,
            key = _ref5.key,
            label = _ref5.label,
            logoURL = _ref5.logoURL,
            url = _ref5.url;
        return _react["default"].createElement(Site, {
          current: current,
          key: key,
          label: label,
          logoURL: logoURL,
          url: url
        });
      }), viewAllURL && _react["default"].createElement("li", {
        className: "c-mt-3"
      }, _react["default"].createElement(_button["default"], {
        className: "applications-menu-btn btn-unstyled c-mb-0 c-mt-3",
        displayType: "link",
        onClick: function onClick() {
          (0, _frontendJsWeb.openSelectionModal)({
            id: "".concat(portletNamespace, "selectSite"),
            onSelect: function onSelect(selectedItem) {
              (0, _frontendJsWeb.navigate)(selectedItem.url);
            },
            selectEventName: "".concat(portletNamespace, "selectSite"),
            title: Liferay.Language.get('select-site'),
            url: viewAllURL
          });
        }
      }, Liferay.Language.get('view-all'))));
    };

    var AppsPanel = function AppsPanel(_ref6) {
      var _ref6$categories = _ref6.categories,
          categories = _ref6$categories === void 0 ? [] : _ref6$categories,
          _ref6$handleCloseButt = _ref6.handleCloseButtonClick,
          handleCloseButtonClick = _ref6$handleCloseButt === void 0 ? function () {} : _ref6$handleCloseButt,
          liferayLogoURL = _ref6.liferayLogoURL,
          liferayName = _ref6.liferayName,
          portletNamespace = _ref6.portletNamespace,
          selectedPortletId = _ref6.selectedPortletId,
          sites = _ref6.sites,
          virtualInstance = _ref6.virtualInstance;
      var index = categories.findIndex(function (category) {
        return category.childCategories.some(function (childCategory) {
          return childCategory.panelApps.some(function (panelApp) {
            return panelApp.portletId === selectedPortletId;
          });
        });
      });

      if (index === -1) {
        index = 0;
      }

      var _useState = (0, _react.useState)(index),
          _useState2 = _slicedToArray(_useState, 2),
          activeTab = _useState2[0],
          setActiveTab = _useState2[1];

      return _react["default"].createElement("div", {
        className: "applications-menu-wrapper"
      }, _react["default"].createElement("h1", {
        className: "sr-only"
      }, Liferay.Language.get('applications-menu')), _react["default"].createElement("div", {
        className: "applications-menu-header"
      }, _react["default"].createElement(_layout["default"].ContainerFluid, null, _react["default"].createElement(_layout["default"].Row, null, _react["default"].createElement(_layout["default"].Col, null, _react["default"].createElement(_layout["default"].ContentRow, {
        verticalAlign: "center"
      }, _react["default"].createElement(_layout["default"].ContentCol, {
        expand: true
      }, _react["default"].createElement(_tabs["default"], {
        modern: true
      }, categories.map(function (_ref7, index) {
        var key = _ref7.key,
            label = _ref7.label;
        return _react["default"].createElement(_tabs["default"].Item, {
          active: activeTab === index,
          id: "".concat(portletNamespace, "tab_").concat(index),
          key: key,
          onClick: function onClick() {
            return setActiveTab(index);
          }
        }, _react["default"].createElement("span", {
          className: "c-inner",
          tabIndex: "-1"
        }, label));
      }))), _react["default"].createElement(_layout["default"].ContentCol, null, _react["default"].createElement(_button.ClayButtonWithIcon, {
        displayType: "unstyled",
        onClick: handleCloseButtonClick,
        small: true,
        symbol: "times",
        title: Liferay.Language.get('close')
      }))))))), _react["default"].createElement("div", {
        className: "applications-menu-bg applications-menu-border-top applications-menu-content"
      }, _react["default"].createElement(_layout["default"].ContainerFluid, null, _react["default"].createElement(_layout["default"].Row, null, _react["default"].createElement(_layout["default"].Col, {
        lg: "9",
        md: "8"
      }, _react["default"].createElement(_tabs["default"].Content, {
        activeIndex: activeTab
      }, categories.map(function (_ref8, index) {
        var childCategories = _ref8.childCategories;
        return _react["default"].createElement(_tabs["default"].TabPane, {
          "aria-labelledby": "".concat(portletNamespace, "tab_").concat(index),
          key: "tabPane-".concat(index)
        }, _react["default"].createElement("div", {
          className: "applications-menu-nav-columns c-mt-md-3 c-my-2"
        }, childCategories.map(function (_ref9) {
          var key = _ref9.key,
              label = _ref9.label,
              panelApps = _ref9.panelApps;
          return _react["default"].createElement(_layout["default"].Col, {
            key: key,
            md: true
          }, _react["default"].createElement("ul", {
            className: "list-unstyled"
          }, _react["default"].createElement("li", {
            className: "c-my-3"
          }, _react["default"].createElement("h2", {
            className: "applications-menu-nav-header"
          }, label)), panelApps.map(function (_ref10) {
            var label = _ref10.label,
                portletId = _ref10.portletId,
                url = _ref10.url;
            return _react["default"].createElement("li", {
              className: "c-mt-2",
              key: portletId
            }, _react["default"].createElement("a", {
              className: (0, _classnames["default"])('component-link applications-menu-nav-link', {
                active: portletId === selectedPortletId
              }),
              href: url
            }, _react["default"].createElement("span", {
              className: "c-inner",
              tabIndex: "-1"
            }, label)));
          })));
        })));
      }))), _react["default"].createElement(_layout["default"].Col, {
        className: "c-pl-md-2 c-px-0",
        lg: "3",
        md: "4"
      }, _react["default"].createElement(SitesPanel, {
        portletNamespace: portletNamespace,
        sites: sites,
        virtualInstance: virtualInstance
      }))))), _react["default"].createElement("div", {
        className: "applications-menu-bg applications-menu-footer"
      }, _react["default"].createElement(_layout["default"].ContainerFluid, null, _react["default"].createElement(_layout["default"].Row, null, _react["default"].createElement(_layout["default"].Col, {
        lg: "9",
        md: "8"
      }, _react["default"].createElement(_layout["default"].ContentRow, {
        className: "applications-menu-border-top c-py-3",
        verticalAlign: "center"
      }, _react["default"].createElement(_layout["default"].ContentCol, {
        expand: true
      }, _react["default"].createElement(_layout["default"].ContentRow, {
        verticalAlign: "center"
      }, _react["default"].createElement(_layout["default"].ContentCol, null, _react["default"].createElement(_sticker["default"], null, _react["default"].createElement("img", {
        alt: "",
        height: "32px",
        src: liferayLogoURL
      }))), _react["default"].createElement(_layout["default"].ContentCol, {
        className: "c-ml-2"
      }, _react["default"].createElement("div", {
        className: "applications-menu-company c-mb-0"
      }, liferayName)))))), _react["default"].createElement(_layout["default"].Col, {
        className: "c-pl-md-2 c-px-0 d-md-block d-none",
        lg: "3",
        md: "4"
      }, _react["default"].createElement("div", {
        className: "applications-menu-sites"
      }))))));
    };

    var ApplicationsMenu = function ApplicationsMenu(_ref11) {
      var liferayLogoURL = _ref11.liferayLogoURL,
          liferayName = _ref11.liferayName,
          panelAppsURL = _ref11.panelAppsURL,
          selectedPortletId = _ref11.selectedPortletId,
          virtualInstance = _ref11.virtualInstance;

      var _useState3 = (0, _react.useState)({}),
          _useState4 = _slicedToArray(_useState3, 2),
          appsPanelData = _useState4[0],
          setAppsPanelData = _useState4[1];

      var _useState5 = (0, _react.useState)(false),
          _useState6 = _slicedToArray(_useState5, 2),
          visible = _useState6[0],
          setVisible = _useState6[1];

      var _useModal = (0, _modal.useModal)({
        onClose: function onClose() {
          return setVisible(false);
        }
      }),
          observer = _useModal.observer,
          onClose = _useModal.onClose;

      var buttonTitle = (0, _react.useMemo)(function () {
        var keyLabel = Liferay.Browser.isMac() ? '⌘' : 'Ctrl';
        return getOpenMenuTooltip(keyLabel);
      }, []);
      (0, _frontendJsReactWeb.useEventListener)('keydown', function (event) {
        var isCMDPressed = Liferay.Browser.isMac() ? event.metaKey : event.ctrlKey;

        if (isCMDPressed && event.shiftKey && event.key.toLowerCase() === 'm') {
          event.preventDefault();

          if (visible) {
            onClose();
          } else {
            handleTriggerButtonClick();
          }
        }
      }, true, window);
      var fetchCategoriesPromiseRef = (0, _react.useRef)();

      var fetchCategories = function fetchCategories() {
        if (!fetchCategoriesPromiseRef.current) {
          fetchCategoriesPromiseRef.current = (0, _frontendJsWeb.fetch)(panelAppsURL).then(function (response) {
            return response.json();
          }).then(function (_ref12) {
            var items = _ref12.items,
                portletNamespace = _ref12.portletNamespace,
                sites = _ref12.sites;
            setAppsPanelData({
              categories: items,
              portletNamespace: portletNamespace,
              selectedPortletId: selectedPortletId,
              sites: sites
            });
          })["catch"](function () {
            fetchCategoriesPromiseRef.current = null;
          });
        }
      };

      var handleTriggerButtonClick = function handleTriggerButtonClick() {
        fetchCategories();
        setVisible(true);
      };

      return _react["default"].createElement(_react["default"].Fragment, null, visible && _react["default"].createElement(_modal["default"], {
        className: "applications-menu-modal",
        observer: observer,
        size: "full",
        status: "info"
      }, _react["default"].createElement(_modal["default"].Body, null, _react["default"].createElement(AppsPanel, _extends({
        handleCloseButtonClick: onClose,
        liferayLogoURL: liferayLogoURL,
        liferayName: liferayName,
        virtualInstance: virtualInstance
      }, appsPanelData)))), _react["default"].createElement(_button.ClayButtonWithIcon, {
        "aria-label": Liferay.Language.get('open-menu'),
        className: "dropdown-toggle lfr-portal-tooltip",
        "data-qa-id": "applicationsMenu",
        displayType: "unstyled",
        onClick: handleTriggerButtonClick,
        onFocus: fetchCategories,
        onMouseOver: fetchCategories,
        small: true,
        symbol: "grid",
        title: buttonTitle
      }));
    };

    var _default = ApplicationsMenu;
    exports["default"] = _default;
    //# sourceMappingURL=ApplicationsMenu.js.map
  }
});
//# sourceMappingURL=ApplicationsMenu.js.map