'use strict';

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var _regeneratorRuntime = _interopDefault(require('@babel/runtime/regenerator'));
var _asyncToGenerator = _interopDefault(require('@babel/runtime/helpers/asyncToGenerator'));
var _classCallCheck = _interopDefault(require('@babel/runtime/helpers/classCallCheck'));
var _createClass = _interopDefault(require('@babel/runtime/helpers/createClass'));
var _possibleConstructorReturn = _interopDefault(require('@babel/runtime/helpers/possibleConstructorReturn'));
var _getPrototypeOf = _interopDefault(require('@babel/runtime/helpers/getPrototypeOf'));
var _assertThisInitialized = _interopDefault(require('@babel/runtime/helpers/assertThisInitialized'));
var _inherits = _interopDefault(require('@babel/runtime/helpers/inherits'));
var _defineProperty = _interopDefault(require('@babel/runtime/helpers/defineProperty'));
var Common = _interopDefault(require('./Common'));
var Constants = require('./Constants');
var utils = require('@forensic-js/utils');

var DBChecker =
/*#__PURE__*/
function (_Common) {
  _inherits(DBChecker, _Common);

  function DBChecker() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, DBChecker);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(DBChecker)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_this), "dbModel", void 0);

    _defineProperty(_assertThisInitialized(_this), "dbCaseStyle", void 0);

    _defineProperty(_assertThisInitialized(_this), "query", {});

    return _this;
  }

  _createClass(DBChecker, [{
    key: "execute",

    /**
     * executes the query
     */
    value: function execute(model, query) {
      if (this.dbModel === Constants.DB_MODELS.NOSQL) {
        return model.countDocuments(query).exec();
      } else {
        return model.count({
          where: query
        });
      }
    }
    /**
     * resets the db checker, and checks if the check call should proceed
     */

  }, {
    key: "setup",
    value: function setup(required, field, value, check, index) {
      this.reset(field, check, index);

      if (value === '' && !required) {
        this.shouldProceed = false;
      } else {
        this.shouldProceed = true;

        if (check.query) {
          this.query = check.query;
        } else {
          this.query = _defineProperty({}, utils.applyCase(check.field || field, this.dbCaseStyle), value);
        }
      }

      return this.shouldProceed;
    }
    /**
     * sets db model to use
     *
     * @param dbModel db model to use
     */

  }, {
    key: "setDBModel",
    value: function setDBModel(dbModel) {
      this.dbModel = dbModel;
      return this;
    }
    /**
     * sets db model field case style to use
     *
     * @param dbModel db model case style to use
     */

  }, {
    key: "setDBCaseStyle",
    value: function setDBCaseStyle(dbCaseStyle) {
      this.dbCaseStyle = dbCaseStyle;
      return this;
    }
    /**
     * checks if database field value exists, setting error if it does
     *
     * @param required boolean indicating if field is required
     * @param field current field under check
     * @param value current field value under check
     * @param check check options
     * @param index current field value index under check
     */

  }, {
    key: "checkIfExists",
    value: function () {
      var _checkIfExists = _asyncToGenerator(
      /*#__PURE__*/
      _regeneratorRuntime.mark(function _callee(required, field, value, check, index) {
        var count;
        return _regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                if (!this.setup(required, field, value, check, index)) {
                  _context.next = 5;
                  break;
                }

                _context.next = 3;
                return this.execute(check.model, this.query);

              case 3:
                count = _context.sent;

                if (count > 0) {
                  this.setError(utils.pickValue('err', check, '{_this}:{this} already exists'), value.toString());
                }

              case 5:
                return _context.abrupt("return", this.succeeds());

              case 6:
              case "end":
                return _context.stop();
            }
          }
        }, _callee, this);
      }));

      function checkIfExists(_x, _x2, _x3, _x4, _x5) {
        return _checkIfExists.apply(this, arguments);
      }

      return checkIfExists;
    }()
    /**
     * checks if database field value does not exist, setting error if it does not
     *
     * @param required boolean indicating if field is required
     * @param field current field under check
     * @param value current field value under check
     * @param check check options
     * @param index current field value index under check
     */

  }, {
    key: "checkIfNotExists",
    value: function () {
      var _checkIfNotExists = _asyncToGenerator(
      /*#__PURE__*/
      _regeneratorRuntime.mark(function _callee2(required, field, value, check, index) {
        var count;
        return _regeneratorRuntime.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                if (!this.setup(required, field, value, check, index)) {
                  _context2.next = 5;
                  break;
                }

                _context2.next = 3;
                return this.execute(check.model, this.query);

              case 3:
                count = _context2.sent;

                if (count === 0) {
                  this.setError(utils.pickValue('err', check, '{_this}:{this} does not exist'), value.toString());
                }

              case 5:
                return _context2.abrupt("return", this.succeeds());

              case 6:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2, this);
      }));

      function checkIfNotExists(_x6, _x7, _x8, _x9, _x10) {
        return _checkIfNotExists.apply(this, arguments);
      }

      return checkIfNotExists;
    }()
  }]);

  return DBChecker;
}(Common);

module.exports = DBChecker;
//# sourceMappingURL=DBChecker.js.map
