"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = _default;

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

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

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

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

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

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

var _immutable = require("immutable");

var _maxContent = _interopRequireDefault(require("../util/maxContent"));

var _instructureValidations = require("instructure-validations");

(0, _instructureValidations.addValidator)('maxContent', _maxContent.default);

function _default(schema) {
  var _class, _temp;

  return _temp = _class =
  /*#__PURE__*/
  function (_Record) {
    (0, _inherits2.default)(BaseRecord, _Record);

    function BaseRecord() {
      (0, _classCallCheck2.default)(this, BaseRecord);
      return (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(BaseRecord).apply(this, arguments));
    }

    (0, _createClass2.default)(BaseRecord, [{
      key: "getErrors",
      value: function getErrors() {
        var objectToValidate = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : this;
        return this.errors.merge((0, _instructureValidations.validate)(objectToValidate.toJS(), this.constructor.validations));
      }
    }, {
      key: "isValid",
      value: function isValid() {
        var obj = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : this;
        var errors = this.getErrors();

        if (errors.size === 0) {
          return true;
        }

        return !errors.find(function (val) {
          return val.size > 0;
        });
      }
    }, {
      key: "errorsFor",
      value: function errorsFor(attr) {
        // only for top-level attributes
        // use errorsIn for nested errors
        return this.errorsIn([attr]);
      }
    }, {
      key: "errorsIn",
      value: function errorsIn(nodes) {
        return this.getErrors().getIn(nodes, (0, _immutable.List)());
      }
    }, {
      key: "validData",
      value: function validData() {
        var errors = this.getErrors();
        return this.toMap().delete('errors').filter(function (attrVal, key) {
          return !errors.has(key);
        });
      }
    }], [{
      key: "create",
      // ====================
      // ====================
      // VALIDATION & ERRORS
      // ====================
      // ====================
      value: function create(attrs) {
        this.recordCache = this.hasOwnProperty('recordCache') ? this.recordCache : new WeakMap();
        var recordCache = this.recordCache;

        if (attrs && typeof attrs === 'object' && recordCache.has(attrs)) {
          return recordCache.get(attrs);
        }

        var newRecord = new this(attrs);

        if (attrs && typeof attrs === 'object') {
          recordCache.set(attrs, newRecord);
        }

        return newRecord;
      }
    }]);
    return BaseRecord;
  }((0, _immutable.Record)((0, _objectSpread2.default)({
    errors: (0, _immutable.Map)()
  }, schema))), _class.validations = {}, _temp;
}