function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import { map } from 'lodash';
export default (function (_ref) {
  var formatMessage = _ref.formatMessage,
      projectId = _ref.projectId,
      envId = _ref.envId,
      appId = _ref.appId;

  /**
   * 验证端口号
   * @param value
   * @param type
   * @param record
   */
  function checkPort(value, type, record) {
    var p = /^([1-9]\d*|0)$/;
    var isRepeat = record.dataSet.findIndex(function (r) {
      return record.id !== r.id && r.get(type) === value;
    }) !== -1;
    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
        });
      }
    }
  }

  return {
    fields: [{
      name: 'nodePort',
      type: 'string',
      label: formatMessage({
        id: 'network.config.nodePort'
      }),
      validator: checkPort
    }, {
      name: 'port',
      type: 'string',
      label: formatMessage({
        id: 'network.config.port'
      }),
      required: true,
      validator: checkPort
    }, {
      name: 'targetPort',
      type: 'string',
      label: formatMessage({
        id: 'network.config.targetPort'
      }),
      required: true,
      validator: checkPort,
      valueField: 'codePort',
      textField: 'resourceName',
      lookupAxiosConfig: {
        method: 'get',
        url: "/devops/v1/projects/".concat(projectId, "/env/app_services/list_port?env_id=").concat(envId, "&app_service_id=").concat(appId),
        transformResponse: function transformResponse(resp) {
          try {
            var data = JSON.parse(resp);

            if (data && data.failed) {
              return data;
            } else {
              return map(data, function (item, index) {
                return _objectSpread(_objectSpread({}, item), {}, {
                  codePort: "".concat(item.resourceName, ": ").concat(item.portValue)
                });
              });
            }
          } catch (e) {
            return resp;
          }
        }
      }
    }, {
      name: 'protocol',
      type: 'string',
      label: formatMessage({
        id: 'ist.deploy.ports.protocol'
      }),
      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('type') === 'NodePort';
        }
      }
    }],
    events: {
      update: updateEventHandler
    }
  };
});

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

  if (name === 'targetPort') {
    if (value && value.indexOf(':') >= 0) {
      record.set(name, value.slice(value.indexOf(':') + 2));
    }
  } // 对于在 targetPort，nodePort,port这几个字段，当值发生改变时，对其余记录进行校验


  if (name !== 'protocol') {
    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();
      }
    }
  });
}