import _toConsumableArray from 'babel-runtime/helpers/toConsumableArray';
import React from 'react';
import assign from 'object-assign';
import ColumnGroup from './ColumnGroup';
export function flatArray() {
    var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
    var childrenName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'children';

    var result = [];
    var loop = function loop(array) {
        array.forEach(function (item) {
            var newItem = assign({}, item);
            delete newItem[childrenName];
            result.push(newItem);
            if (item[childrenName] && item[childrenName].length > 0) {
                loop(item[childrenName]);
            }
        });
    };
    loop(data);
    return result;
}
export function treeMap(tree, mapper) {
    var childrenName = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children';

    return tree.map(function (node, index) {
        var extra = {};
        if (node[childrenName]) {
            extra[childrenName] = treeMap(node[childrenName], mapper, childrenName);
        }
        return assign({}, mapper(node, index), extra);
    });
}
export function flatFilter(tree, callback) {
    return tree.reduce(function (acc, node) {
        if (callback(node)) {
            acc.push(node);
        }
        if (node.children) {
            var children = flatFilter(node.children, callback);
            acc.push.apply(acc, _toConsumableArray(children));
        }
        return acc;
    }, []);
}
export function normalizeColumns(elements) {
    var columns = [];
    React.Children.forEach(elements, function (element) {
        if (!React.isValidElement(element)) {
            return;
        }
        var column = assign({}, element.props);
        if (element.key) {
            column.key = element.key;
        }
        if (element.type === ColumnGroup) {
            column.children = normalizeColumns(column.children);
        }
        columns.push(column);
    });
    return columns;
}