'use strict';

const AccountManager = require('../account-manager');
const {handleResponseData} = require('../utils');

module.exports = class CheckEmail {
  constructor({name, retryStep} = {}) {
    this.name = name;
    this.retryStep = retryStep;
  }

  start(data) {
    return AccountManager.checkEmail(data)
    .then(resp => Promise.all([
      Promise.resolve(resp.statusCode),
      handleResponseData(resp),
    ]))
    .then(([status, raw]) => {
      const json = status === 200 ? {} : JSON.parse(raw);
      switch (status) {
        case 200:
          return {
            email: data.email,
            invalid: false,
            exists: false,
            hasPassword: false,
            reason: null,
          };
        case 403:
        case 404:
        case 409:
          if (json.email_invalid) {
            const err = new Error(json.fail_reason);
            err.data = {
              email: data.email,
              invalid: json.email_invalid,
              exists: json.account_exists,
              hasPassword: json.has_password,
              reason: json.fail_reason,
            };
            throw err;
          } else {
            return {
              email: data.email,
              invalid: json.email_invalid,
              exists: json.account_exists,
              hasPassword: json.has_password,
              reason: json.fail_reason,
            };
          }
        case 500:
          throw new Error('Server error while checking email');
      }
      return undefined;
    });
  }
};

@@ -1426,16 +1426,18 @@
 ase 500:
+ %7B
 %0A       
@@ -1439,21 +1439,27 @@
         
-throw
+const err =
  new Err
@@ -1499,16 +1499,114 @@
 mail');%0A
+          err.data = %7B%0A            email: data.email,%0A          %7D;%0A          throw err;%0A        %7D%0A
       %7D%0A

Normalize check email step errors