function keyValidator(dataSet) {
  dataSet.forEach(function (eachRecord) {
    var _eachRecord$getField;

    return (_eachRecord$getField = eachRecord.getField('key')) === null || _eachRecord$getField === void 0 ? void 0 : _eachRecord$getField.checkValidity();
  });
}

function handleUpdate(_ref) {
  var name = _ref.name,
      dataSet = _ref.dataSet;

  if (name === 'key') {
    keyValidator(dataSet);
  }
}

function handleRemove(_ref2) {
  var dataSet = _ref2.dataSet;
  keyValidator(dataSet);
}

export default (function (_ref3) {
  var formatMessage = _ref3.formatMessage,
      intlPrefix = _ref3.intlPrefix;

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

    if (!value && !record.get('value')) {
      return true;
    }

    if (!value && record.get('value')) {
      return formatMessage({
        id: 'mapping.keyValueSpan'
      });
    }

    if (p.test(value)) {
      var dataSet = record.dataSet;
      var repeatRecord = dataSet === null || dataSet === void 0 ? void 0 : dataSet.find(function (eachRecord) {
        return eachRecord.id !== record.id && eachRecord.get('key') === value;
      });

      if (repeatRecord) {
        return formatMessage({
          id: "".concat(intlPrefix, ".label.exist")
        });
      }

      return true;
    }

    return formatMessage({
      id: "".concat(intlPrefix, ".label.failed")
    });
  };

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

    if (!value && record.get('key')) {
      return formatMessage({
        id: 'mapping.keyValueSpan'
      });
    }

    if (value && !p.test(value)) {
      return formatMessage({
        id: "".concat(intlPrefix, ".label.failed")
      });
    }

    return true;
  };

  return {
    autoCreate: true,
    autoQuery: false,
    selection: false,
    paging: false,
    transport: {},
    fields: [{
      name: 'key',
      type: 'string',
      label: formatMessage({
        id: 'key'
      }),
      // @ts-ignore
      validator: checkKey,
      maxLength: 63
    }, {
      name: 'value',
      type: 'string',
      label: formatMessage({
        id: 'value'
      }),
      // @ts-ignore
      validator: checkValue,
      maxLength: 63
    }],
    events: {
      update: handleUpdate,
      remove: handleRemove
    }
  };
});