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 { handlePromptError } from "../../../../../../../../utils";
export default (function (_ref) {
  var title = _ref.title,
      id = _ref.id,
      formatMessage = _ref.formatMessage,
      projectId = _ref.projectId,
      envId = _ref.envId,
      store = _ref.store;

  var checkName = /*#__PURE__*/function () {
    var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(value, name, record) {
      var pattern, res;
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              pattern = /^[a-z]([-a-z0-9]*[a-z0-9])?$/;

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

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

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

              _context.prev = 6;
              _context.next = 9;
              return store.checkName(projectId, envId, value);

            case 9:
              res = _context.sent;

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

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

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

            case 15:
              _context.next = 20;
              break;

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

            case 20:
              _context.next = 23;
              break;

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

            case 23:
            case "end":
              return _context.stop();
          }
        }
      }, _callee, null, [[6, 17]]);
    }));

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

  return {
    autoCreate: typeof id !== 'number',
    // autoQuery: typeof id === 'number',
    transport: {
      read: function read() {
        return {
          url: title === 'mapping' ? "/devops/v1/projects/".concat(projectId, "/config_maps/").concat(id) : "/devops/v1/projects/".concat(projectId, "/secret/").concat(id, "?to_decode=true"),
          method: 'GET'
        };
      }
    },
    fields: [{
      name: 'name',
      type: 'string',
      label: formatMessage({
        id: 'app.name'
      }),
      required: true,
      validator: checkName,
      maxLength: 100
    }, {
      name: 'description',
      type: 'string',
      label: formatMessage({
        id: 'configMap.des'
      }),
      maxLength: 30
    }]
  };
});