'use strict';

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

var _typeof2 = require('babel-runtime/helpers/typeof');

var _typeof3 = _interopRequireDefault(_typeof2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

exports.default = function (schema) {
  var _class, _temp;

  return _temp = _class = function (_Record) {
    (0, _inherits3.default)(ReduxRecord, _Record);

    function ReduxRecord() {
      (0, _classCallCheck3.default)(this, ReduxRecord);
      return (0, _possibleConstructorReturn3.default)(this, (ReduxRecord.__proto__ || Object.getPrototypeOf(ReduxRecord)).apply(this, arguments));
    }

    (0, _createClass3.default)(ReduxRecord, [{
      key: 'getErrors',
      value: function getErrors() {
        var objectToValidate = arguments.length > 0 && arguments[0] !== undefined ? 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] !== undefined ? 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);
        });
      }

      // =====================
      // =====================
      // REDUX & RELATIONSHIPS
      // =====================
      // =====================

    }, {
      key: 'stateHelper',
      value: function stateHelper() {
        return _stateHelper3.default;
      }
    }, {
      key: 'getState',
      value: function getState() {
        return _reduxStore2.default.getAppState();
      }
    }, {
      key: 'getRelationList',
      value: function getRelationList(relationName, relatedItemFinder) {
        return this[relationName].reduce(function (relatedItemsFromState, id) {
          var currChoice = _stateHelper3.default[relatedItemFinder](id);
          if (currChoice) {
            return relatedItemsFromState.push(currChoice);
          }
          return relatedItemsFromState;
        }, (0, _immutable.List)());
      }
    }], [{
      key: 'create',
      value: function create(attrs) {
        this.recordCache = this.hasOwnProperty('recordCache') ? this.recordCache : new WeakMap();

        var recordCache = this.recordCache;


        if (attrs && (typeof attrs === 'undefined' ? 'undefined' : (0, _typeof3.default)(attrs)) === 'object' && recordCache.has(attrs)) {
          return recordCache.get(attrs);
        }

        var newRecord = new this(attrs);
        if (attrs && (typeof attrs === 'undefined' ? 'undefined' : (0, _typeof3.default)(attrs)) === 'object') {
          recordCache.set(attrs, newRecord);
        }
        return newRecord;
      }
      // ====================
      // ====================
      // VALIDATION & ERRORS
      // ====================
      // ====================

    }]);
    return ReduxRecord;
  }((0, _immutable.Record)(Object.assign({
    errors: (0, _immutable.Map)()
  }, schema))), _class.validations = {}, _temp;
};

var _immutable = require('immutable');

var _reduxStore = require('../../reduxStore');

var _reduxStore2 = _interopRequireDefault(_reduxStore);

var _maxContent = require('../util/maxContent');

var _maxContent2 = _interopRequireDefault(_maxContent);

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

var _stateHelper2 = require('../util/stateHelper');

var _stateHelper3 = _interopRequireDefault(_stateHelper2);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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