var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.firestoreBatchDelete = exports.queryFirestore = exports.getFirebase = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

let firebaseInstance;

const getFirebase = (firebase, customToken, config) => {
  if (firebaseInstance) return firebaseInstance;
  firebase.initializeApp(config);
  firebase.auth().signInWithCustomToken(customToken).catch(error => {
    console.warn(error);
  });
  firebaseInstance = firebase;
  return firebase;
};

exports.getFirebase = getFirebase;

const queryFirestore = (firebase, collection, first, last, handleData, search, order, pageSize) => {
  let query = firebase.firestore().collection(collection);
  if (search.length) query = query.where("keywords", "array-contains", search.toLowerCase());
  query = query.orderBy(order[0], order[1]);
  if (first) query = query.endBefore(first).limitToLast(pageSize);
  if (last) query = query.startAfter(last);
  if (!first) query = query.limit(pageSize);
  query.get().then(snapshot => handleData(snapshot, !first && !last));
};

exports.queryFirestore = queryFirestore;

const firestoreBatchDelete = (firebase, collection, items) => {
  var query, batch;
  return _regenerator.default.async(function _callee$(_context) {
    while (1) switch (_context.prev = _context.next) {
      case 0:
        query = firebase.firestore().collection(collection);
        batch = firebase.firestore().batch();
        items.map(item => batch.delete(query.doc(item)));
        _context.next = 5;
        return _regenerator.default.awrap(batch.commit());

      case 5:
      case "end":
        return _context.stop();
    }
  }, null, null, null, Promise);
};

exports.firestoreBatchDelete = firestoreBatchDelete;
//# sourceMappingURL=firebase.js.map