import { _ as _asyncToGenerator, r as regenerator } from './asyncToGenerator-64822ccc.js';
import { m as md5 } from './md5-4e42248e.js';
import './_commonjsHelpers-0c4b6f40.js';
import './core.get-iterator-method-ea258bb1.js';
import './web.dom.iterable-ab4dea5c.js';
import './_library-528f1934.js';
import './_set-species-2cd9340f.js';
import './_iter-detect-98db3b07.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 authenticateUserByForm = function () {
  var _ref4 = _asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee4(email, password, form_name) {
    var secret = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'fnAEG3Bb7AACAC0BiSQFYGBLFUtHjn3QOTKE1iBX';
    var client, result, hashedPassword, isAuth;
    return regenerator.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            _context4.prev = 0;
            client = new faunadb.Client({ secret: secret });
            _context4.next = 4;
            return client.query(q.Get(q.Match(q.Index('Email_and_FormName'), [email, form_name])));

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

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

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

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

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

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

var findUserByEmailAndForm = function () {
  var _ref5 = _asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee5(email, form_name) {
    var secret = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'fnAEG3Bb7AACAC0BiSQFYGBLFUtHjn3QOTKE1iBX';
    var client, result;
    return regenerator.wrap(function _callee5$(_context5) {
      while (1) {
        switch (_context5.prev = _context5.next) {
          case 0:
            _context5.prev = 0;
            client = new faunadb.Client({ secret: secret });
            _context5.next = 4;
            return client.query(q.Get(q.Match(q.Index('Email_and_FormName'), [email, form_name])));

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

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

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

  return function findUserByEmailAndForm(_x12, _x13) {
    return _ref5.apply(this, arguments);
  };
}();

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

            _context6.prev = 1;

            console.log(email, hash, expiration);
            client = new faunadb.Client({ secret: secret });
            _context6.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 = _context6.sent;
            return _context6.abrupt('return', result ? true : false);

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

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

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

  return function updateUserSecret(_x15, _x16) {
    return _ref6.apply(this, arguments);
  };
}();
var updateUserSecretByForm = function () {
  var _ref7 = _asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee7(email, hash, form) {
    var secret = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'fnAEG3Bb7AACAC0BiSQFYGBLFUtHjn3QOTKE1iBX';
    var expiration, client, result;
    return regenerator.wrap(function _callee7$(_context7) {
      while (1) {
        switch (_context7.prev = _context7.next) {
          case 0:
            expiration = new Date().getTime() + 43200000; //1day

            _context7.prev = 1;

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

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

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

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

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

  return function updateUserSecretByForm(_x18, _x19, _x20) {
    return _ref7.apply(this, arguments);
  };
}();

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

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

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

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

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

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

          case 12:
            password = _context8.sent;
            _context8.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 = _context8.sent;

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

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

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

            throw new Error('User not found');

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

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

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

  return function updateUserPassword(_x22, _x23) {
    return _ref8.apply(this, arguments);
  };
}();

export { authenticateUser, authenticateUserByForm, findUserByEmail, findUserByEmailAndForm, registerUser, updateUserPassword, updateUserSecret, updateUserSecretByForm };
