import { _flattenChildren } from "./createElement";
import { cloneElement } from "./cloneElement";
export const Children = {
    only(children) {
    //only方法接受的参数只能是一个对象，不能是多个对象（数组）。
        if (Array.isArray(children)) {
            children = children[0];
        }
        if (children && children.vtype) {
            return children;
        }
        throw new Error("expect only one child");
    },
    count(children) {
        return _flattenChildren(children, false).length;
    },
    map(children, callback, context) {
        var ret = [];
        _flattenChildren(children, false).forEach(function(el, index) {
            var old = el;
            el = callback.call(context, el, index);
            if (el === null) {
                return;
            }
            if (el.vtype) {
                //如果返回的el等于old,还需要使用原来的key, _prefix
                if(old){
                    let oldKey = old.key,
                        oldFix = old._prefix,
                        key =
                 oldKey == null
                     ? oldFix
                     : oldFix.indexOf(":") === "-1"
                         ? ".$" + oldKey
                         : (oldFix + "$" + oldKey).replace(/\d+\$/, "$");
                }
                

                ret.push(cloneElement(el, { key }));
            } else if (el.type) {
                ret.push(Object.assign({}, el));
            } else {
                ret.push(el);
            }
        });
        return ret;
    },
    forEach(children, callback, context) {
        _flattenChildren(children, false).forEach(callback, context);
    },

    toArray: function(children) {
        return _flattenChildren(children, false);
    }
};
