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 BaseComDeployApis from "../apis";
import { mapping } from "./baseDeployDataSet";
/**
 * key变化 设置value值
 * @param store
 * @param value
 * @param record
 */

function setValueWhenMatchKey(store, value, record) {
  var list = store.getPvLabelsList;
  var item = list.find(function (i) {
    return i.key === value;
  });

  if (item) {
    record.set('value', item.value);
  }
}

export default (function (projectId, BaseDeployDataSet, BaseComDeployStore) {
  return {
    autoCreate: true,
    fields: [{
      name: 'key',
      type: 'string',
      label: '键',
      textField: 'key',
      valueField: 'key',
      dynamicProps: {
        lookupAxiosConfig: function lookupAxiosConfig() {
          return BaseDeployDataSet.current.get(mapping.env.name) ? {
            url: BaseComDeployApis.getPvLabesUrl(projectId, BaseComDeployStore.getEnvList.find(function (i) {
              return i.id === BaseDeployDataSet.current.get(mapping.env.name);
            }).clusterId),
            method: 'get',
            transformResponse: function transformResponse(res) {
              var newRes = res;

              try {
                newRes = JSON.parse(res);
                BaseComDeployStore.setPvLabelsList(newRes);
                return newRes;
              } catch (e) {
                BaseComDeployStore.setPvLabelsList(newRes);
                return newRes;
              }
            }
          } : undefined;
        }
      }
    }, {
      name: 'value',
      type: 'string',
      label: '值'
    }],
    events: {
      update: function () {
        var _update = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(_ref) {
          var record, name, value;
          return regeneratorRuntime.wrap(function _callee$(_context) {
            while (1) {
              switch (_context.prev = _context.next) {
                case 0:
                  record = _ref.record, name = _ref.name, value = _ref.value;
                  _context.t0 = name;
                  _context.next = _context.t0 === 'key' ? 4 : 6;
                  break;

                case 4:
                  setValueWhenMatchKey(BaseComDeployStore, value, record);
                  return _context.abrupt("break", 7);

                case 6:
                  return _context.abrupt("break", 7);

                case 7:
                case "end":
                  return _context.stop();
              }
            }
          }, _callee);
        }));

        function update(_x) {
          return _update.apply(this, arguments);
        }

        return update;
      }()
    }
  };
});