import { __assign, __awaiter, __extends, __generator, __rest } from "tslib";
import { Icon, Select } from '@alicloud/console-components';
import classnames from 'classnames';
import React from 'react';
var Option = Select.Option, OptionGroup = Select.OptionGroup;
var LPSelect = /** @class */ (function (_super) {
    __extends(LPSelect, _super);
    function LPSelect(props) {
        var _this = _super.call(this, props) || this;
        _this.handleMore = function (e, dispalyLaoding) { return __awaiter(_this, void 0, void 0, function () {
            var e_1;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        e.stopPropagation();
                        if (dispalyLaoding)
                            return [2 /*return*/];
                        this.setState({ isLoading: true });
                        if (!this.props.loadMore) return [3 /*break*/, 4];
                        _a.label = 1;
                    case 1:
                        _a.trys.push([1, 3, , 4]);
                        return [4 /*yield*/, this.props.loadMore()];
                    case 2:
                        _a.sent();
                        return [3 /*break*/, 4];
                    case 3:
                        e_1 = _a.sent();
                        console.error(e_1);
                        return [3 /*break*/, 4];
                    case 4:
                        this.setState({ isLoading: false });
                        return [2 /*return*/];
                }
            });
        }); };
        _this.returnMyItem = function () {
            var items = _this.props.dataSource && _this.props.dataSource.map(function (v) {
                var value = v.value, disabled = v.disabled, rest = __rest(v, ["value", "disabled"]);
                return React.createElement(Option, __assign({ value: value, key: value, disabled: disabled }, rest), v.label);
            });
            var isShow = _this.props.showLoadMore !== undefined ? _this.props.showLoadMore : (_this.props.loadMore !== undefined);
            if (isShow) {
                var dispalyLaoding_1 = false;
                if (_this.props.isLoading === undefined) {
                    dispalyLaoding_1 = _this.state.isLoading;
                }
                else {
                    dispalyLaoding_1 = _this.props.isLoading;
                }
                items && items.push(React.createElement(Option, { value: 'lp-selectLoadMoreIndex', key: 'lp-selectLoadMoreIndex' + Math.random(), disabled: dispalyLaoding_1 },
                    React.createElement("div", { onClick: function (e) { return _this.handleMore(e, dispalyLaoding_1); }, className: "lp-select-loadMore" },
                        React.createElement(Icon, { type: "loading", size: "small", style: {
                                display: dispalyLaoding_1 ? 'inline-block' : 'none',
                                right: 14,
                                position: 'absolute'
                            } }),
                        "\u52A0\u8F7D\u66F4\u591A")));
            }
            return items;
        };
        _this.state = {
            isLoading: false,
        };
        return _this;
    }
    LPSelect.prototype.render = function () {
        var _a = this.props, width = _a.width, className = _a.className, isLoading = _a.isLoading, loadMore = _a.loadMore, showLoadMore = _a.showLoadMore, rest = __rest(_a, ["width", "className", "isLoading", "loadMore", "showLoadMore"]);
        var loadState = isLoading ? isLoading : this.state.isLoading;
        if (loadState) {
            return (React.createElement(Select, __assign({ style: { width: width } }, rest, { visible: true, className: classnames('lp-select', className) }), this.props.children || this.returnMyItem()));
        }
        else {
            return (React.createElement(Select, __assign({ style: { width: width } }, rest, { className: classnames('lp-select', className) }), this.props.children || this.returnMyItem()));
        }
    };
    return LPSelect;
}(React.Component));
LPSelect.Option = Option;
LPSelect.OptionGroup = OptionGroup;
export default LPSelect;
