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 { axios } from '@choerodon/boot';
import some from 'lodash/some';
import forEach from 'lodash/forEach';
export default (function (_ref) {
  var formatMessage = _ref.formatMessage,
      projectId = _ref.projectId,
      portsDs = _ref.portsDs,
      pathListDs = _ref.pathListDs;

  function checkName(_x, _x2, _x3) {
    return _checkName.apply(this, arguments);
  }

  function _checkName() {
    _checkName = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(value, name, record) {
      var envId, pattern, res;
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              envId = record.cascadeParent.get('environmentId');

              if (envId) {
                _context.next = 3;
                break;
              }

              return _context.abrupt("return");

            case 3:
              pattern = /^[a-z]([-a-z0-9]*[a-z0-9])?$/;

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

              return _context.abrupt("return", formatMessage({
                id: 'network.name.check.failed'
              }));

            case 8:
              if (!(value && pattern.test(value))) {
                _context.next = 14;
                break;
              }

              _context.next = 11;
              return axios.get("/devops/v1/projects/".concat(projectId, "/service/check_name?env_id=").concat(envId, "&name=").concat(value));

            case 11:
              res = _context.sent;

              if (res) {
                _context.next = 14;
                break;
              }

              return _context.abrupt("return", formatMessage({
                id: 'network.name.check.exist'
              }));

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

  function checkIP(value, name, record) {
    var p = /^((\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.){3}(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])$/;
    var errorMsg;

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

      return errorMsg;
    }
  }

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

    if (name === 'name' && value) {
      pathListDs.forEach(function (pathRecord) {
        return pathRecord.init('serviceName', value);
      });
    }

    if (name === 'type') {
      record.get('externalIp') && record.set('externalIp', null);
      forEach(record.getCascadeRecords('ports'), function (portRecord) {
        portRecord.get('nodePort') && portRecord.set('nodePort', null);
        portRecord.get('protocol') && portRecord.set('protocol', null);
      });
    }
  }

  function isRequired(_ref3) {
    var record = _ref3.record;
    var name = record.get('name');
    var dirty = some(record.getCascadeRecords('ports'), function (portRecord) {
      return portRecord.dirty;
    });
    return !!name || dirty;
  }

  return {
    autoCreate: true,
    autoQuery: false,
    paging: false,
    children: {
      ports: portsDs
    },
    fields: [{
      name: 'name',
      type: 'string',
      label: formatMessage({
        id: 'network.form.name'
      }),
      dynamicProps: {
        required: isRequired
      },
      validator: checkName,
      maxLength: 30
    }, {
      name: 'type',
      type: 'string',
      defaultValue: 'ClusterIP',
      label: formatMessage({
        id: 'ist.networking.service.type'
      }),
      dynamicProps: {
        required: isRequired
      }
    }, {
      name: 'externalIp',
      label: formatMessage({
        id: 'network.config.ip'
      }),
      multiple: true,
      validator: checkIP
    }],
    events: {
      update: handleUpdate
    }
  };
});