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 ConfigProvider from '../config-provider';
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import cls from 'classnames';
import hoistNonReactStatics from 'hoist-non-react-statics';
import { Select as NextSelect } from '@alifd/next';
import { getTheme } from '../utils/getTheme';
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-', popupClassName, className, mode, size } = this.props;
        const theme = getTheme(this.context, this.props);
        // 多选 模式
        if (mode === 'multiple') {
            const _a = this.props, { prefix = 'next-', className, dataSource } = _a, otherProps = __rest(_a, ["prefix", "className", "dataSource"]);
            if (size === 'xs') {
                return (React.createElement(NextSelect, Object.assign({}, omitProps(otherProps, ['size']), { mode: "multiple", prefix: prefix, adjustTagSize: true, className: cls(className, {
                        [`${prefix}xs`]: size === 'xs',
                        [`${prefix}white`]: theme === 'white',
                        [`${prefix}grey`]: theme === 'grey',
                    }), popupClassName: cls(popupClassName, {
                        [`${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", adjustTagSize: true, prefix: prefix, size: size, className: cls(className, {
                    [`${prefix}white`]: theme === 'white',
                    [`${prefix}grey`]: theme === 'grey',
                }), popupClassName: cls(popupClassName, {
                    [`${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 } = _b, otherProps = __rest(_b, ["prefix", "className"]);
            return (React.createElement(NextSelect, Object.assign({}, omitProps(otherProps, ['size']), { prefix: prefix, popupClassName: cls(popupClassName, {
                    [`${prefix}white`]: theme === 'white',
                    [`${prefix}grey`]: theme === 'grey',
                }), 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, popupClassName: cls(popupClassName, {
                [`${prefix}white`]: theme === 'white',
                [`${prefix}grey`]: theme === 'grey',
            }), 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;
Select.contextTypes = {
    theme: PropTypes.string,
};
hoistNonReactStatics(Select, NextSelect);
export default ConfigProvider.config(Select);
