Liferay.Loader.define("commerce-frontend-js@3.0.30/components/item_finder/ItemFinder", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/icon', 'frontend-js-web', 'frontend-js-react-web$prop-types', 'frontend-js-react-web$react', '../../utilities/eventsDefinitions', '../../utilities/index', '../../utilities/notifications', './AddOrCreate'], 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 = require("@frontend-taglib-clay$clayui/icon");

    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 _eventsDefinitions = require("../../utilities/eventsDefinitions");

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

    var _notifications = require("../../utilities/notifications");

    var _AddOrCreate = _interopRequireDefault(require("./AddOrCreate"));

    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 _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 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 ItemFinder(props) {
      var _useState = (0, _react.useState)([]),
          _useState2 = _slicedToArray(_useState, 2),
          items = _useState2[0],
          updateItems = _useState2[1];

      var _useState3 = (0, _react.useState)(props.pageSize),
          _useState4 = _slicedToArray(_useState3, 2),
          pageSize = _useState4[0],
          updatePageSize = _useState4[1];

      var _useState5 = (0, _react.useState)(props.currentPage),
          _useState6 = _slicedToArray(_useState5, 2),
          currentPage = _useState6[0],
          updateCurrentPage = _useState6[1];

      var _useState7 = (0, _react.useState)(''),
          _useState8 = _slicedToArray(_useState7, 2),
          textFilter = _useState8[0],
          updateTextFilter = _useState8[1];

      var _useState9 = (0, _react.useState)(props.itemsCount || 0),
          _useState10 = _slicedToArray(_useState9, 2),
          itemsCount = _useState10[0],
          updateItemsCount = _useState10[1];

      var _useState11 = (0, _react.useState)([]),
          _useState12 = _slicedToArray(_useState11, 2),
          selectedItems = _useState12[0],
          updateSelectedItems = _useState12[1];

      (0, _react.useEffect)(function () {
        if (!textFilter) {
          updateItems(null);
          updateItemsCount(0);
          return;
        }

        (0, _frontendJsWeb.fetch)("".concat(props.apiUrl).concat(props.apiUrl.includes('?') ? '&' : '?', "pageSize=").concat(pageSize, "&page=").concat(currentPage, "&search=").concat(encodeURIComponent(textFilter)), _objectSpread(_objectSpread({}, _index.fetchParams), {}, {
          method: 'GET'
        })).then(function (data) {
          return data.json();
        }).then(function (jsonResponse) {
          updateItems(jsonResponse.items);
          updateItemsCount(jsonResponse.totalCount);
        }).catch(_notifications.showErrorNotification);
      }, [pageSize, currentPage, textFilter, updateItems, updateItemsCount, props.apiUrl]);
      (0, _react.useEffect)(function () {
        props.getSelectedItems().then(function () {
          var selectedItems = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
          return updateSelectedItems(selectedItems);
        });

        function handleDatasetActions(e) {
          if (props.linkedDatasetsId.includes(e.id)) {
            props.getSelectedItems().then(function () {
              var selectedItems = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
              return updateSelectedItems(selectedItems);
            });
          }
        }

        Liferay.on(_eventsDefinitions.DATASET_ACTION_PERFORMED, handleDatasetActions);
        return function () {
          return Liferay.detach(_eventsDefinitions.DATASET_ACTION_PERFORMED, handleDatasetActions);
        };
      }, [props, props.getSelectedItems]);

      return (/*#__PURE__*/_react.default.createElement(_icon.ClayIconSpriteContext.Provider, {
          value: props.spritemap
        }, /*#__PURE__*/_react.default.createElement(_AddOrCreate.default, {
          createNewItemLabel: props.createNewItemLabel,
          currentPage: currentPage,
          inputPlaceholder: props.inputPlaceholder,
          inputSearchValue: textFilter,
          itemCreation: props.itemCreation,
          items: items,
          itemsCount: itemsCount,
          itemsKey: props.itemsKey,
          onInputSearchChange: updateTextFilter,
          onItemCreated: function createItem() {
            props.onItemCreated(textFilter).then(function (id) {
              updateTextFilter('');

              if (id) {
                updateSelectedItems(function (i) {
                  return [].concat(_toConsumableArray(i), [id]);
                });
              }
            }).catch(_notifications.showErrorNotification);
          },
          onItemSelected: function selectItem(itemId) {
            var selectedItem = items.find(function (item) {
              return item[props.itemsKey] === itemId;
            });
            props.onItemSelected(selectedItem).then(function () {
              if (props.multiSelectableEntries) {
                (0, _notifications.showNotification)(props.itemSelectedMessage);
              } else {
                updateSelectedItems(function (i) {
                  return [].concat(_toConsumableArray(i), [itemId]);
                });
              }
            }).catch(_notifications.showErrorNotification);
          },
          pageSize: pageSize,
          panelHeaderLabel: props.panelHeaderLabel,
          schema: props.schema,
          searchInputValue: textFilter,
          selectedItems: selectedItems,
          spritemap: props.spritemap,
          titleLabel: props.titleLabel,
          updateCurrentPage: updateCurrentPage,
          updatePageSize: updatePageSize
        }))
      );
    }

    ItemFinder.defaultProps = {
      currentPage: 1,
      itemCreation: true,
      itemSelectedMessage: Liferay.Language.get('item-selected'),
      multiSelectableEntries: false,
      pageSize: 5,
      selectedItems: []
    };
    var _default = ItemFinder;
    exports.default = _default;
    //# sourceMappingURL=ItemFinder.js.map
  }
});
//# sourceMappingURL=ItemFinder.js.map