Liferay.Loader.define("asset-categories-selector-web@3.0.22/js/SelectCategory.es", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/alert', '@frontend-taglib-clay$clayui/button', '@frontend-taglib-clay$clayui/icon', '@frontend-taglib-clay$clayui/layout', 'frontend-js-components-web', 'frontend-js-react-web$prop-types', '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"] = void 0;

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

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

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

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

    var _frontendJsComponentsWeb = require("frontend-js-components-web");

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

    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 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 _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 visit(nodes, callback) {
      nodes.forEach(function (node) {
        callback(node);

        if (node.children) {
          visit(node.children, callback);
        }
      });
    }

    function SelectCategory(_ref) {
      var addCategoryURL = _ref.addCategoryURL,
          itemSelectorSaveEvent = _ref.itemSelectorSaveEvent,
          moveCategory = _ref.moveCategory,
          multiSelection = _ref.multiSelection,
          namespace = _ref.namespace,
          nodes = _ref.nodes;
      var flattenedNodes = (0, _react.useMemo)(function () {
        if (nodes.length === 1 && nodes[0].vocabulary) {
          return nodes[0].children;
        }

        return nodes;
      }, [nodes]);

      var _useState = (0, _react.useState)(''),
          _useState2 = _slicedToArray(_useState, 2),
          filterQuery = _useState2[0],
          setFilterQuery = _useState2[1];

      var selectedNodesRef = (0, _react.useRef)(null);
      var handleQueryChange = (0, _react.useCallback)(function (event) {
        var value = event.target.value;
        setFilterQuery(value);
      }, []);
      var handleAddCategoryClick = (0, _react.useCallback)(function () {
        var dialog = Liferay.Util.getWindow(itemSelectorSaveEvent);
        var footer = dialog.getToolbar('footer');
        footer.get('boundingBox').one('#addButton').hide();
        footer.get('boundingBox').one('#cancelButton').hide();
        Liferay.Util.navigate(addCategoryURL);
      }, [addCategoryURL, itemSelectorSaveEvent]);
      (0, _react.useEffect)(function () {
        var dialog = Liferay.Util.getWindow(itemSelectorSaveEvent);
        var footer = dialog.getToolbar('footer');

        if (!dialog.get('initialTitle')) {
          dialog.set('initialTitle', dialog.headerNode.one('.modal-title').text());
        }

        footer.get('boundingBox').all('.add-category-toolbar-button').hide();
        footer.get('boundingBox').one('#addButton').show();
        footer.get('boundingBox').one('#cancelButton').show();

        if (dialog.get('initialTitle') !== dialog.headerNode.one('.modal-title').text()) {
          dialog.headerNode.one('.modal-title').text(dialog.get('initialTitle'));
        }
      }, [itemSelectorSaveEvent]);

      var initialSelectedNodeIds = (0, _react.useMemo)(function () {
        var selectedNodes = [];
        visit(flattenedNodes, function (node) {
          if (node.selected) {
            selectedNodes.push(node.id);
          }
        });
        return selectedNodes;
      }, [flattenedNodes]);
      return _react["default"].createElement("div", {
        className: "select-category"
      }, moveCategory && _react["default"].createElement(_alert["default"], {
        displayType: "info",
        variant: "embedded"
      }, Liferay.Language.get('categories-can-only-be-moved-to-a-vocabulary-or-a-category-with-the-same-visibility')), _react["default"].createElement("form", {
        className: "select-category-filter",
        onSubmit: function onSubmit(event) {
          return event.preventDefault();
        },
        role: "search"
      }, _react["default"].createElement(_layout["default"].ContainerFluid, {
        className: "d-flex"
      }, _react["default"].createElement("div", {
        className: "input-group"
      }, _react["default"].createElement("div", {
        className: "input-group-item"
      }, _react["default"].createElement("input", {
        className: "form-control h-100 input-group-inset input-group-inset-after",
        onChange: handleQueryChange,
        placeholder: Liferay.Language.get('search'),
        type: "text"
      }), _react["default"].createElement("div", {
        className: "input-group-inset-item input-group-inset-item-after pr-3"
      }, _react["default"].createElement(_icon["default"], {
        symbol: "search"
      })))), addCategoryURL && _react["default"].createElement(_button["default"], {
        className: "btn-monospaced ml-3 nav-btn nav-btn-monospaced",
        displayType: "primary",
        onClick: handleAddCategoryClick
      }, _react["default"].createElement(_icon["default"], {
        symbol: "plus"
      })))), _react["default"].createElement("form", {
        name: "".concat(namespace, "selectCategoryFm")
      }, _react["default"].createElement(_layout["default"].ContainerFluid, {
        containerElement: "fieldset"
      }, _react["default"].createElement("div", {
        className: "category-tree",
        id: "".concat(namespace, "categoryContainer")
      }, flattenedNodes.length > 0 ? _react["default"].createElement(_frontendJsComponentsWeb.Treeview, {
        filterQuery: filterQuery,
        initialSelectedNodeIds: initialSelectedNodeIds,
        multiSelection: multiSelection,
        NodeComponent: _frontendJsComponentsWeb.Treeview.Card,
        nodes: flattenedNodes,
        onSelectedNodesChange: function handleSelectionChange(selectedNodes) {
          var data = {}; // Mark newly selected nodes as selected.

          visit(flattenedNodes, function (node) {
            if (selectedNodes.has(node.id)) {
              data[node.id] = {
                categoryId: node.vocabulary ? 0 : node.id,
                nodePath: node.nodePath,
                value: node.name,
                vocabularyId: node.vocabulary ? node.id : 0
              };
            }
          }); // Mark unselected nodes as unchecked.

          if (selectedNodesRef.current) {
            Object.entries(selectedNodesRef.current).forEach(function (_ref2) {
              var _ref3 = _slicedToArray(_ref2, 2),
                  id = _ref3[0],
                  node = _ref3[1];

              if (!selectedNodes.has(id)) {
                data[id] = _objectSpread({}, node, {
                  unchecked: true
                });
              }
            });
          }

          selectedNodesRef.current = data;
          Liferay.Util.getOpener().Liferay.fire(itemSelectorSaveEvent, {
            data: data
          });
        }
      }) : _react["default"].createElement("div", {
        className: "border-0 pt-0 sheet taglib-empty-result-message"
      }, _react["default"].createElement("div", {
        className: "taglib-empty-result-message-header"
      }), _react["default"].createElement("div", {
        className: "sheet-text text-center"
      }, Liferay.Language.get('no-categories-were-found')))))));
    }

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