'use strict';

const React = require("react");

module.exports = function childrenWithProps(children, extraProps) {
	if (React.Children.count(children) > 1) {
		return React.Children.map(children, (child) => {
			if (React.isValidElement(child)) {
				if (typeof extraProps === "function") {
					return React.cloneElement(child, extraProps(child));
				} else {
					return React.cloneElement(child, extraProps);
				}
			} else {
				return child;
			}
		});
	} else if (React.isValidElement(children)) {
		/* FIXME: The above may possibly break, considering that the React docs claim that .children is an opaque data structure?
                       (ref. https://reactjs.org/docs/react-api.html#reactchildren ) */
		if (typeof extraProps === "function") {
			return React.cloneElement(children, extraProps(children));
		} else {
			return React.cloneElement(children, extraProps);
		}
	} else {
		return children;
	}
};
