
import h from 'snabbdom/h'
import extend from 'extend'

const svgTags = [
    'svg', 'circle', 'ellipse', 'line', 'polygon', 'polyline', 'rect', 'g', 'path', 'text'
]

const sanitizeProps = (props) => {

  props = props === null ? {} : props

  Object.keys(props).map((prop) => {

    const keysRiver = prop.split('-').reverse()

    if (keysRiver.length > 1) {

      let newObject = keysRiver.reduce(
        (object, key) => ({ [key]: object }),
        props[prop]
      )
      extend(true, props, newObject)
      delete props[prop]

    } else if (!(['class', 'props', 'attrs', 'style', 'on', 'hook', 'key'].indexOf(prop) > -1)) {

      extend(true, props, { props: { [prop]: props[prop] } })
      delete props[prop]

    }

    return { toto: true }

  })

  return props

}

const sanitizeChilds = (children) => {

  return children.length === 1 && typeof children[0] === 'string' ?
    children[0] : children

}

const considerSVG = (props, type) => {

    if(svgTags.indexOf(type) > -1) {
        const attrs = Object.assign({}, props.props, props.props.className ? { class: props.props.className } : undefined);
        let p = Object.assign({}, props, { attrs: attrs })
        if(p.attrs.className) { delete p.attrs.className; }
        delete p.props;
        return p;
    }
    return props;
        
}

export const createElement = (type, props, ...children) => {

  return (typeof type === 'function') ?
    type(props, children) :
    h(type, considerSVG(sanitizeProps(props), type), sanitizeChilds(children))

}

export default {
  createElement
}

@@ -68,18 +68,16 @@
 ags = %5B%0A
-  
   'svg',
@@ -957,22 +957,21 @@
 ) =%3E %7B%0A%0A
-  
   if
+ 
 (svgTags
@@ -993,20 +993,17 @@
 %3E -1) %7B%0A
-    
+%0A
     cons
@@ -1112,22 +1112,18 @@
 defined)
-;%0A    
+%0A%0A
     let 
@@ -1173,18 +1173,16 @@
  %7D)%0A
-    
+%0A
     if
+ 
 (p.a
@@ -1198,16 +1198,23 @@
 sName) %7B
+%0A%0A     
  delete 
@@ -1234,16 +1234,17 @@
 Name
-; %7D
+%0A
 %0A    
+%7D%0A%0A
     
@@ -1253,30 +1253,25 @@
 lete p.props
-;%0A    
+%0A
     return p
@@ -1274,18 +1274,14 @@
 rn p
-;%0A    %7D%0A  
+%0A%0A  %7D%0A
   re
@@ -1294,18 +1294,9 @@
 rops
-;%0A        
+%0A
 %0A%7D%0A%0A

Update jvanbruegge additions according to XO rules