"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.adduserDetails = adduserDetails;
exports.getUserDetails = getUserDetails;
exports.setupDB = setupDB;

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

//
// Database management
//
let db;
let dbNamespace;

function setupDB(namespace, callback) {
  if (namespace != dbNamespace) {
    db = null;
  }

  dbNamespace = namespace; // If setupDB has already been run and the database was set up, no need to
  // open the database again; just run our callback and return!

  if (db) {
    callback();
    return;
  }

  let dbName = namespace == '' ? 'myDatabase' : 'myDatabase_' + namespace;
  let dbReq = indexedDB.open(dbName, 2); // Fires when the version of the database goes up, or the database is created
  // for the first time

  dbReq.onupgradeneeded = function (event) {
    db = event.target.result; // Create an object store named userDetails, or retrieve it if it already exists.
    // Object stores in databases are where data are stored.

    let userDetails;

    if (!db.objectStoreNames.contains('userDetails')) {
      userDetails = db.createObjectStore('userDetails', {
        autoIncrement: true
      });
    } else {
      userDetails = dbReq.transaction.objectStore('userDetails');
    }
  }; // Fires once the database is opened (and onupgradeneeded completes, if
  // onupgradeneeded was called)


  dbReq.onsuccess = function (event) {
    // Set the db variable to our database so we can use it!
    db = event.target.result;
    callback();
  }; // Fires when we can't open the database


  dbReq.onerror = function (event) {
    alert('error opening database ' + event.target.errorCode);
  };
} //
// IndexedDB functions
//
// adduserDetails function is used to adds userDetails in the mocked IndexedDB
// object store.


function adduserDetails(message, callback) {
  // Start a database transaction and get the userDetails object store
  let tx = db.transaction(['userDetails'], 'readwrite');
  let store = tx.objectStore('userDetails'); // Put userDetails into the object store

  const userDetails = message;
  store.add(userDetails); // Wait for the database transaction to complete. If it is successful, run
  // the callback function

  tx.oncomplete = callback;

  tx.onerror = function (event) {
    alert('error storing userDetails ' + event.target.errorCode);
  };
} // getUserDetails retrieves all data in the userDetails object store using an IndexedDB
// cursor and sends them to the calback passed in


function getUserDetails(reverseOrder, callback) {
  let tx = db.transaction(['userDetails'], 'readonly'); // Retrieve the userDetails object store to run our cursor query on

  let store = tx.objectStore('userDetails');
  let req = store.openCursor(null, reverseOrder ? 'prev' : 'next');
  let userDetails = {};

  req.onsuccess = function (event) {
    let cursor = event.target.result;

    if (cursor != null) {
      userDetails = _objectSpread({}, cursor.value);
      cursor.continue();
    } else {
      // If we have a null cursor, it means we've gotten all the items in the
      // store, so display the userDetails we got
      callback(userDetails);
    }
  };

  req.onerror = function (event) {
    alert('error in cursor request ' + event.target.errorCode);
  };
}