'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var asyncToGenerator = require('./asyncToGenerator-58c289d9.js');
var md5 = require('./md5-e1ca5797.js');
require('./_commonjsHelpers-06173234.js');
require('./es6.string.iterator-c2573ffd.js');
require('./_to-object-6de10e57.js');
require('./web.dom.iterable-46657b5c.js');
require('./_library-dd23b178.js');
require('./core.get-iterator-method-f474eacc.js');
require('./_iter-detect-55a5657a.js');

var encrypt = function () {
  var _ref = asyncToGenerator._asyncToGenerator( /*#__PURE__*/asyncToGenerator.regenerator.mark(function _callee(password) {
    var hash;
    return asyncToGenerator.regenerator.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            hash = md5.md5(password);
            return _context.abrupt('return', hash);

          case 2:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, this);
  }));

  return function encrypt(_x) {
    return _ref.apply(this, arguments);
  };
}();

var comparePassword = function () {
  var _ref2 = asyncToGenerator._asyncToGenerator( /*#__PURE__*/asyncToGenerator.regenerator.mark(function _callee2(plainTextPassword, hashedPassword) {
    var hash;
    return asyncToGenerator.regenerator.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            hash = md5.md5(plainTextPassword);
            return _context2.abrupt('return', hash === hashedPassword);

          case 2:
          case 'end':
            return _context2.stop();
        }
      }
    }, _callee2, this);
  }));

  return function comparePassword(_x2, _x3) {
    return _ref2.apply(this, arguments);
  };
}();

var faunadb = require('faunadb'),
    q = faunadb.query;
var secret = 'fnAEG3Bb7AACAC0BiSQFYGBLFUtHjn3QOTKE1iBX'; //

var registerUser = function () {
  var _ref = asyncToGenerator._asyncToGenerator( /*#__PURE__*/asyncToGenerator.regenerator.mark(function _callee(body) {
    var data, client, hashedPassword, result;
    return asyncToGenerator.regenerator.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.prev = 0;
            data = body;

            {
              _context.next = 4;
              break;
            }

          case 4:
            client = new faunadb.Client({ secret: secret });
            _context.next = 7;
            return encrypt(data.Password);

          case 7:
            hashedPassword = _context.sent;

            data.Password = hashedPassword;

            _context.next = 11;
            return client.query(q.Create(q.Collection('Users'), {
              data: data
            }));

          case 11:
            result = _context.sent;

            if (!(!result || !result.data || result.data.Email != data.Email)) {
              _context.next = 14;
              break;
            }

            throw new Error('Failed to register!');

          case 14:
            return _context.abrupt('return', result);

          case 17:
            _context.prev = 17;
            _context.t0 = _context['catch'](0);
            return _context.abrupt('return', { error: _context.t0.message });

          case 20:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, this, [[0, 17]]);
  }));

  return function registerUser(_x) {
    return _ref.apply(this, arguments);
  };
}();

var findUserByEmail = function () {
  var _ref2 = asyncToGenerator._asyncToGenerator( /*#__PURE__*/asyncToGenerator.regenerator.mark(function _callee2(email) {
    var client, result;
    return asyncToGenerator.regenerator.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            _context2.prev = 0;
            client = new faunadb.Client({ secret: secret });
            _context2.next = 4;
            return client.query(q.Get(q.Match(q.Index('Email'), email)));

          case 4:
            result = _context2.sent;
            return _context2.abrupt('return', result ? true : false);

          case 8:
            _context2.prev = 8;
            _context2.t0 = _context2['catch'](0);
            return _context2.abrupt('return', false);

          case 11:
          case 'end':
            return _context2.stop();
        }
      }
    }, _callee2, this, [[0, 8]]);
  }));

  return function findUserByEmail(_x2) {
    return _ref2.apply(this, arguments);
  };
}();

var authenticateUser = function () {
  var _ref3 = asyncToGenerator._asyncToGenerator( /*#__PURE__*/asyncToGenerator.regenerator.mark(function _callee3(email, password) {
    var client, result, hashedPassword, isAuth;
    return asyncToGenerator.regenerator.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            _context3.prev = 0;
            client = new faunadb.Client({ secret: secret });
            _context3.next = 4;
            return client.query(q.Get(q.Match(q.Index('Email'), email)));

          case 4:
            result = _context3.sent;
            hashedPassword = result.data.Password;
            _context3.next = 8;
            return comparePassword(password, hashedPassword);

          case 8:
            isAuth = _context3.sent;
            return _context3.abrupt('return', isAuth ? { email: email } : false);

          case 12:
            _context3.prev = 12;
            _context3.t0 = _context3['catch'](0);

            console.log(_context3.t0);
            return _context3.abrupt('return', false);

          case 16:
          case 'end':
            return _context3.stop();
        }
      }
    }, _callee3, this, [[0, 12]]);
  }));

  return function authenticateUser(_x3, _x4) {
    return _ref3.apply(this, arguments);
  };
}();

exports.authenticateUser = authenticateUser;
exports.findUserByEmail = findUserByEmail;
exports.registerUser = registerUser;
