import { __assign, __extends, __rest } from "tslib";
import { CascaderSelect, Message } from '@alicloud/console-components';
import classnames from 'classnames';
import React, { Component } from 'react';
;
var LPCascaderSelect = /** @class */ (function (_super) {
    __extends(LPCascaderSelect, _super);
    function LPCascaderSelect(props) {
        var _this = _super.call(this, props) || this;
        _this.componentWillReceiveProps = function (nextProps) {
            _this.setState({ dataSource: nextProps.dataSource });
        };
        _this.handleChange = function (value, data, extra) {
            _this.props.handleChange && _this.props.handleChange(value, data, extra);
        };
        _this.addDataSource = function (data, targetValue, needAddData) {
            for (var _i = 0, data_1 = data; _i < data_1.length; _i++) {
                var one = data_1[_i];
                if (one.value === targetValue) {
                    if (!one.children) {
                        one['children'] = [];
                    }
                    one['children'] = needAddData;
                    return true;
                }
                else if (one.children && one.children.length > 0) {
                    if (_this.addDataSource(one.children, targetValue, needAddData)) {
                        return;
                    }
                }
            }
            return;
        };
        _this.handleLoadData = function (data) {
            if (data.children && data.children.length > 0) {
                //说明是有数据节点不做处理
                return new Promise(function (resolve) {
                    resolve();
                });
            }
            else if (_this.props.fetchData) {
                return _this.props.fetchData(data).then(function (result) {
                    if (result.length > 0) {
                        var tempData = _this.state.dataSource.splice(0);
                        _this.addDataSource(tempData, data.value, result);
                        _this.setState({ dataSource: tempData });
                    }
                    else {
                        Message.notice('抱歉没有数据');
                    }
                });
            }
            else {
                return new Promise(function (resolve) {
                    resolve();
                });
            }
        };
        _this.state = {
            dataSource: _this.props.dataSource ? _this.props.dataSource : []
        };
        return _this;
    }
    LPCascaderSelect.prototype.render = function () {
        var _a = this.props, width = _a.width, className = _a.className, triggerType = _a.triggerType, loadEmtyTip = _a.loadEmtyTip, handleChange = _a.handleChange, fetchData = _a.fetchData, dataSource = _a.dataSource, rest = __rest(_a, ["width", "className", "triggerType", "loadEmtyTip", "handleChange", "fetchData", "dataSource"]);
        var style = {};
        if (width) {
            style = { width: width };
        }
        return React.createElement(CascaderSelect, __assign({ showSearch: true, className: classnames('lp-cascader-select', className), expandTriggerType: triggerType, dataSource: this.state.dataSource, onChange: this.handleChange, loadData: fetchData ? this.handleLoadData : undefined, popupClassName: "lp-cascader-select-over", listClassName: "lp-list-Item", style: style, popupStyle: style }, rest));
    };
    return LPCascaderSelect;
}(Component));
export default LPCascaderSelect;
LPCascaderSelect.defaultProps = {
    showSearch: true,
    triggerType: 'click',
    loadEmtyTip: '抱歉没有数据'
};
