import React, {PropTypes} from 'react'

const callbacks = []

function addScript (src, cb) {
  if (callbacks.length === 0) {
    callbacks.push(cb)
    var s = document.createElement('script')
    s.setAttribute('src', src)
    s.onload = () => callbacks.forEach((cb) => cb())
    document.body.appendChild(s)
  } else {
    callbacks.push(cb)
  }
}

class TweetEmbed extends React.Component {
  componentDidMount () {
    const renderTweet = () => {
      window.twttr.ready().then(({ widgets }) => {
        const { options, onTweetLoadSuccess, onTweetLoadError } = this.props
        widgets
          .createTweetEmbed(this.props.id, this._div, options)
          .then(onTweetLoadSuccess)
          .catch(onTweetLoadError)
      })
    }

    if (!window.twttr) {
      const isLocal = window.location.protocol.indexOf('file') >= 0
      const protocol = isLocal ? this.props.protocol : ''

      addScript(`${protocol}//platform.twitter.com/widgets.js`, renderTweet)
    } else {
      renderTweet()
    }
  }

  render () {
    return <div className={this.props.className} ref={(c) => {
      this._div = c
    }} />
  }
}

TweetEmbed.propTypes = {
  id: PropTypes.string,
  options: PropTypes.object,
  protocol: PropTypes.string,
  onTweetLoadSuccess: PropTypes.func,
  onTweetLoadError: PropTypes.func
}

TweetEmbed.defaultProps = {
  protocol: 'https:',
  options: {},
  className: null
}

export default TweetEmbed

@@ -1305,16 +1305,47 @@
 pes.func
+,%0A  className: PropTypes.string
 %0A%7D%0A%0ATwee

update PropTypes