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); }); }; }

/* eslint-disable */
import { axios } from '@choerodon/boot';
import includes from 'lodash/includes';
export default (function (_ref) {
  var intlPrefix = _ref.intlPrefix,
      formatMessage = _ref.formatMessage,
      projectId = _ref.projectId,
      importStore = _ref.importStore;

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

    if (name === 'name' || name === 'code') {
      dataSet.forEach(function (eachRecord) {
        if (record.id !== eachRecord.id) {
          eachRecord.getField(name).checkValidity();
        }
      });
    }
  }

  function checkCode(_x, _x2, _x3) {
    return _checkCode.apply(this, arguments);
  }

  function _checkCode() {
    _checkCode = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(value, name, record) {
      var pa, dataSet, repeatRecord, _ref4, listCode, res;

      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              pa = /^[a-z]([-a-z0-9]*[a-z0-9])?$/;

              if (!(value && pa.test(value))) {
                _context.next = 23;
                break;
              }

              dataSet = record.dataSet;
              repeatRecord = dataSet.find(function (eachRecord) {
                return eachRecord.id !== record.id && eachRecord.get('code') === value;
              });
              _ref4 = importStore.getRepeatData || {}, listCode = _ref4.listCode;

              if (!repeatRecord) {
                _context.next = 7;
                break;
              }

              return _context.abrupt("return", formatMessage({
                id: 'checkCodeExist'
              }));

            case 7:
              if (!includes(listCode, value)) {
                _context.next = 9;
                break;
              }

              return _context.abrupt("return", formatMessage({
                id: 'checkCodeExist'
              }));

            case 9:
              if (importStore.getSkipCheck) {
                _context.next = 21;
                break;
              }

              _context.prev = 10;
              _context.next = 13;
              return axios.get("/devops/v1/projects/".concat(projectId, "/app_service/check_code?code=").concat(value));

            case 13:
              res = _context.sent;

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

              return _context.abrupt("return", formatMessage({
                id: 'checkCodeExist'
              }));

            case 16:
              _context.next = 21;
              break;

            case 18:
              _context.prev = 18;
              _context.t0 = _context["catch"](10);
              return _context.abrupt("return", formatMessage({
                id: 'checkCodeFailed'
              }));

            case 21:
              _context.next = 24;
              break;

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

            case 24:
            case "end":
              return _context.stop();
          }
        }
      }, _callee, null, [[10, 18]]);
    }));
    return _checkCode.apply(this, arguments);
  }

  function checkName(_x4, _x5, _x6) {
    return _checkName.apply(this, arguments);
  }

  function _checkName() {
    _checkName = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(value, name, record) {
      var pa, _ref5, listName, dataSet, repeatRecord, res;

      return regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              pa = /^\S+$/;

              if (!(value && pa.test(value))) {
                _context2.next = 23;
                break;
              }

              _ref5 = importStore.getRepeatData || {}, listName = _ref5.listName;
              dataSet = record.dataSet;
              repeatRecord = dataSet.find(function (eachRecord) {
                return eachRecord.id !== record.id && eachRecord.get('name') === value;
              });

              if (!repeatRecord) {
                _context2.next = 7;
                break;
              }

              return _context2.abrupt("return", formatMessage({
                id: 'checkNameExist'
              }));

            case 7:
              if (!includes(listName, value)) {
                _context2.next = 9;
                break;
              }

              return _context2.abrupt("return", formatMessage({
                id: 'checkNameExist'
              }));

            case 9:
              if (importStore.getSkipCheck) {
                _context2.next = 21;
                break;
              }

              _context2.prev = 10;
              _context2.next = 13;
              return axios.get("/devops/v1/projects/".concat(projectId, "/app_service/check_name?name=").concat(encodeURIComponent(value)));

            case 13:
              res = _context2.sent;

              if (!(res && res.failed || !res)) {
                _context2.next = 16;
                break;
              }

              return _context2.abrupt("return", formatMessage({
                id: 'checkNameExist'
              }));

            case 16:
              _context2.next = 21;
              break;

            case 18:
              _context2.prev = 18;
              _context2.t0 = _context2["catch"](10);
              return _context2.abrupt("return", formatMessage({
                id: "".concat(intlPrefix, ".name.failed")
              }));

            case 21:
              _context2.next = 24;
              break;

            case 23:
              return _context2.abrupt("return", formatMessage({
                id: 'nameCanNotHasSpaces'
              }));

            case 24:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2, null, [[10, 18]]);
    }));
    return _checkName.apply(this, arguments);
  }

  function getLookUpConfig(_ref3) {
    var record = _ref3.record;
    return {
      url: "/devops/v1/projects/".concat(projectId, "/app_service_versions/page_by_options?app_service_id=").concat(record.get('id'), "&deploy_only=false&do_page=true&page=1&size=40"),
      method: 'post'
    };
  }

  return {
    autoQuery: false,
    selection: false,
    paging: false,
    transport: {},
    fields: [{
      name: 'id',
      type: 'string'
    }, {
      name: 'name',
      type: 'string',
      validator: checkName,
      maxLength: 40,
      label: formatMessage({
        id: "".concat(intlPrefix, ".name")
      })
    }, {
      name: 'code',
      type: 'string',
      validator: checkCode,
      maxLength: 30,
      label: formatMessage({
        id: "".concat(intlPrefix, ".code")
      })
    }, {
      name: 'type',
      type: 'string',
      label: formatMessage({
        id: "".concat(intlPrefix, ".type")
      })
    }, {
      name: 'projectName',
      type: 'string',
      label: formatMessage({
        id: "".concat(intlPrefix, ".project")
      })
    }, {
      name: 'share',
      type: 'boolean',
      label: formatMessage({
        id: "".concat(intlPrefix, ".source")
      })
    }, {
      name: 'versionId',
      type: 'string',
      textField: 'version',
      valueField: 'id',
      dynamicProps: {
        lookupAxiosConfig: getLookUpConfig
      },
      label: formatMessage({
        id: "".concat(intlPrefix, ".version")
      }),
      required: true
    }],
    events: {
      update: handleUpdate
    }
  };
});