import React from 'react';

import AuthUserContext from './context';
import { withFirebase } from '../Firebase';

const needsEmailVerification = authUser =>
  authUser &&
  !authUser.emailVerified &&
  authUser.providerData
    .map(provider => provider.providerId)
    .includes('password');

const withEmailVerification = Component => {
  class WithEmailVerification extends React.Component {
    constructor(props) {
      super(props);

      this.state = { isSent: false };
    }

    onSendEmailVerification = () => {
      this.props.firebase
        .doSendEmailVerification()
        .then(() => this.setState({ isSent: true }));
    };

    render() {
      return (
        <AuthUserContext.Consumer>
          {authUser =>
            needsEmailVerification(authUser) ? (
              <div>
                {this.state.isSent ? (
                  <p>
                    E-Mail confirmation sent: Check you E-Mails (Spam
                    folder included) for a confirmation E-Mail.
                    Refresh this page once you confirmed your E-Mail.
                  </p>
                ) : (
                  <p>
                    Verify your E-Mail: Check you E-Mails (Spam folder
                    included) for a confirmation E-Mail or send
                    another confirmation E-Mail.
                  </p>
                )}

                <button
                  type="button"
                  onClick={this.onSendEmailVerification}
                  disabled={this.state.isSent}
                >
                  Send confirmation E-Mail
                </button>
              </div>
            ) : (
              <Component {...this.props} />
            )
          }
        </AuthUserContext.Consumer>
      );
    }
  }

  return withFirebase(WithEmailVerification);
};

export default withEmailVerification;

@@ -879,18 +879,17 @@
        E
--M
+m
 ail conf
@@ -904,36 +904,36 @@
  sent: Check you
- E-M
+r em
 ails (Spam%0A     
@@ -975,35 +975,34 @@
  a confirmation 
-E-M
+em
 ail.%0A           
@@ -1052,19 +1052,18 @@
 ed your 
-E-M
+em
 ail.%0A   
@@ -1158,19 +1158,18 @@
 fy your 
-E-M
+em
 ail: Che
@@ -1178,16 +1178,15 @@
  you
- E-M
+r em
 ail
-s
  (Sp
@@ -1244,19 +1244,18 @@
 rmation 
-E-M
+em
 ail or s
@@ -1299,19 +1299,18 @@
 rmation 
-E-M
+em
 ail.%0A   

Fix typos in message.