export default (function (_ref) {
  var formatMessage = _ref.formatMessage,
      keyOptionsDs = _ref.keyOptionsDs;

  /**
   * 关键字检查
   * @param rule
   * @param value
   * @param callback
   */
  function checkKeywords(value, name, record) {
    // 必须由字母数字字符，' - '，'_'或'.'组成，并且必须以字母数字开头和结尾
    // 并且包括可选的DNS子域前缀(包括一级、二级域名)和'/'（例如'example.com/MyName'）
    var p = /^((?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\/)*([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]$/;
    var isRepeat = record.dataSet.findIndex(function (r) {
      return record.id !== r.id && r.get(name) === value;
    }) !== -1;

    if (value) {
      if (p.test(value)) {
        if (isRepeat) {
          return formatMessage({
            id: 'network.label.check.repeat'
          });
        }
      } else {
        return formatMessage({
          id: 'network.label.check.failed'
        });
      }
    }
  }

  function checkValue(value, name, record) {
    var p = /^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?$/;

    if (value) {
      if (!p.test(value)) {
        return formatMessage({
          id: 'network.label.check.failed'
        });
      }
    }
  }

  var dynamicProps = {
    required: function required(_ref2) {
      var dataSet = _ref2.dataSet,
          record = _ref2.record,
          name = _ref2.name;
      if (!dataSet.parent.current) return false;
      return dataSet.parent.current.get('target') === 'param';
    }
  };
  return {
    fields: [{
      name: 'keyword',
      type: 'string',
      label: formatMessage({
        id: 'network.config.keyword'
      }),
      options: keyOptionsDs,
      validator: checkKeywords,
      textField: 'key',
      valueField: 'meaning',
      dynamicProps: dynamicProps
    }, {
      name: 'value',
      type: 'string',
      label: formatMessage({
        id: 'network.config.value'
      }),
      options: keyOptionsDs,
      validator: checkValue,
      textField: 'value',
      valueField: 'meaning',
      dynamicProps: dynamicProps
    }],
    events: {
      update: updateEventHandler
    }
  };
});

function updateEventHandler(_ref3) {
  var dataSet = _ref3.dataSet,
      record = _ref3.record,
      name = _ref3.name,
      value = _ref3.value,
      oldValue = _ref3.oldValue;

  if (value && value.includes(':')) {
    var splitkv = value.split(':');
    record.set('keyword', splitkv[0]);
    record.set('value', splitkv[1]);
  } // 当keyword的值发生变化的时候 对其余记录做校验 


  if (name === 'keyword') {
    checkOtherRecords(record, name);
  }
}

function checkOtherRecords(record, type) {
  record.dataSet.forEach(function (r) {
    if (r.id !== record.id) {
      // 此处只对重复性做校验，不对空值做校验
      if (r.get(type)) {
        r.getField(type).checkValidity();
      }
    }
  });
}