Liferay.Loader.define("product-navigation-control-menu-web@5.0.58/js/LayoutFinder.es", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/icon', '@frontend-taglib-clay$clayui/loading-indicator', 'frontend-js-web', 'frontend-js-react-web$prop-types', 'frontend-js-react-web$react', './useDebounceCallback.es'], 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 _icon = _interopRequireDefault(require("@frontend-taglib-clay$clayui/icon"));

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

    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"));

    var _useDebounceCallback3 = require("./useDebounceCallback.es");

    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 _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 _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 MAX_ITEMS_TO_SHOW = 10;

    function LayoutFinder(props) {
      var _useState = (0, _react.useState)(''),
          _useState2 = _slicedToArray(_useState, 2),
          keywords = _useState2[0],
          setKeywords = _useState2[1];

      var _useState3 = (0, _react.useState)([]),
          _useState4 = _slicedToArray(_useState3, 2),
          layouts = _useState4[0],
          setLayouts = _useState4[1];

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

      var _useState7 = (0, _react.useState)(0),
          _useState8 = _slicedToArray(_useState7, 2),
          totalCount = _useState8[0],
          setTotalCount = _useState8[1];

      var handleFormSubmit = (0, _react.useCallback)(function (event) {
        event.preventDefault();
        event.stopPropagation();
      }, []);

      var _useDebounceCallback = (0, _useDebounceCallback3.useDebounceCallback)(function (newKeywords) {
        (0, _frontendJsWeb.fetch)(props.findLayoutsURL, {
          body: (0, _frontendJsWeb.objectToFormData)(_defineProperty({}, "".concat(props.namespace, "keywords"), newKeywords)),
          method: 'post'
        }).then(function (response) {
          return response.ok ? response.json() : {
            layouts: [],
            totalCount: 0
          };
        }).then(function (response) {
          setLoading(false);
          setLayouts(response.layouts);
          setTotalCount(response.totalCount);
        });
      }, 1000),
          _useDebounceCallback2 = _slicedToArray(_useDebounceCallback, 2),
          updatePageResults = _useDebounceCallback2[0],
          cancelUpdatePageResults = _useDebounceCallback2[1];

      var handleOnChange = (0, _react.useCallback)(function (event) {
        var newKeywords = event.target.value;
        setKeywords(newKeywords);
        var tree = document.querySelector("#".concat(props.namespace, "layoutsTree"));

        if (newKeywords.length == 0) {
          setLoading(false);
          setLayouts([]);
          setTotalCount(0);
          cancelUpdatePageResults();
          tree.classList.remove('hide');
        } else {
          setLoading(true);
          updatePageResults(newKeywords);

          if (!tree.classList.contains('hide')) {
            tree.classList.add('hide');
          }
        }
      }, [cancelUpdatePageResults, props.namespace, updatePageResults]);
      var handleOnClick = (0, _react.useCallback)(function () {
        Liferay.Portlet.destroy("#p_p_id".concat(props.namespace), true);
        Liferay.Util.Session.set('com.liferay.product.navigation.product.menu.web_pagesTreeState', 'closed').then(function () {
          (0, _frontendJsWeb.fetch)(props.productMenuPortletURL).then(function (response) {
            if (!response.ok) {
              throw new Error();
            }

            return response.text();
          }).then(function (productMenuContent) {
            var sidebar = document.querySelector('.lfr-product-menu-sidebar .sidebar-body');
            sidebar.innerHTML = '';
            var range = document.createRange();
            range.selectNode(sidebar);
            sidebar.appendChild(range.createContextualFragment(productMenuContent));
          }).catch(function () {
            (0, _frontendJsWeb.openToast)({
              message: Liferay.Language.get('an-unexpected-error-occurred'),
              title: Liferay.Language.get('error'),
              type: 'danger'
            });
          });
        });
      }, [props]);
      return (/*#__PURE__*/_react.default.createElement("div", {
          className: "layout-finder"
        }, /*#__PURE__*/_react.default.createElement("button", {
          className: "back-to-menu btn btn-sm btn-unstyled mb-3 pr-3",
          onClick: handleOnClick
        }, /*#__PURE__*/_react.default.createElement(_icon.default, {
          className: "icon-monospaced",
          symbol: "angle-left"
        }), "".concat(Liferay.Language.get('back-to-menu'), " ")), /*#__PURE__*/_react.default.createElement("form", {
          onSubmit: handleFormSubmit,
          role: "search"
        }, /*#__PURE__*/_react.default.createElement("label", {
          className: "sr-only",
          htmlFor: "".concat(props.namespace, "-layout-finder-page-input")
        }, "".concat(Liferay.Language.get('page-name'), ": ")), /*#__PURE__*/_react.default.createElement("input", {
          autoComplete: "off",
          className: "form-control form-control-sm",
          id: "".concat(props.namespace, "-layout-finder-page-input"),
          onChange: handleOnChange,
          placeholder: Liferay.Language.get('start-typing-to-find-a-page'),
          type: "text",
          value: keywords
        })), totalCount > 0 && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("nav", {
          className: "mt-2"
        }, layouts.map(function (layout, layoutIndex) {
          return layoutIndex < MAX_ITEMS_TO_SHOW && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("ol", {
            className: "breadcrumb"
          }, layout.path && layout.path.length > 0 && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, layout.path.map(function (layoutPath) {
            return (/*#__PURE__*/_react.default.createElement("li", {
                className: "breadcrumb-item text-secondary",
                key: layoutPath
              }, /*#__PURE__*/_react.default.createElement("span", {
                className: "breadcrumb-text-truncate"
              }, layoutPath))
            );
          })), /*#__PURE__*/_react.default.createElement("li", {
            className: "breadcrumb-item",
            key: layout.name
          }, /*#__PURE__*/_react.default.createElement("a", {
            className: "d-block font-weight-bold mb-2 text-break",
            href: layout.url,
            key: layout.url
          }, layout.name))));
        })), totalCount > MAX_ITEMS_TO_SHOW && /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement("div", {
          className: "mb-3 mt-3 text-center"
        }, Liferay.Util.sub(Liferay.Language.get('there-are-x-more-results-narrow-your-searc-to-get-more-precise-results'), totalCount - MAX_ITEMS_TO_SHOW)), /*#__PURE__*/_react.default.createElement("div", {
          className: "text-center"
        }, /*#__PURE__*/_react.default.createElement("a", {
          href: "".concat(props.administrationPortletURL, "&").concat(props.administrationPortletNamespace, "keywords=").concat(keywords)
        }, Liferay.Language.get('view-in-page-administration'))))), loading && /*#__PURE__*/_react.default.createElement(_loadingIndicator.default, {
          className: "mb-0 mt-3",
          light: true,
          small: true
        }), totalCount === 0 && !loading && keywords.length > 1 && /*#__PURE__*/_react.default.createElement("div", {
          className: "mt-3 text-center"
        }, Liferay.Language.get('page-not-found')))
      );
    }

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