import assign from 'object-assign';
import pascalCase from 'pascal-case';

const defaults = {
  React: window.React,
  ReactDOM: window.ReactDOM,
};

function syncEvent(node, eventName, newEventHandler) {
  const eventNameLc = eventName[0].toLowerCase() + eventName.substring(1);
  const eventStore = node.__events || (node.__events = {});
  const oldEventHandler = eventStore[eventNameLc];

  // Remove old listener so they don't double up.
  if (oldEventHandler) {
    node.removeEventListener(eventNameLc, oldEventHandler);
  }

  // Bind new listener.
  if (newEventHandler) {
    node.addEventListener(eventNameLc, eventStore[eventNameLc] = function handler(e) {
      newEventHandler.call(this, e);
    });
  }
}

export default function (CustomElement, opts) {
  opts = assign(defaults, opts);
  const tagName = (new CustomElement()).tagName;
  const displayName = pascalCase(tagName);
  const { React, ReactDOM } = opts;

  if (!React || !ReactDOM) {
    throw new Error('React and ReactDOM must be globally available or passed via opts.');
  }

  return class extends React.Component {
    static get displayName() {
      return displayName;
    }
    static get propTypes() {
      return {
        children: React.PropTypes.any,
        style: React.PropTypes.any,
      };
    }
    componentDidMount() {
      this.componentWillReceiveProps(this.props);
    }
    componentWillReceiveProps(props) {
      const node = ReactDOM.findDOMNode(this);
      for (var name in props) {
        if (name === 'children' || name === 'style') {
          continue;
        }

        if (name.indexOf('on') === 0) {
          syncEvent(node, name.substring(2), props[name]);
        } else {
          node[name] = props[name];
        }
      }
    }
    render() {
      return React.createElement(tagName, { style: this.props.style }, this.props.children);
    }
  };
}

@@ -1463,31 +1463,42 @@
     
-for (var name in props)
+Object.keys(props).forEach(name =%3E
  %7B%0A 
@@ -1565,16 +1565,14 @@
     
-continue
+return
 ;%0A  
@@ -1745,24 +1745,26 @@
    %7D%0A      %7D
+);
 %0A    %7D%0A    r

Revert "feat(proto): expose web component proto props onto react component"