"use strict";var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;exports.__esModule = true;exports.ComboBoxMenuDataTids = exports.ComboBoxMenu = void 0;var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));var _react = _interopRequireDefault(require("react"));

var _utils = require("../../lib/utils");
var _decorators = require("../../lib/locale/decorators");
var _Menu = require("../Menu");

var _MenuItem = require("../../components/MenuItem");
var _Spinner = require("../../components/Spinner");

var _MenuSeparator = require("../../components/MenuSeparator");
var _createPropsGetter = require("../../lib/createPropsGetter");
var _MenuMessage = require("../MenuMessage");


var _ThemeContext = require("../../lib/theming/ThemeContext");
var _MenuFooter = require("../../components/MenuFooter");


var _CustomComboBoxTypes = require("./CustomComboBoxTypes");

var _locale = require("./locale");var _dec, _class, _ComboBoxMenu;























var ComboBoxMenuDataTids = exports.ComboBoxMenuDataTids = {
  loading: 'ComboBoxMenu__loading',
  failed: 'ComboBoxMenu__failed',
  notFound: 'ComboBoxMenu__notFound',
  items: 'ComboBoxMenu__items',
  item: 'ComboBoxMenu__item'
};var




ComboBoxMenu = exports.ComboBoxMenu = (_dec = (0, _decorators.locale)('ComboBox', _locale.CustomComboBoxLocaleHelper), _dec(_class = (_ComboBoxMenu = /*#__PURE__*/function (_React$Component) {function ComboBoxMenu() {var _this;for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {args[_key] = arguments[_key];}_this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;_this.








    getProps = (0, _createPropsGetter.createPropsGetter)(ComboBoxMenu.defaultProps);_this.

















































































































































    renderItem = function (item, index) {
      // NOTE this is undesireable feature, better
      // to remove it from further versions
      var _this$props = _this.props,renderItem = _this$props.renderItem,onValueChange = _this$props.onValueChange,itemWrapper = _this$props.itemWrapper;

      if (!isSimpleItem(item)) {
        var element = (0, _utils.isFunction)(item) ? item() : item;
        var props = Object.assign(
          {
            key: index,
            onClick: function onClick() {return onValueChange(element.props);}
          },
          element.props
        );
        return /*#__PURE__*/_react.default.cloneElement(element, props);
      }

      return /*#__PURE__*/(
        _react.default.createElement(_MenuItem.MenuItem, {
          component: itemWrapper == null ? void 0 : itemWrapper(item),
          "data-tid": ComboBoxMenuDataTids.item,
          onClick: function onClick() {return onValueChange(item);},
          key: index,
          size: _this.props.size,
          isMobile: _this.props.isMobile },

        function (state) {return renderItem(item, state);}
        ));

    };return _this;}(0, _inheritsLoose2.default)(ComboBoxMenu, _React$Component);var _proto = ComboBoxMenu.prototype;_proto.render = function render() {var _this2 = this;return /*#__PURE__*/_react.default.createElement(_ThemeContext.ThemeContext.Consumer, null, function (theme) {_this2.theme = theme;return /*#__PURE__*/_react.default.createElement(_ThemeContext.ThemeContext.Provider, { value: _this2.theme }, _this2.renderMain());});};_proto.renderMain = function renderMain() {var _this$props2 = this.props,opened = _this$props2.opened,items = _this$props2.items,totalCount = _this$props2.totalCount,loading = _this$props2.loading,refMenu = _this$props2.refMenu,_this$props2$renderNo = _this$props2.renderNotFound,renderNotFound = _this$props2$renderNo === void 0 ? function () {return notFound;} : _this$props2$renderNo,renderTotalCount = _this$props2.renderTotalCount,maxMenuHeight = _this$props2.maxMenuHeight,isMobile = _this$props2.isMobile;var requestStatus = this.getProps().requestStatus;var _this$locale = this.locale,notFound = _this$locale.notFound,errorNetworkButton = _this$locale.errorNetworkButton,errorNetworkMessage = _this$locale.errorNetworkMessage;if (!opened) {return null;}var renderAddButton = null;if (this.props.renderAddButton) {renderAddButton = this.props.renderAddButton();}var maxHeight = isMobile ? 'auto' : maxMenuHeight;if (loading && (!items || !items.length)) {return /*#__PURE__*/_react.default.createElement(_Menu.Menu, { maxHeight: maxHeight, ref: refMenu, hasMargin: this.props.hasMargin, disableScrollContainer: isMobile, id: this.props.menuId, "data-tid": ComboBoxMenuDataTids.loading + " " + _Menu.MenuDataTids.root }, /*#__PURE__*/_react.default.createElement(_MenuMessage.MenuMessage, { size: this.props.size, as: "div" }, /*#__PURE__*/_react.default.createElement(_Spinner.Spinner, { type: "mini", dimmed: true })));}if (items === null && requestStatus === _CustomComboBoxTypes.ComboBoxRequestStatus.Failed) {return /*#__PURE__*/_react.default.createElement(_Menu.Menu, { ref: refMenu, maxHeight: maxHeight, hasMargin: this.props.hasMargin, disableScrollContainer: isMobile, id: this.props.menuId, "data-tid": ComboBoxMenuDataTids.failed + " " + _Menu.MenuDataTids.root }, /*#__PURE__*/_react.default.createElement(_MenuMessage.MenuMessage, { size: this.props.size, key: "message" }, /*#__PURE__*/_react.default.createElement("div", { style: { maxWidth: 300, whiteSpace: 'normal' } }, errorNetworkMessage)), /*#__PURE__*/_react.default.createElement(_MenuItem.MenuItem, { link: true, onClick: this.getProps().repeatRequest, size: this.props.size, key: "retry", isMobile: isMobile }, errorNetworkButton));}if (((0, _utils.isNullable)(items) || items.length === 0) && renderNotFound) {var notFoundValue = renderNotFound();if (renderAddButton) {return /*#__PURE__*/_react.default.createElement(_Menu.Menu, { id: this.props.menuId, hasMargin: this.props.hasMargin, maxHeight: maxHeight, ref: refMenu, disableScrollContainer: isMobile }, renderAddButton);}if (notFoundValue) {return /*#__PURE__*/_react.default.createElement(_Menu.Menu, { id: this.props.menuId, hasMargin: this.props.hasMargin, maxHeight: maxHeight, ref: refMenu, disableScrollContainer: isMobile }, /*#__PURE__*/_react.default.createElement(_MenuMessage.MenuMessage, { size: this.props.size, "data-tid": ComboBoxMenuDataTids.notFound }, notFoundValue));}return null;}var total = null;var renderedItems = items && items.map(this.renderItem);var menuItems = renderedItems == null ? void 0 : renderedItems.filter(function (item) {return (0, _MenuItem.isMenuItem)(item);});var countItems = menuItems == null ? void 0 : menuItems.length;if (countItems && renderTotalCount && totalCount && countItems < totalCount) {total = /*#__PURE__*/_react.default.createElement(_MenuFooter.MenuFooter, { size: this.props.size, key: "total" }, /*#__PURE__*/_react.default.createElement("div", null, renderTotalCount(countItems, totalCount)));}return /*#__PURE__*/_react.default.createElement(_Menu.Menu, { id: this.props.menuId, "data-tid": ComboBoxMenuDataTids.items + " " + _Menu.MenuDataTids.root, ref: refMenu, maxHeight: maxHeight, hasMargin: this.props.hasMargin, disableScrollContainer: isMobile }, renderedItems, total, renderAddButton && [/*#__PURE__*/_react.default.createElement(_MenuSeparator.MenuSeparator, { key: "separator" }), renderAddButton]);};return ComboBoxMenu;}(_react.default.Component), _ComboBoxMenu.__KONTUR_REACT_UI__ = 'ComboBoxMenu', _ComboBoxMenu.displayName = 'ComboBoxMenu', _ComboBoxMenu.defaultProps = { repeatRequest: function repeatRequest() {return undefined;}, requestStatus: _CustomComboBoxTypes.ComboBoxRequestStatus.Unknown }, _ComboBoxMenu)) || _class);


function isSimpleItem(item) {
  return !(0, _utils.isFunction)(item) && ! /*#__PURE__*/_react.default.isValidElement(item);
}