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 paramSettingDataSet from "./paramSettingDataSet";
import { mapping as baseMapping } from "./baseDeployDataSet";
var mapping = {
  checked: {
    name: 'checked',
    type: 'boolean'
  },
  hostId: {
    name: 'hostId',
    type: 'string'
  },
  status: {
    name: 'status',
    type: 'string'
  },
  hostName: {
    name: 'hostName',
    type: 'string',
    label: '主机名称',
    textField: 'name',
    valueField: 'id'
  },
  ip: {
    name: 'ip',
    type: 'string',
    label: '外部SSH认证IP',
    disabled: true
  },
  port: {
    name: 'port',
    type: 'string',
    label: '外部SSH认证端口',
    disabled: true
  },
  privateIp: {
    name: 'privateIp',
    type: 'string',
    label: '内部SSH认证IP',
    disabled: true
  },
  privatePort: {
    name: 'privatePort',
    type: 'number',
    label: '内部SSH认证端口',
    disabled: true
  }
};
export { mapping };
export default (function (projectId, BaseComDeployStore, random) {
  return {
    paging: false,
    autoCreate: true,
    fields: Object.keys(mapping).map(function (key) {
      var item = mapping[key];

      if (key === 'hostName') {
        item.lookupAxiosConfig = function () {
          return {
            url: BaseComDeployApis.getHostListApi(projectId, random),
            method: 'post',
            data: {
              params: [],
              searchParam: {
                type: 'deploy'
              }
            },
            transformResponse: function transformResponse(res) {
              var newRes = res;

              try {
                newRes = JSON.parse(newRes);
                BaseComDeployStore.setHostList(newRes.content);
                return newRes.content;
              } catch (e) {
                BaseComDeployStore.setHostList(newRes);
                return newRes;
              }
            }
          };
        };
      }

      return item;
    }),
    events: {
      update: function () {
        var _update = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(_ref) {
          var dataSet, record, name, value, item;
          return regeneratorRuntime.wrap(function _callee$(_context) {
            while (1) {
              switch (_context.prev = _context.next) {
                case 0:
                  dataSet = _ref.dataSet, record = _ref.record, name = _ref.name, value = _ref.value;
                  _context.t0 = name;
                  _context.next = _context.t0 === mapping.hostName.name ? 4 : 7;
                  break;

                case 4:
                  item = BaseComDeployStore.getHostList.find(function (i) {
                    return i.id === value;
                  });

                  if (item) {
                    record.set(mapping.ip.name, item.hostIp);
                    record.set(mapping.port.name, item.sshPort);
                    record.set(mapping.hostId.name, item.id);
                    record.set(mapping.privateIp.name, item.privateIp);
                    record.set(mapping.privatePort.name, item.privatePort);
                  } else {
                    record.set(mapping.ip.name, '');
                    record.set(mapping.port.name, '');
                    record.set(mapping.hostId.name, '');
                    record.set(mapping.privateIp.name, '');
                    record.set(mapping.privatePort.name, '');
                  }

                  return _context.abrupt("break", 8);

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

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

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

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