import { a as _asyncToGenerator, r as regenerator } from './asyncToGenerator-7f8f9219.js';
import { m as md5 } from './md5-4e42248e.js';
import './_commonjsHelpers-0c4b6f40.js';
import './es6.string.iterator-bc33758b.js';
import './_to-object-b50e61c3.js';
import './web.dom.iterable-4109ff68.js';
import './_library-528f1934.js';
import './core.get-iterator-method-133dfacd.js';
import './_iter-detect-7e5e5ce2.js';

var encrypt = function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee(password) {
    var hash;
    return regenerator.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            hash = 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( /*#__PURE__*/regenerator.mark(function _callee2(plainTextPassword, hashedPassword) {
    var hash;
    return regenerator.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            hash = 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 registerUser = function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee(body) {
    var secret = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'fnAEG3Bb7AACAC0BiSQFYGBLFUtHjn3QOTKE1iBX';
    var data, client, hashedPassword, result;
    return regenerator.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.prev = 0;
            data = body;

            if (secret) {
              _context.next = 4;
              break;
            }

            throw new Error('Missing secret key');

          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( /*#__PURE__*/regenerator.mark(function _callee2(email) {
    var secret = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'fnAEG3Bb7AACAC0BiSQFYGBLFUtHjn3QOTKE1iBX';
    var client, result;
    return 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(_x3) {
    return _ref2.apply(this, arguments);
  };
}();

var authenticateUser = function () {
  var _ref3 = _asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee3(email, password) {
    var secret = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'fnAEG3Bb7AACAC0BiSQFYGBLFUtHjn3QOTKE1iBX';
    var client, result, hashedPassword, isAuth;
    return 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(_x5, _x6) {
    return _ref3.apply(this, arguments);
  };
}();

var updateUserSecret = function () {
  var _ref4 = _asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee4(email, hash) {
    var secret = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'fnAEG3Bb7AACAC0BiSQFYGBLFUtHjn3QOTKE1iBX';
    var expiration, client, result;
    return regenerator.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            expiration = new Date().getTime() + 43200000; //1day

            _context4.prev = 1;

            console.log(email, hash, expiration);
            client = new faunadb.Client({ secret: secret });
            _context4.next = 6;
            return client.query(q.Update(q.Select(['ref'], q.Get(q.Match(q.Index('Email'), email))), { data: { secretKey: hash, secretExpiration: expiration } }));

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

          case 10:
            _context4.prev = 10;
            _context4.t0 = _context4['catch'](1);

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

          case 14:
          case 'end':
            return _context4.stop();
        }
      }
    }, _callee4, this, [[1, 10]]);
  }));

  return function updateUserSecret(_x8, _x9) {
    return _ref4.apply(this, arguments);
  };
}();

var updateUserPassword = function () {
  var _ref5 = _asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee5(hash, newPassword) {
    var secret = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'fnAEG3Bb7AACAC0BiSQFYGBLFUtHjn3QOTKE1iBX';
    var now, client, result, expiration, password, updatePassword;
    return regenerator.wrap(function _callee5$(_context5) {
      while (1) {
        switch (_context5.prev = _context5.next) {
          case 0:
            _context5.prev = 0;
            now = Date.now();
            client = new faunadb.Client({ secret: secret });
            _context5.next = 5;
            return client.query(q.Get(q.Match(q.Index('SecretKey'), hash)));

          case 5:
            result = _context5.sent;
            expiration = result.data.secretExpiration;

            if (!(now > expiration)) {
              _context5.next = 9;
              break;
            }

            throw new Error('eKey has expired.');

          case 9:
            if (!(now < expiration)) {
              _context5.next = 18;
              break;
            }

            _context5.next = 12;
            return encrypt(newPassword);

          case 12:
            password = _context5.sent;
            _context5.next = 15;
            return client.query(q.Update(q.Select(['ref'], q.Get(q.Match(q.Index('SecretKey'), hash))), { data: { Password: password, secretKey: '' } }));

          case 15:
            updatePassword = _context5.sent;

            if (updatePassword) {
              _context5.next = 18;
              break;
            }

            throw new Error('Failed to update password.');

          case 18:
            if (!(!result || result.error)) {
              _context5.next = 20;
              break;
            }

            throw new Error('User not found');

          case 20:
            return _context5.abrupt('return', true);

          case 23:
            _context5.prev = 23;
            _context5.t0 = _context5['catch'](0);
            return _context5.abrupt('return', { err: _context5.t0 });

          case 26:
          case 'end':
            return _context5.stop();
        }
      }
    }, _callee5, this, [[0, 23]]);
  }));

  return function updateUserPassword(_x11, _x12) {
    return _ref5.apply(this, arguments);
  };
}();

export { authenticateUser, findUserByEmail, registerUser, updateUserPassword, updateUserSecret };
