import _extends from 'babel-runtime/helpers/extends';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
var __rest = this && this.__rest || function (s, e) {
    var t = {};
    for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
    }if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
        if (e.indexOf(p[i]) < 0) t[p[i]] = s[p[i]];
    }return t;
};
/* tslint:disable:jsx-no-multiline-js */
import React from 'react';
import RMCPopupCascader from 'rmc-cascader/es/Popup';
import RMCCascader from 'rmc-cascader/es/Cascader';
import RMCMultiPicker from 'rmc-picker/es/MultiPicker';
import RMCPicker from 'rmc-picker/es/Picker';
import treeFilter from 'array-tree-filter';
export function getDefaultProps() {
    var defaultFormat = function defaultFormat(values) {
        return values.join(',');
    };
    return {
        triggerType: 'onClick',
        prefixCls: 'am-picker',
        pickerPrefixCls: 'am-picker-col',
        popupPrefixCls: 'am-picker-popup',
        format: defaultFormat,
        cols: 3,
        cascade: true,
        extra: '请选择',
        okText: '确定',
        dismissText: '取消',
        title: ''
    };
}

var AbstractPicker = function (_React$Component) {
    _inherits(AbstractPicker, _React$Component);

    function AbstractPicker() {
        _classCallCheck(this, AbstractPicker);

        var _this = _possibleConstructorReturn(this, (AbstractPicker.__proto__ || Object.getPrototypeOf(AbstractPicker)).apply(this, arguments));

        _this.getSel = function () {
            var value = _this.props.value || [];
            var treeChildren = void 0;
            if (_this.props.cascade) {
                treeChildren = treeFilter(_this.props.data, function (c, level) {
                    return c.value === value[level];
                });
            } else {
                treeChildren = value.map(function (v, i) {
                    return _this.props.data[i].filter(function (d) {
                        return d.value === v;
                    })[0];
                });
            }
            return _this.props.format && _this.props.format(treeChildren.map(function (v) {
                return v.label;
            }));
        };
        _this.getPickerCol = function () {
            var _this$props = _this.props,
                data = _this$props.data,
                pickerPrefixCls = _this$props.pickerPrefixCls;

            return data.map(function (col, index) {
                return React.createElement(
                    RMCPicker,
                    { key: index, prefixCls: pickerPrefixCls },
                    col.map(function (item) {
                        return React.createElement(
                            RMCPicker.Item,
                            { key: item.value, value: item.value },
                            item.label
                        );
                    })
                );
            });
        };
        return _this;
    }

    _createClass(AbstractPicker, [{
        key: 'render',
        value: function render() {
            var _a = this.props,
                children = _a.children,
                _a$value = _a.value,
                value = _a$value === undefined ? [] : _a$value,
                extra = _a.extra,
                okText = _a.okText,
                itemStyle = _a.itemStyle,
                dismissText = _a.dismissText,
                popupPrefixCls = _a.popupPrefixCls,
                cascade = _a.cascade,
                prefixCls = _a.prefixCls,
                pickerPrefixCls = _a.pickerPrefixCls,
                data = _a.data,
                cols = _a.cols,
                onPickerChange = _a.onPickerChange,
                restProps = __rest(_a, ["children", "value", "extra", "okText", "itemStyle", "dismissText", "popupPrefixCls", "cascade", "prefixCls", "pickerPrefixCls", "data", "cols", "onPickerChange"]);
            var cascader = void 0;
            var popupMoreProps = {};
            if (cascade) {
                cascader = React.createElement(RMCCascader, { prefixCls: prefixCls, pickerPrefixCls: pickerPrefixCls, data: data, cols: cols, onChange: onPickerChange, pickerItemStyle: itemStyle });
            } else {
                cascader = React.createElement(
                    RMCMultiPicker,
                    { prefixCls: prefixCls, pickerItemStyle: itemStyle },
                    this.getPickerCol()
                );
                popupMoreProps = {
                    pickerValueProp: 'selectedValue',
                    pickerValueChangeProp: 'onValueChange'
                };
            }
            return React.createElement(
                RMCPopupCascader,
                _extends({ cascader: cascader }, this.popupProps, restProps, { prefixCls: popupPrefixCls, value: value, dismissText: dismissText, okText: okText }, popupMoreProps),
                React.cloneElement(children, { extra: this.getSel() || extra })
            );
        }
    }]);

    return AbstractPicker;
}(React.Component);

export default AbstractPicker;