'use strict';

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

var _checkfunc = require('./checkfunc');

var _checkfunc2 = _interopRequireDefault(_checkfunc);

var _typeDic = require('./typeDic');

var _typeDic2 = _interopRequireDefault(_typeDic);

var _MapUtils = require('../../../utils/MapUtils');

var _StringUtils = require('../../../utils/StringUtils');

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

var checkFields = function checkFields(record, value, form, formValidate, isModalInputMappingItem) {
  var validateType = formValidate && formValidate.validateType;
  // const opt = formValidate && formValidate.opt;
  var opt = formValidate;
  var errors = _checkfunc2.default[validateType] && _checkfunc2.default[validateType](record, value, form.getFieldsValue(), form, opt, isModalInputMappingItem);
  return errors;
};

// 表单校验
var createRules = function createRules(record, form, isModalInputMappingItem) {
  var analyserName = (0, _MapUtils.getValueByKey)(record, 'default', 'displayComponent', 'componentType');
  var desc = '请输入';
  if (_typeDic.requiredDesc.indexOf(analyserName) >= 0) {
    if (analyserName !== 'MODALINPUT' || record.isReadonly) {
      desc = '请选择';
    }
  }
  var ret = [];
  if (record.isVisible) {
    var isRequired = { required: record.isRequired, message: desc + record.description };
    // if (analyserName !== 'CHECKBOXGROUP') {
    isRequired.type = _typeDic2.default[record.type];
    // }
    ret.push(isRequired);
    ret.push({
      validator: function validator(rule, value, callback) {
        // TODO: 多个校验怎么处理
        // if (record.type === 'String') {
        //   if (value && value.length > record.length) {
        //     callback([new Error('请输入' + record.length + '个汉字内的' + record.description)]);
        //     return;
        //   }
        // }
        var formValidate = record && record.formValidate || record && record.formValidates;
        if ((0, _StringUtils.isArray)(formValidate)) {
          (function () {
            var errors = [];
            formValidate.some(function (formV) {
              var er = checkFields(record, value, form, formV, isModalInputMappingItem);
              if (er) {
                errors.push(er);
              }
              return er;
            });
            callback(errors);
          })();
        } else {
          var errors = checkFields(record, value, form, formValidate, isModalInputMappingItem);
          callback(errors && [errors] || []);
        }
      }
    });
  }
  return ret;
};

exports.default = createRules;