import _inheritsLoose from "@babel/runtime/helpers/esm/inheritsLoose";
var _dec, _class, _ComboBoxMenu;
import React from 'react';
import { isFunction, isNullable } from "../../../lib/utils";
import { locale } from "../../../lib/locale/decorators";
import { Menu, MenuDataTids } from "../../Menu";
import { isMenuItem, MenuItem } from "../../../components/MenuItem";
import { Spinner } from "../../../components/Spinner";
import { MenuSeparator } from "../../../components/MenuSeparator";
import { createPropsGetter } from "../../../lib/createPropsGetter";
import { MenuMessage } from "../../MenuMessage";
import { ThemeContext } from "../../../lib/theming/ThemeContext";
import { MenuFooter } from "../../../components/MenuFooter";
import { ComboBoxRequestStatus } from "../CustomComboBoxTypes";
import { CustomComboBoxLocaleHelper } from "../locale";
export var ComboBoxMenuDataTids = {
  loading: 'ComboBoxMenu__loading',
  failed: 'ComboBoxMenu__failed',
  notFound: 'ComboBoxMenu__notFound',
  items: 'ComboBoxMenu__items',
  item: 'ComboBoxMenu__item'
};
export var ComboBoxMenu = (_dec = locale('ComboBox', 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 = 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 = isFunction(item) ? item() : item;
        var props = Object.assign({
          key: index,
          onClick: function onClick() {
            return onValueChange(element.props);
          }
        }, element.props);
        return /*#__PURE__*/React.cloneElement(element, props);
      }
      return /*#__PURE__*/(
        React.createElement(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;
  }
  _inheritsLoose(ComboBoxMenu, _React$Component);
  var _proto = ComboBoxMenu.prototype;
  _proto.render = function render() {
    var _this2 = this;
    return /*#__PURE__*/React.createElement(ThemeContext.Consumer, null, function (theme) {
      _this2.theme = theme;
      return /*#__PURE__*/React.createElement(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.createElement(Menu, {
        maxHeight: maxHeight,
        ref: refMenu,
        hasMargin: this.props.hasMargin,
        disableScrollContainer: isMobile,
        id: this.props.menuId,
        "data-tid": ComboBoxMenuDataTids.loading + " " + MenuDataTids.root
      }, /*#__PURE__*/React.createElement(MenuMessage, {
        size: this.props.size,
        as: "div"
      }, /*#__PURE__*/React.createElement(Spinner, {
        type: "mini",
        dimmed: true
      })));
    }
    if (items === null && requestStatus === ComboBoxRequestStatus.Failed) {
      return /*#__PURE__*/React.createElement(Menu, {
        ref: refMenu,
        maxHeight: maxHeight,
        hasMargin: this.props.hasMargin,
        disableScrollContainer: isMobile,
        id: this.props.menuId,
        "data-tid": ComboBoxMenuDataTids.failed + " " + MenuDataTids.root
      }, /*#__PURE__*/React.createElement(MenuMessage, {
        size: this.props.size,
        key: "message"
      }, /*#__PURE__*/React.createElement("div", {
        style: {
          maxWidth: 300,
          whiteSpace: 'normal'
        }
      }, errorNetworkMessage)), /*#__PURE__*/React.createElement(MenuItem, {
        link: true,
        onClick: this.getProps().repeatRequest,
        size: this.props.size,
        key: "retry",
        isMobile: isMobile
      }, errorNetworkButton));
    }
    if ((isNullable(items) || items.length === 0) && renderNotFound) {
      var notFoundValue = renderNotFound();
      if (renderAddButton) {
        return /*#__PURE__*/React.createElement(Menu, {
          id: this.props.menuId,
          hasMargin: this.props.hasMargin,
          maxHeight: maxHeight,
          ref: refMenu,
          disableScrollContainer: isMobile
        }, renderAddButton);
      }
      if (notFoundValue) {
        return /*#__PURE__*/React.createElement(Menu, {
          id: this.props.menuId,
          hasMargin: this.props.hasMargin,
          maxHeight: maxHeight,
          ref: refMenu,
          disableScrollContainer: isMobile
        }, /*#__PURE__*/React.createElement(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 isMenuItem(item);
    });
    var countItems = menuItems == null ? void 0 : menuItems.length;
    if (countItems && renderTotalCount && totalCount && countItems < totalCount) {
      total = /*#__PURE__*/React.createElement(MenuFooter, {
        size: this.props.size,
        key: "total"
      }, /*#__PURE__*/React.createElement("div", null, renderTotalCount(countItems, totalCount)));
    }
    return /*#__PURE__*/React.createElement(Menu, {
      id: this.props.menuId,
      "data-tid": ComboBoxMenuDataTids.items + " " + MenuDataTids.root,
      ref: refMenu,
      maxHeight: maxHeight,
      hasMargin: this.props.hasMargin,
      disableScrollContainer: isMobile
    }, renderedItems, total, renderAddButton && [/*#__PURE__*/React.createElement(MenuSeparator, {
      key: "separator"
    }), renderAddButton]);
  };
  return ComboBoxMenu;
}(React.Component), _ComboBoxMenu.__KONTUR_REACT_UI__ = 'ComboBoxMenu', _ComboBoxMenu.displayName = 'ComboBoxMenu', _ComboBoxMenu.defaultProps = {
  repeatRequest: function repeatRequest() {
    return undefined;
  },
  requestStatus: ComboBoxRequestStatus.Unknown
}, _ComboBoxMenu)) || _class);
function isSimpleItem(item) {
  return !isFunction(item) && ! /*#__PURE__*/React.isValidElement(item);
}