import React from 'react';
import isPlainObject from 'lodash.isplainobject';
import { convertToReact } from '@yummies/core';

import mergeWithProps from './mergeWithProps';
import convertToReactType from './convertToReactType';

/**
 * 'inherit' from React
 */
const Yummies = Object.create(React);

Yummies.Component = class extends React.Component {
    static __yummies__ = true;
};

/**
 * .createElement({ block: 'test' })
 * .createElement(class extends Yummies.Component {})
 * .createElement('div', { foo: 'bar' }, [ … ])
 */
Yummies.createElement = function(type, props, ...children) {
    if (isPlainObject(type)) {
        return convertToReact(type);
    }

    const patchedChildren = convertToReact(children);
    const patchedType = convertToReactType(type);

    return React.createElement(patchedType, props, ...patchedChildren);
};

/**
 * .createFactory(class extends Yummies.Component {})
 * .createFactory('div')
 */
Yummies.createFactory = function(type) {
    const patchedType = convertToReactType(type);

    return function(props, ...children) {
        const patchedChildren = convertToReact(children);

        return React.createFactory(patchedType)(props, ...patchedChildren);
    };
};

/**
 * Prepare class before the factory.
 */
Yummies.yummify = function(Base) {
    const originalRender = Base.prototype.render;

    Base.prototype.render = function() {
        const result = originalRender.call(this);

        if (!isPlainObject(result) || React.isValidElement(result)) {
            return result;
        }

        mergeWithProps(result, this.props);

        return convertToReact(result);
    };

    return Base;
};

export default Yummies;

@@ -1299,96 +1299,59 @@
     
-const originalRender = Base.prototype.render;%0A%0A    Base.prototype.render = function
+return class extends Base %7B%0A        render
 () %7B%0A
+    
     
@@ -1373,32 +1373,21 @@
 t = 
-originalRender.call(this
+super.render(
 );%0A%0A
@@ -1390,24 +1390,28 @@
 );%0A%0A        
+  
+  
 if (!isPlain
@@ -1468,24 +1468,28 @@
             
+    
 return resul
@@ -1495,27 +1495,35 @@
 lt;%0A        
+   
+ 
 %7D%0A%0A
+    
         merg
@@ -1551,24 +1551,28 @@
 is.props);%0A%0A
+    
         retu
@@ -1606,27 +1606,19 @@
     
-%7D;%0A%0A    return Base
+    %7D%0A    %7D
 ;%0A%7D;

revert `yummify` to not change original class