import React, { Component } from 'react';
export default function applyDefaults(defaultProps, ComponentHolder) {
    if (isStateless(ComponentHolder)) {
        return function ProjectWrapperComponent(props) {
            const mergedProps = mergeProps(defaultProps, props);
            return <ComponentHolder {...mergedProps}/>;
        };
    }
    else {
        return class ProjectWrapperComponent extends Component {
            constructor() {
                super(...arguments);
                this.setValue = (v) => {
                    if (this.comp.setValue) {
                        this.comp.setValue(v);
                    }
                };
                this.extratRef = (comp) => (this.comp = comp);
            }
            render() {
                const mergedProps = mergeProps(defaultProps, this.props);
                return <ComponentHolder ref={this.extratRef} {...mergedProps}/>;
            }
        };
    }
}
export function mergeProps(defaultProps, newProps) {
    if (!defaultProps) {
        return newProps;
    }
    const props = Object.assign({}, defaultProps);
    Object.keys(newProps).forEach(key => {
        const val = newProps[key];
        if (props[key]) {
            if (Array.isArray(props[key])) {
                if (Array.isArray(val)) {
                    props[key] = [...props[key], ...val];
                }
                else {
                    props[key] = [...props[key], val];
                }
            }
            else if (typeof props[key] === 'object' &&
                !React.isValidElement(props[key])) {
                if (typeof val === 'object') {
                    props[key] = Object.assign({}, props[key], val);
                }
                else if (typeof val === 'number') {
                    props[key] = [props[key], val];
                }
            }
            else {
                props[key] = val;
            }
        }
        else {
            props[key] = val;
        }
    });
    return props;
}
export function isStateless(Component) {
    return !Component.prototype.render;
}
//# sourceMappingURL=applyDefaults.js.map