var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import React from 'react';
import { isFunction, isNullable } from '../../lib/utils.js';
import { locale } from '../../lib/locale/decorators.js';
import { Menu, MenuDataTids } from '../Menu/index.js';
import { isMenuItem, MenuItem } from '../../components/MenuItem/index.js';
import { Spinner } from '../../components/Spinner/index.js';
import { MenuSeparator } from '../../components/MenuSeparator/index.js';
import { createPropsGetter } from '../../lib/createPropsGetter.js';
import { MenuMessage } from '../MenuMessage/index.js';
import { ThemeContext } from '../../lib/theming/ThemeContext.js';
import { MenuFooter } from '../../components/MenuFooter/index.js';
import { ComboBoxRequestStatus } from './CustomComboBoxTypes.js';
import { CustomComboBoxLocaleHelper } from './locale/index.js';
export var ComboBoxMenuDataTids = {
    loading: 'ComboBoxMenu__loading',
    failed: 'ComboBoxMenu__failed',
    notFound: 'ComboBoxMenu__notFound',
    items: 'ComboBoxMenu__items',
    item: 'ComboBoxMenu__item',
};
var ComboBoxMenu = /** @class */ (function (_super) {
    __extends(ComboBoxMenu, _super);
    function ComboBoxMenu() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.getProps = createPropsGetter(ComboBoxMenu_1.defaultProps);
        _this.renderItem = function (item, index) {
            // NOTE this is undesireable feature, better
            // to remove it from further versions
            var _a = _this.props, renderItem = _a.renderItem, onValueChange = _a.onValueChange, itemWrapper = _a.itemWrapper;
            if (!isSimpleItem(item)) {
                var element_1 = isFunction(item) ? item() : item;
                var props = Object.assign({
                    key: index,
                    onClick: function () { return onValueChange(element_1.props); },
                }, element_1.props);
                return React.cloneElement(element_1, props);
            }
            return (React.createElement(MenuItem, { component: itemWrapper === null || itemWrapper === void 0 ? void 0 : itemWrapper(item), "data-tid": ComboBoxMenuDataTids.item, onClick: function () { return onValueChange(item); }, key: index, size: _this.props.size, isMobile: _this.props.isMobile }, function (state) { return renderItem(item, state); }));
        };
        return _this;
    }
    ComboBoxMenu_1 = ComboBoxMenu;
    ComboBoxMenu.prototype.render = function () {
        var _this = this;
        return (React.createElement(ThemeContext.Consumer, null, function (theme) {
            _this.theme = theme;
            return React.createElement(ThemeContext.Provider, { value: _this.theme }, _this.renderMain());
        }));
    };
    ComboBoxMenu.prototype.renderMain = function () {
        var _a = this.props, opened = _a.opened, items = _a.items, totalCount = _a.totalCount, loading = _a.loading, refMenu = _a.refMenu, _b = _a.renderNotFound, renderNotFound = _b === void 0 ? function () { return notFound; } : _b, renderTotalCount = _a.renderTotalCount, maxMenuHeight = _a.maxMenuHeight, isMobile = _a.isMobile;
        var requestStatus = this.getProps().requestStatus;
        var _c = this.locale, notFound = _c.notFound, errorNetworkButton = _c.errorNetworkButton, errorNetworkMessage = _c.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 (React.createElement(Menu, { maxHeight: maxHeight, ref: refMenu, hasMargin: this.props.hasMargin, disableScrollContainer: isMobile, id: this.props.menuId, "data-tid": "".concat(ComboBoxMenuDataTids.loading, " ").concat(MenuDataTids.root) },
                React.createElement(MenuMessage, { size: this.props.size, as: "div" },
                    React.createElement(Spinner, { type: "mini", dimmed: true }))));
        }
        if (items === null && requestStatus === ComboBoxRequestStatus.Failed) {
            return (React.createElement(Menu, { ref: refMenu, maxHeight: maxHeight, hasMargin: this.props.hasMargin, disableScrollContainer: isMobile, id: this.props.menuId, "data-tid": "".concat(ComboBoxMenuDataTids.failed, " ").concat(MenuDataTids.root) },
                React.createElement(MenuMessage, { size: this.props.size, key: "message" },
                    React.createElement("div", { style: { maxWidth: 300, whiteSpace: 'normal' } }, errorNetworkMessage)),
                React.createElement(MenuItem, { 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 (React.createElement(Menu, { id: this.props.menuId, hasMargin: this.props.hasMargin, maxHeight: maxHeight, ref: refMenu, disableScrollContainer: isMobile }, renderAddButton));
            }
            if (notFoundValue) {
                return (React.createElement(Menu, { id: this.props.menuId, hasMargin: this.props.hasMargin, maxHeight: maxHeight, ref: refMenu, disableScrollContainer: isMobile },
                    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 || renderedItems === void 0 ? void 0 : renderedItems.filter(function (item) {
            return isMenuItem(item);
        });
        var countItems = menuItems === null || menuItems === void 0 ? void 0 : menuItems.length;
        if (countItems && renderTotalCount && totalCount && countItems < totalCount) {
            total = (React.createElement(MenuFooter, { size: this.props.size, key: "total" },
                React.createElement("div", null, renderTotalCount(countItems, totalCount))));
        }
        return (React.createElement(Menu, { id: this.props.menuId, "data-tid": "".concat(ComboBoxMenuDataTids.items, " ").concat(MenuDataTids.root), ref: refMenu, maxHeight: maxHeight, hasMargin: this.props.hasMargin, disableScrollContainer: isMobile },
            renderedItems,
            total,
            renderAddButton && [React.createElement(MenuSeparator, { key: "separator" }), renderAddButton]));
    };
    var ComboBoxMenu_1;
    ComboBoxMenu.__KONTUR_REACT_UI__ = 'ComboBoxMenu';
    ComboBoxMenu.displayName = 'ComboBoxMenu';
    ComboBoxMenu.defaultProps = {
        repeatRequest: function () { return undefined; },
        requestStatus: ComboBoxRequestStatus.Unknown,
    };
    ComboBoxMenu = ComboBoxMenu_1 = __decorate([
        locale('ComboBox', CustomComboBoxLocaleHelper)
    ], ComboBoxMenu);
    return ComboBoxMenu;
}(React.Component));
export { ComboBoxMenu };
function isSimpleItem(item) {
    return !isFunction(item) && !React.isValidElement(item);
}
//# sourceMappingURL=ComboBoxMenu.js.map