Liferay.Loader.define("commerce-frontend-js@3.0.33/components/item_finder/AddOrCreate", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/button', '@frontend-taglib-clay$clayui/icon', '@frontend-taglib-clay$clayui/list', '@frontend-taglib-clay$clayui/pagination-bar', '@frontend-taglib-clay$clayui/table', 'frontend-js-react-web$classnames', 'frontend-js-react-web$prop-types', 'frontend-js-react-web$react', '../../utilities/index', '../data_renderers/index', './Expose'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.default = void 0;

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

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

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

    var _paginationBar = require("@frontend-taglib-clay$clayui/pagination-bar");

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

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

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

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

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

    var _index2 = require("../data_renderers/index");

    var _Expose = _interopRequireDefault(require("./Expose"));

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

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

    function _classCallCheck(instance, Constructor) {
      if (!(instance instanceof Constructor)) {
        throw new TypeError("Cannot call a class as a function");
      }
    }

    function _defineProperties(target, props) {
      for (var i = 0; i < props.length; i++) {
        var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ("value" in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);
      }
    }

    function _createClass(Constructor, protoProps, staticProps) {
      if (protoProps) _defineProperties(Constructor.prototype, protoProps);if (staticProps) _defineProperties(Constructor, staticProps);return Constructor;
    }

    function _possibleConstructorReturn(self, call) {
      if (call && (_typeof(call) === "object" || typeof call === "function")) {
        return call;
      }return _assertThisInitialized(self);
    }

    function _assertThisInitialized(self) {
      if (self === void 0) {
        throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
      }return self;
    }

    function _getPrototypeOf(o) {
      _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
        return o.__proto__ || Object.getPrototypeOf(o);
      };return _getPrototypeOf(o);
    }

    function _inherits(subClass, superClass) {
      if (typeof superClass !== "function" && superClass !== null) {
        throw new TypeError("Super expression must either be null or a function");
      }subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } });if (superClass) _setPrototypeOf(subClass, superClass);
    }

    function _setPrototypeOf(o, p) {
      _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
        o.__proto__ = p;return o;
      };return _setPrototypeOf(o, p);
    }

    function Item(props) {
      return (/*#__PURE__*/_react.default.createElement(_table.default.Row, null, props.fields.map(function (field, i) {
          var value = (0, _index.getValueFromItem)(props.itemData, field.fieldName);
          var DataRenderer = (0, _index2.getDataRendererById)(field.contentRenderer);
          return (/*#__PURE__*/_react.default.createElement(_table.default.Cell, {
              expanded: true,
              headingTitle: i === 0,
              key: Array.isArray(field.fieldName) ? field.fieldName[0] : field.fieldName
            }, /*#__PURE__*/_react.default.createElement(DataRenderer, {
              actions: [],
              itemData: props.itemData,
              options: field,
              value: value
            }))
          );
        }), /*#__PURE__*/_react.default.createElement(_table.default.Cell, null, /*#__PURE__*/_react.default.createElement(_button.default, {
          disabled: props.selected,
          displayType: "secondary",
          onClick: props.onSelect,
          small: true
        }, Liferay.Language.get('select'))))
      );
    }

    var AddOrCreateBase = /*#__PURE__*/function (_Component) {
      _inherits(AddOrCreateBase, _Component);

      function AddOrCreateBase(props) {
        var _this;

        _classCallCheck(this, AddOrCreateBase);

        _this = _possibleConstructorReturn(this, _getPrototypeOf(AddOrCreateBase).call(this, props));
        _this.state = {
          focus: false,
          selected: 0
        };
        return _this;
      }

      _createClass(AddOrCreateBase, [{
        key: "focus",
        value: function focus() {
          this.setState({
            focus: true
          });

          if (this.props.onFocusIn) {
            this.props.onFocusIn();
          }
        }
      }, {
        key: "handleFocusIn",
        value: function handleFocusIn(_e) {
          clearTimeout(this._timeoutID);
        }
      }, {
        key: "render",
        value: function render() {
          var _this2 = this;

          return (/*#__PURE__*/_react.default.createElement("div", {
              className: "card mb-0 add-or-create ".concat(this.state.focus ? 'has-focus' : ''),
              onFocus: function onFocus(e) {
                return _this2.handleFocusIn(e);
              }
            }, /*#__PURE__*/_react.default.createElement("h4", {
              className: "align-items-center card-header py-3"
            }, this.props.panelHeaderLabel), /*#__PURE__*/_react.default.createElement("div", {
              className: "card-body"
            }, /*#__PURE__*/_react.default.createElement("div", {
              className: "input-group"
            }, /*#__PURE__*/_react.default.createElement("div", {
              className: "input-group-item"
            }, /*#__PURE__*/_react.default.createElement("input", {
              className: "form-control input-group-inset input-group-inset-after",
              onChange: function onChange(e) {
                return _this2.props.onInputSearchChange(e.target.value);
              },
              onFocus: function onFocus(e) {
                return _this2.focus(e);
              },
              placeholder: this.props.inputPlaceholder,
              ref: this.input,
              type: "text",
              value: this.props.inputSearchValue
            }), /*#__PURE__*/_react.default.createElement("span", {
              className: "input-group-inset-item input-group-inset-item-after"
            }, this.props.inputSearchValue && /*#__PURE__*/_react.default.createElement("button", {
              className: "btn btn-unstyled",
              onClick: function onClick(_e) {
                return _this2.props.onInputSearchChange('');
              },
              type: "button"
            }, /*#__PURE__*/_react.default.createElement(_icon.default, {
              symbol: "times"
            })))))), this.props.active && (this.props.inputSearchValue || this.props.items && this.props.items.length) && /*#__PURE__*/_react.default.createElement("div", {
              className: "card-body"
            }, /*#__PURE__*/_react.default.createElement(_list.default, null, this.props.itemCreation && this.props.inputSearchValue && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_list.default.Header, {
              className: "px-0"
            }, this.props.createNewItemLabel), /*#__PURE__*/_react.default.createElement(_list.default.Item, {
              className: (0, _classnames.default)('py-3', this.props.items && this.props.items.length && 'border-bottom mb-3'),
              flex: true
            }, /*#__PURE__*/_react.default.createElement(_list.default.ItemField, {
              expand: true
            }, /*#__PURE__*/_react.default.createElement(_list.default.ItemTitle, null, "\"", this.props.inputSearchValue, "\"")), /*#__PURE__*/_react.default.createElement(_list.default.ItemField, null, /*#__PURE__*/_react.default.createElement(_button.default, {
              onClick: this.props.onItemCreated,
              small: true
            }, Liferay.Language.get('create-new'))))), this.props.items && this.props.items.length === 0 && !this.props.itemCreation && /*#__PURE__*/_react.default.createElement(_list.default.Header, {
              className: "d-flex px-0"
            }, Liferay.Language.get('no-items-were-found'))), this.props.items && this.props.items.length ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, this.props.itemCreation && /*#__PURE__*/_react.default.createElement(_list.default.Header, {
              className: "px-0"
            }, this.props.titleLabel), /*#__PURE__*/_react.default.createElement(_table.default, {
              borderless: true,
              hover: false,
              responsive: true
            }, /*#__PURE__*/_react.default.createElement(_table.default.Body, null, this.props.items.map(function (item, i) {
              return (/*#__PURE__*/_react.default.createElement(Item, {
                  fields: _this2.props.schema,
                  itemData: item,
                  key: item[_this2.props.itemsKey] || i,
                  onSelect: function onSelect() {
                    return _this2.props.onItemSelected(item[_this2.props.itemsKey]);
                  },
                  selected: _this2.props.selectedItems.includes(item[_this2.props.itemsKey])
                })
              );
            }))), /*#__PURE__*/_react.default.createElement(_paginationBar.ClayPaginationBarWithBasicItems, {
              activeDelta: this.props.pageSize,
              activePage: this.props.currentPage,
              className: "mt-3",
              deltas: this.props.deltas,
              ellipsisBuffer: 3,
              onDeltaChange: function onDeltaChange(deltaVal) {
                _this2.props.updateCurrentPage(1);

                _this2.props.updatePageSize(deltaVal);
              },
              onPageChange: this.props.updateCurrentPage,
              spritemap: this.props.spritemap,
              totalItems: this.props.itemsCount
            })) : null))
          );
        }
      }]);

      return AddOrCreateBase;
    }(_react.Component);

    AddOrCreateBase.defaultProps = {
      createNewItemLabel: Liferay.Language.get('create-new-item'),
      deltas: [{
        label: 5
      }, {
        label: 10
      }, {
        label: 20
      }, {
        label: 30
      }, {
        label: 50
      }, {
        label: 75
      }],
      inputPlaceholder: Liferay.Language.get('find-an-option-or-create-one'),
      inputSearchValue: '',
      panelHeaderLabel: Liferay.Language.get('add-new'),
      titleLabel: Liferay.Language.get('select-an-existing-one')
    };

    var _default = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
      var _React$useState = _react.default.useState(false),
          _React$useState2 = _slicedToArray(_React$useState, 2),
          active = _React$useState2[0],
          setActive = _React$useState2[1];

      return (/*#__PURE__*/_react.default.createElement(_Expose.default, {
          active: active,
          onClose: function onClose() {
            return setActive(false);
          }
        }, /*#__PURE__*/_react.default.createElement(AddOrCreateBase, _extends({}, props, {
          active: active,
          innerRef: ref,
          onFocusIn: function onFocusIn() {
            return setActive(true);
          },
          onFocusOut: function onFocusOut() {
            return setActive(false);
          },
          onSubmit: function closeAndSubmit(e) {
            props.onSubmit(e);
            setActive(false);
          }
        })))
      );
    });

    exports.default = _default;
    //# sourceMappingURL=AddOrCreate.js.map
  }
});
//# sourceMappingURL=AddOrCreate.js.map