"use strict";

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

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

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

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

var _isString = _interopRequireDefault(require("lodash/isString"));

var _ValidationResult = _interopRequireDefault(require("../ValidationResult"));

var _localeContext = require("../../locale-context");

var _utils = require("../../utils");

var _utils2 = require("../../data-set/utils");

var _enum = require("../../data-set/enum");

var _iteratorHelper = require("../../iterator-helper");

function uniqueError(value, props, getProp) {
  var dataSet = props.dataSet,
      record = props.record,
      name = props.name;

  if (!(0, _utils.isEmpty)(value) && dataSet && record && name) {
    var unique = getProp('unique');

    if (unique) {
      var multiple = getProp('multiple');

      if (!multiple) {
        var range = getProp('range');

        if (!range) {
          var myField = dataSet.getField(name);

          if (myField && getProp('type') === _enum.FieldType.object) {
            value = value[myField.get('valueField', record)];

            if ((0, _utils.isEmpty)(value)) {
              return true;
            }
          }

          if (myField) {
            var dirty = myField.isDirty(record);
            var fields = (0, _defineProperty2["default"])({}, name, value);

            if (!dirty) {
              return true;
            }

            if ((0, _isString["default"])(unique) && (0, _iteratorHelper.iteratorSome)(dataSet.fields.entries(), function (_ref) {
              var _ref2 = (0, _slicedToArray2["default"])(_ref, 2),
                  fieldName = _ref2[0],
                  field = _ref2[1];

              if (fieldName !== name && field && field.get('unique', record) === unique && !field.get('multiple', record) && !field.get('range', record)) {
                var otherValue = record.get(fieldName);

                if ((0, _utils.isEmpty)(otherValue)) {
                  return true;
                }

                if (!dirty && field.isDirty(record)) {
                  dirty = true;
                }

                if (field && field.get('type', record) === _enum.FieldType.object) {
                  var otherObjectValue = otherValue[field.get('valueField', record)];

                  if ((0, _utils.isEmpty)(otherObjectValue)) {
                    return true;
                  }

                  fields[fieldName] = otherObjectValue;
                } else {
                  fields[fieldName] = otherValue;
                }
              }

              return false;
            })) {
              return true;
            }

            var invalid = dataSet.some(function (item) {
              return item !== record && Object.keys(fields).every(function (field) {
                var dataSetField = dataSet.getField(name);

                if (dataSetField && dataSetField.get('type', record) === _enum.FieldType.object) {
                  var valueField = dataSetField.get('valueField', record);
                  return fields[field] === item.get(field)[valueField];
                }

                return fields[field] === item.get(field);
              });
            });

            var call = function call($invalid) {
              if ($invalid) {
                var ruleName = 'uniqueError';

                var _ref3 = getProp('defaultValidationMessages') || {},
                    _ref3$ruleName = _ref3[ruleName],
                    validationMessage = _ref3$ruleName === void 0 ? (0, _localeContext.$l)('Validator', 'unique') : _ref3$ruleName;

                return new _ValidationResult["default"]({
                  validationProps: {
                    unique: unique,
                    multiple: multiple,
                    range: range
                  },
                  validationMessage: validationMessage,
                  value: value,
                  ruleName: ruleName
                });
              }

              return true;
            };

            if (!invalid) {
              var newConfig = (0, _utils2.axiosConfigAdapter)('validate', dataSet, {
                unique: [fields]
              });

              if (newConfig.url) {
                var resultsPromise = dataSet.axios(newConfig);
                return resultsPromise.then(function (results) {
                  return call([].concat(results).some(function (result) {
                    return !result;
                  }));
                });
              }
            }

            return call(invalid);
          }
        }
      }
    }
  }

  return true;
}
//# sourceMappingURL=uniqueError.js.map
