'use strict'

import R from 'ramda';
import React, {PropTypes} from 'react';
import Registry from './registry';
import NotifyObservers from './components/core/NotifyObservers.react';


export default function render(component, path=[]) {
    if (R.contains(R.type(component), ['String', 'Number', 'Null'])) {
        return component;
    }

    // Create list of child elements
    let children;

    // TODO - Rename component.content to component.children
    const props = R.propOr({}, 'props', component);
    const content = props.content;
    if (!R.has('props', component) || !R.has('content', component.props)) {

        // No children
        children = [];

    } else if (R.contains(R.type(component.props.content), ['String', 'Number', 'Null'])) {

        children = [component.props.content];

    } else {
        // One or multiple objects

        // Recursively render the tree
        const renderChild = (child, i) =>
            render(child, R.append(i, path))

        children = (Array.isArray(content) ? content : [content])
                   .map(renderChild);

    }

    const element = Registry.resolve(component.type, component.namespace);

    const parent = React.createElement(
        element,
        R.omit(['content'], component.props),
        ...children
    );

    return (
        <NotifyObservers id={props.id}>
            {parent}
        </NotifyObservers>
    );
}

render.propTypes = {
    content: PropTypes.object,
    id: PropTypes.string
}

@@ -395,69 +395,8 @@
 n;%0A%0A
-    // TODO - Rename component.content to component.children%0A
     
@@ -447,42 +447,69 @@
 t);%0A
+%0A
     
-const content = props.content;
+// TODO - Rename component.content to component.children
 %0A   
@@ -542,16 +542,24 @@
 nent) %7C%7C
+%0A       
  !R.has(
@@ -585,16 +585,74 @@
 t.props)
+ %7C%7C%0A        typeof component.props.content === 'undefined'
 ) %7B%0A%0A   

handle undefined case