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 forEach from 'lodash/forEach';
import some from 'lodash/some';
export default (function (_ref) {
  var formatMessage = _ref.formatMessage,
      intlPrefix = _ref.intlPrefix,
      projectId = _ref.projectId,
      pathListDs = _ref.pathListDs,
      annotationDs = _ref.annotationDs;

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

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

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

              return _context.abrupt("return");

            case 3:
              envId = parentRecord.get('environmentId');

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

              return _context.abrupt("return");

            case 6:
              p = /^([a-z0-9]([-a-z0-9]?[a-z0-9])*)$/;

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

              if (!p.test(value)) {
                _context.next = 25;
                break;
              }

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

            case 12:
              res = _context.sent;

              if (!(res && !res.failed)) {
                _context.next = 17;
                break;
              }

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

            case 17:
              return _context.abrupt("return", formatMessage({
                id: 'checkNameExist'
              }));

            case 18:
              _context.next = 23;
              break;

            case 20:
              _context.prev = 20;
              _context.t0 = _context["catch"](9);
              return _context.abrupt("return", formatMessage({
                id: 'checkNameFailed'
              }));

            case 23:
              _context.next = 26;
              break;

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

            case 26:
            case "end":
              return _context.stop();
          }
        }
      }, _callee, null, [[9, 20]]);
    }));
    return _checkName.apply(this, arguments);
  }

  function checkDomain(value) {
    if (!value) {
      return;
    }

    var pattern = /^(\*\.)?[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$/;

    if (!pattern.test(value)) {
      return formatMessage({
        id: 'domain.domain.check.reg.failed'
      });
    }
  }

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

    if (name === 'domain') {
      pathListDs.forEach(function (pathRecord) {
        return pathRecord.getField('path').checkValidity();
      });
    }

    if (name === 'isNormal') {
      record.get('certId') && record.set('certId', null);
    }
  }

  function renderLookupUrl(_ref3) {
    var record = _ref3.record;
    var parentRecord = record.cascadeParent;

    if (!parentRecord) {
      return;
    }

    var envId = parentRecord.get('environmentId');
    var domain = record.get('domain');

    if (domain && !record.get('isNormal')) {
      return {
        url: "/devops/v1/projects/".concat(projectId, "/certifications/active?env_id=").concat(envId, "&domain=").concat(domain),
        method: 'post'
      };
    }
  }

  function isRequired(_ref4) {
    var record = _ref4.record;
    var hasValue = record.get('name') || record.get('domain') || record.get('certId');
    var pathDirty = some(record.getCascadeRecords('pathList'), function (pathRecord) {
      return pathRecord.dirty;
    });
    var annotationDirty = some(record.getCascadeRecords('annotations'), function (annotationRecord) {
      return annotationRecord.dirty;
    });
    return !!hasValue || pathDirty || annotationDirty;
  }

  return {
    autoCreate: true,
    autoQuery: false,
    selection: false,
    children: {
      pathList: pathListDs,
      annotations: annotationDs
    },
    fields: [{
      name: 'name',
      type: 'string',
      label: formatMessage({
        id: 'domain.column.name'
      }),
      validator: checkName,
      maxLength: 40,
      dynamicProps: {
        required: isRequired
      }
    }, {
      name: 'domain',
      type: 'string',
      label: formatMessage({
        id: 'domain.form.domain'
      }),
      validator: checkDomain,
      maxLength: 50,
      dynamicProps: {
        required: isRequired
      }
    }, {
      name: 'isNormal',
      type: 'boolean',
      defaultValue: true,
      ignore: 'always',
      label: formatMessage({
        id: 'domain.protocol.type'
      }),
      dynamicProps: {
        required: isRequired
      }
    }, {
      name: 'certId',
      type: 'string',
      textField: 'certName',
      valueField: 'id',
      label: formatMessage({
        id: 'domain.form.cert'
      }),
      dynamicProps: {
        required: function required(_ref5) {
          var record = _ref5.record;
          return isRequired({
            record: record
          }) && !record.get('isNormal');
        },
        lookupAxiosConfig: renderLookupUrl
      }
    }],
    events: {
      update: handleUpdate
    }
  };
});