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 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
// import { Select } from '@alifd/next';
import Checkbox from '../checkbox';
import React, { Component } from 'react';
import cls from 'classnames';
import hoistNonReactStatics from 'hoist-non-react-statics';
import { Select as NextSelect } from '@alifd/next';
import { omitProps } from '../utils/object';
const { Option } = NextSelect;
class Select extends Component {
    constructor(props) {
        super(props);
        this.onSelectChange = (value, actionType, item) => {
            const { onChange } = this.props;
            this.setState({
                renderValue: value,
            });
            onChange && onChange(value, actionType, item);
        };
        this.getSelected = item => {
            const { renderValue } = this.state;
            if (Array.isArray(renderValue)) {
                for (const value of renderValue) {
                    if (typeof value === 'string' && typeof item.value === 'number' ||
                        typeof value === 'number' && typeof item.value === 'string') {
                        if (parseInt(String(value), 10) === parseInt(String(item.value), 10)) {
                            return true;
                        }
                    }
                    if (value === item.value) {
                        return true;
                    }
                }
            }
            else if (renderValue === item.value) {
                return true;
            }
            return false;
        };
        this.checkedItemRender = item => {
            const selected = this.getSelected(item);
            return (React.createElement(Option, { value: item.value },
                React.createElement(Checkbox, { checked: selected, disabled: item.disabled }),
                item.label));
        };
        this.state = {
            inited: false,
            renderValue: null,
        };
    }
    static getDerivedStateFromProps(nextProps, prevState) {
        if (!prevState.inited) {
            return {
                inited: true,
                renderValue: nextProps.value || nextProps.defaultValue,
            };
        }
        if (nextProps.value) {
            return {
                renderValue: nextProps.value,
            };
        }
        return null;
    }
    render() {
        const { prefix = 'next-', className, mode, size, theme = 'white' } = this.props;
        // 多选 模式
        if (mode === 'multiple') {
            const _a = this.props, { prefix = 'next-', className, dataSource, theme = 'white' } = _a, otherProps = __rest(_a, ["prefix", "className", "dataSource", "theme"]);
            if (size === 'xs') {
                return (React.createElement(NextSelect, Object.assign({}, omitProps(otherProps, ['size']), { mode: "multiple", prefix: prefix, className: cls(className, {
                        [`${prefix}xs`]: size === 'xs',
                        [`${prefix}white`]: theme === 'white',
                        [`${prefix}grey`]: theme === 'grey',
                    }), dataSource: dataSource, itemRender: this.checkedItemRender, onChange: this.onSelectChange })));
            }
            return (React.createElement(NextSelect, Object.assign({}, otherProps, { mode: "multiple", prefix: prefix, size: size, className: cls(className, {
                    [`${prefix}white`]: theme === 'white',
                    [`${prefix}grey`]: theme === 'grey',
                }), dataSource: dataSource, itemRender: this.checkedItemRender, onChange: this.onSelectChange })));
        }
        if (size === 'xs') {
            const _b = this.props, { prefix = 'next-', className, theme = 'white' } = _b, otherProps = __rest(_b, ["prefix", "className", "theme"]);
            return (React.createElement(NextSelect, Object.assign({}, omitProps(otherProps, ['size']), { prefix: prefix, className: cls(className, {
                    [`${prefix}xs`]: size === 'xs',
                    [`${prefix}white`]: theme === 'white',
                    [`${prefix}grey`]: theme === 'grey',
                }) })));
        }
        return React.createElement(NextSelect, Object.assign({}, this.props, { size: size, prefix: prefix, className: cls(className, {
                [`${prefix}white`]: theme === 'white',
                [`${prefix}grey`]: theme === 'grey',
            }) }));
    }
}
Select.Option = NextSelect.Option;
Select.AutoComplete = NextSelect.AutoComplete;
Select.OptionGroup = NextSelect.OptionGroup;
hoistNonReactStatics(Select, NextSelect);
export default Select;
