function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

import { difference, without } from 'lodash';

function ipValidator(dataSet) {
  dataSet.forEach(function (eachRecord) {
    return eachRecord.getField('hostIp').checkValidity();
  });
}

export default (function (_ref) {
  var accountDs = _ref.accountDs,
      formatMessage = _ref.formatMessage,
      intlPrefix = _ref.intlPrefix,
      createHostClusterStore = _ref.createHostClusterStore;

  function hasAllCheckedFields(record, fieldName) {
    var tempArr = ['hostIp', 'hostPort', 'username', 'password'];
    var omitArr = without(tempArr, fieldName);
    return omitArr.some(function (item) {
      return record.get(item);
    });
  }

  function checkPort(_x, _x2, _x3) {
    return _checkPort.apply(this, arguments);
  }

  function _checkPort() {
    _checkPort = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(value, name, record) {
      var p, data;
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              if (!(value && record.getPristineValue(name) && String(value) === String(record.getPristineValue(name)))) {
                _context.next = 2;
                break;
              }

              return _context.abrupt("return", true);

            case 2:
              p = /^([1-9]\d*|0)$/;
              data = {
                typeMsg: '',
                min: 1,
                max: 65535,
                failedMsg: "".concat(intlPrefix, ".port.check.failed")
              };

              if (!value) {
                _context.next = 8;
                break;
              }

              if (!(p.test(value) && parseInt(value, 10) >= data.min && parseInt(value, 10) <= data.max)) {
                _context.next = 7;
                break;
              }

              return _context.abrupt("return", true);

            case 7:
              return _context.abrupt("return", formatMessage({
                id: data.failedMsg
              }));

            case 8:
              return _context.abrupt("return", true);

            case 9:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }));
    return _checkPort.apply(this, arguments);
  }

  function checkoutHasSameIP(value, name, record) {
    var p = /^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$/;

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

    return true;
  }

  function handleUpdate(_x4) {
    return _handleUpdate.apply(this, arguments);
  }

  function _handleUpdate() {
    _handleUpdate = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(_ref2) {
      var dataSet, record, name, value, oldValue, reConnectFields;
      return regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              dataSet = _ref2.dataSet, record = _ref2.record, name = _ref2.name, value = _ref2.value, oldValue = _ref2.oldValue;

              if (name && value) {
                record.set('type', 'outter');
              }

              if (name === 'hostIp') {
                ipValidator(dataSet);
              }

              reConnectFields = ['hostIp', 'hostPort', 'authType', 'username', 'password'];

              if (reConnectFields.includes(name) && value !== oldValue) {
                record.set('status', null);
              }

            case 5:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2);
    }));
    return _handleUpdate.apply(this, arguments);
  }

  return {
    autoCreate: true,
    events: {
      update: handleUpdate
    },
    fields: [{
      name: 'isInnerNode',
      type: 'boolean',
      label: '是否同时作为集群节点',
      ignore: 'always'
    }, {
      name: 'innerNodeName',
      type: 'string',
      label: '集群节点',
      dynamicProps: {
        required: function required(_ref3) {
          var record = _ref3.record;
          return record.get('isInnerNode') && hasAllCheckedFields(record);
        },
        ignore: function ignore(_ref4) {
          var record = _ref4.record;
          return record.get('isInnerNode') ? 'never' : 'always';
        }
      }
    }, {
      name: 'hostIp',
      type: 'string',
      label: '公网IP',
      dynamicProps: {
        required: function required(_ref5) {
          var record = _ref5.record;
          return hasAllCheckedFields(record);
        }
      },
      validator: checkoutHasSameIP
    }, {
      name: 'hostPort',
      type: 'string',
      label: '公网SSH端口',
      dynamicProps: {
        required: function required(_ref6) {
          var record = _ref6.record;
          return hasAllCheckedFields(record);
        }
      },
      validator: checkPort
    }, {
      name: 'authType',
      type: 'string',
      label: '账号配置',
      textField: 'text',
      valueField: 'value',
      dynamicProps: {
        required: function required(_ref7) {
          var record = _ref7.record;
          return hasAllCheckedFields(record);
        }
      },
      defaultValue: 'accountPassword',
      options: accountDs
    }, {
      name: 'username',
      type: 'string',
      label: '用户名',
      dynamicProps: {
        required: function required(_ref8) {
          var record = _ref8.record;
          return hasAllCheckedFields(record);
        }
      }
    }, {
      name: 'password',
      type: 'string',
      dynamicProps: {
        required: function required(_ref9) {
          var record = _ref9.record;
          return hasAllCheckedFields(record);
        },
        label: function label(_ref10) {
          var record = _ref10.record;
          return formatMessage({
            id: record.get('authType') === 'accountPassword' ? 'password' : "".concat(intlPrefix, ".nodesCreate.token")
          });
        }
      }
    }, {
      name: 'type',
      type: 'string',
      dynamicProps: {
        defaultValue: function defaultValue(_ref11) {
          var record = _ref11.record;
          return hasAllCheckedFields(record) ? 'outter' : null;
        }
      }
    }, {
      name: 'status',
      type: 'string',
      validator: function validator(value, name, record) {
        // 当填了公网节点的值的时候就应该去检测这个公网节点测通了没
        if (hasAllCheckedFields(record)) {
          if (!value) {
            record.set('status', 'wait');
            return false;
          }

          if (value === 'success') {
            return true;
          }

          return false;
        }

        return true;
      }
    }]
  };
});