Liferay.Loader.define("layout-taglib@5.4.20/select_layout/js/SearchResults", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/button', '@frontend-taglib-clay$clayui/form', '@frontend-taglib-clay$clayui/icon', '@frontend-taglib-clay$clayui/loading-indicator', 'frontend-js-web', 'frontend-js-react-web$react'], 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 = SearchResults;

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

    var _form = require("@frontend-taglib-clay$clayui/form");

    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 _react = _interopRequireWildcard(require("frontend-js-react-web$react"));

    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 _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;
    }

    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 findLayouts(url, checkDisplayPage, keywords, groupId, onFindLayouts) {
      var _Liferay$Util$objectT;

      (0, _frontendJsWeb.fetch)(url, {
        body: Liferay.Util.objectToURLSearchParams((_Liferay$Util$objectT = {}, _defineProperty(_Liferay$Util$objectT, "checkDisplayPage", checkDisplayPage), _defineProperty(_Liferay$Util$objectT, "groupId", groupId), _defineProperty(_Liferay$Util$objectT, "keywords", keywords), _Liferay$Util$objectT)),
        method: 'post'
      }).then(function (response) {
        return response.json();
      }).then(function (_ref) {
        var layouts = _ref.layouts;
        onFindLayouts(layouts);
      }).catch(function () {
        return (0, _frontendJsWeb.openToast)({
          message: Liferay.Language.get('an-unexpected-error-occurred'),
          title: Liferay.Language.get('error'),
          type: 'danger'
        });
      });
    }

    var debouncedFindLayouts = (0, _frontendJsWeb.debounce)(findLayouts, 300);

    function SearchResults(_ref2) {
      var checkDisplayPage = _ref2.checkDisplayPage,
          filter = _ref2.filter,
          findLayoutsURL = _ref2.findLayoutsURL,
          groupId = _ref2.groupId,
          multiSelection = _ref2.multiSelection,
          onSelect = _ref2.onSelect,
          selection = _ref2.selection;

      var _useState = (0, _react.useState)([]),
          _useState2 = _slicedToArray(_useState, 2),
          results = _useState2[0],
          setResults = _useState2[1];

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

      var onFindLayouts = (0, _react.useCallback)(function (layouts) {
        setLoading(false);
        setResults(layouts);
      }, []);
      (0, _react.useEffect)(function () {
        setLoading(true);
        debouncedFindLayouts(findLayoutsURL, checkDisplayPage, filter, groupId, onFindLayouts);
      }, [checkDisplayPage, filter, findLayoutsURL, groupId, onFindLayouts]);

      if (loading) {
        return (/*#__PURE__*/_react.default.createElement(_loadingIndicator.default, {
            displayType: "secondary"
          })
        );
      }

      return (/*#__PURE__*/_react.default.createElement("div", {
          className: "pt-3"
        }, results.map(function (layout) {
          return (/*#__PURE__*/_react.default.createElement(SearchResult, {
              key: layout.id,
              layout: layout,
              multiSelection: multiSelection,
              onSelect: onSelect,
              selection: selection
            })
          );
        }))
      );
    }

    function SearchResult(_ref3) {
      var layout = _ref3.layout,
          multiSelection = _ref3.multiSelection,
          onSelect = _ref3.onSelect,
          selection = _ref3.selection;
      return (/*#__PURE__*/_react.default.createElement("div", {
          className: "align-items-center d-flex pb-2"
        }, multiSelection && /*#__PURE__*/_react.default.createElement(_form.ClayCheckbox, {
          checked: selection.includes(layout.id),
          containerProps: {
            className: 'mr-3 my-0'
          },
          disabled: layout.disabled,
          onChange: function onChange() {
            return onSelect(layout);
          }
        }), layout.path.map(function (ancestor, index) {
          return (/*#__PURE__*/_react.default.createElement("span", {
              className: "pr-2 text-secondary",
              key: index
            }, ancestor, /*#__PURE__*/_react.default.createElement(_icon.default, {
              className: "ml-2",
              symbol: "angle-right-small"
            }))
          );
        }), multiSelection ? /*#__PURE__*/_react.default.createElement("span", {
          className: "font-weight-semi-bold p-0"
        }, layout.name) : /*#__PURE__*/_react.default.createElement(_button.default, {
          className: "font-weight-semi-bold px-0 py-1 search-result-button",
          disabled: layout.disabled,
          displayType: "unstyled",
          onClick: function onClick() {
            return onSelect(layout);
          }
        }, layout.name))
      );
    }
    //# sourceMappingURL=SearchResults.js.map
  }
});
//# sourceMappingURL=SearchResults.js.map