import "choerodon-ui/pro/lib/data-set/style";
import _DataSet from "choerodon-ui/pro/lib/data-set";
import map from 'lodash/map';

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

export default (function (_ref) {
  var formatMessage = _ref.formatMessage,
      pathListDs = _ref.pathListDs;
  var protocolDs = new _DataSet({
    data: [{
      value: 'TCP'
    }, {
      value: 'UDP'
    }],
    selection: 'single'
  });
  /**
   * 验证端口号
   * @param value
   * @param type
   * @param record
   */

  function checkPort(value, type, record) {
    var p = /^([1-9]\d*|0)$/;
    var isRepeat = record.dataSet.some(function (r) {
      return record.id === r.id;
    }) && record.dataSet.some(function (r) {
      return record.id !== r.id && r.get(type) === value;
    });
    var data = {
      typeMsg: '',
      min: 0,
      max: 65535,
      failedMsg: 'network.port.check.failed'
    };

    switch (type) {
      case 'targetPort':
        data.typeMsg = 'network.tport.check.repeat';
        break;

      case 'nodePort':
        data.typeMsg = 'network.nport.check.repeat';
        data.min = 30000;
        data.max = 32767;
        data.failedMsg = 'network.nport.check.failed';
        break;

      default:
        data.typeMsg = 'network.port.check.repeat';
    }

    if (value) {
      if (p.test(value) && parseInt(value, 10) >= data.min && parseInt(value, 10) <= data.max) {
        if (!isRepeat) {
          return true;
        } else {
          return formatMessage({
            id: data.typeMsg
          });
        }
      } else {
        return formatMessage({
          id: data.failedMsg
        });
      }
    }
  }

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

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

    if (name === 'port') {
      var ports = map(dataSet.toData(), function (_ref3) {
        var port = _ref3.port;

        if (port) {
          return Number(port);
        }
      });
      pathListDs.forEach(function (pathRecord) {
        return pathRecord.init('ports', ports);
      });
    }
  }

  function isRequired(_ref4) {
    var dataSet = _ref4.dataSet,
        record = _ref4.record;
    var name = record.cascadeParent && record.cascadeParent.get('name');
    var dirty = dataSet.some(function (portRecord) {
      return portRecord === record;
    }) && dataSet.some(function (portRecord) {
      return portRecord.dirty;
    });
    return dirty || !!name;
  }

  return {
    autoCreate: true,
    fields: [{
      name: 'nodePort',
      type: 'string',
      label: formatMessage({
        id: 'network.config.nodePort'
      }),
      validator: checkPort,
      maxLength: 5
    }, {
      name: 'port',
      type: 'string',
      label: formatMessage({
        id: 'network.config.port'
      }),
      validator: checkPort,
      dynamicProps: {
        required: isRequired
      },
      maxLength: 5
    }, {
      name: 'targetPort',
      type: 'string',
      label: formatMessage({
        id: 'network.config.targetPort'
      }),
      required: true,
      validator: checkPort,
      dynamicProps: {
        required: isRequired
      },
      maxLength: 5
    }, {
      name: 'protocol',
      type: 'string',
      label: formatMessage({
        id: 'ist.deploy.ports.protocol'
      }),
      textField: 'value',
      valueField: 'value',
      dynamicProps: {
        required: function required(_ref5) {
          var dataSet = _ref5.dataSet,
              record = _ref5.record;
          return isRequired({
            dataSet: dataSet,
            record: record
          }) && record.cascadeParent.get('type') === 'NodePort';
        }
      },
      options: protocolDs
    }],
    events: {
      update: handleUpdate
    }
  };
});