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 ConfigProvider from '../config-provider';
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import Tree from '../tree';
import cls from 'classnames';
import { Transfer as NextTransfer } from '@alifd/next';
import { getTheme } from '../utils/getTheme';
const TreeNode = Tree.Node;
class Transfer extends Component {
    constructor(props) {
        super(props);
        this.state = {
            selected: [],
        };
    }
    render() {
        const _a = this.props, { prefix = 'next-', className, mode, showTreeSelect, showTable, dataSource } = _a, otherProps = __rest(_a, ["prefix", "className", "mode", "showTreeSelect", "showTable", "dataSource"]);
        const theme = getTheme(this.context, this.props);
        const { selected } = this.state;
        const handleChange = value => {
            this.setState({ selected: value });
        };
        const transferDataSource = [];
        const flatten = (list = []) => {
            list.forEach(item => {
                transferDataSource.push(item);
                flatten(item.children);
            });
        };
        // 处理tree的datasource
        const getTreeDataSource = (dataSource = [], value) => {
            return dataSource.map((_a) => {
                var { children } = _a, props = __rest(_a, ["children"]);
                return (React.createElement(TreeNode, Object.assign({}, props, { disabled: props.disabled || value.includes(props.value), key: props.value }), getTreeDataSource(children, value)));
            });
        };
        // 带tree的穿梭框
        if (showTreeSelect) {
            flatten(dataSource);
            return (React.createElement(Transfer, Object.assign({}, otherProps, { theme: theme, mode: mode, dataSource: transferDataSource, onChange: handleChange, className: cls(className, {
                    [`${prefix}transfer-simple`]: mode === 'simple',
                    [`${prefix}transfer-tree`]: true,
                }) }), ({ position, onChange, value }) => {
                if (position === 'left') {
                    return (React.createElement(Tree, { checkable: true, editable: true, style: { padding: '10px' }, checkedKeys: value, onCheck: (keys, extra) => {
                            const newValues = extra.checkedNodes.map(item => item.props.value);
                            onChange(position, newValues);
                        } }, getTreeDataSource(dataSource, selected)));
                }
            }));
        }
        return (React.createElement(NextTransfer, Object.assign({}, this.props, { listClassName: cls(className, {
                [`${prefix}white`]: theme === 'white',
                [`${prefix}grey`]: theme === 'grey',
            }), className: cls(className, {
                [`${prefix}white`]: theme === 'white',
                [`${prefix}grey`]: theme === 'grey',
                [`${prefix}transfer-simple`]: mode === 'simple',
                [`${prefix}transfer-table`]: showTable,
            }) })));
    }
}
Transfer.contextTypes = {
    theme: PropTypes.string,
};
export default ConfigProvider.config(Transfer);
