var _class, _descriptor, _descriptor2, _descriptor3, _descriptor4, _descriptor5;

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

function _initializerDefineProperty(target, property, descriptor, context) { if (!descriptor) return; Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 }); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object.keys(descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object.defineProperty(target, property, desc); desc = null; } return desc; }

function _initializerWarningHelper(descriptor, context) { throw new Error('Decorating class property failed. Please ensure that ' + 'proposal-class-properties is enabled and runs after the decorators transform.'); }

import { observable, action, computed } from 'mobx';
import { axios, Choerodon } from '@choerodon/boot';
import { handlePromptError } from "../../../../../../utils";
var InstanceDetails = (_class = /*#__PURE__*/function () {
  function InstanceDetails() {
    _classCallCheck(this, InstanceDetails);

    _initializerDefineProperty(this, "resources", _descriptor, this);

    _initializerDefineProperty(this, "loading", _descriptor2, this);

    _initializerDefineProperty(this, "deployments", _descriptor3, this);

    _initializerDefineProperty(this, "deploymentsYaml", _descriptor4, this);

    _initializerDefineProperty(this, "targetCount", _descriptor5, this);
  }

  _createClass(InstanceDetails, [{
    key: "setTargetCount",
    value: function setTargetCount(count) {
      this.targetCount = count;
    }
  }, {
    key: "getTargetCount",
    get: function get() {
      return this.targetCount;
    }
  }, {
    key: "setResources",
    value: function setResources(data) {
      this.resources = data;
    }
  }, {
    key: "getResources",
    get: function get() {
      return this.resources;
    }
  }, {
    key: "setLoading",
    value: function setLoading(data) {
      this.loading = data;
    }
  }, {
    key: "getLoading",
    get: function get() {
      return this.loading;
    }
  }, {
    key: "setDeployments",
    value: function setDeployments(data) {
      this.deployments = data;
    }
  }, {
    key: "getDeployments",
    get: function get() {
      return this.deployments;
    }
  }, {
    key: "setDeploymentsYaml",
    value: function setDeploymentsYaml(data) {
      this.deploymentsYaml = data;
    }
  }, {
    key: "getDeploymentsYaml",
    get: function get() {
      return this.deploymentsYaml;
    }
    /**
     * 根据实例id获取更多部署详情(Json格式)
     * @param type
     * @param project
     * @param instance
     * @param name
     */

  }, {
    key: "loadDeploymentsJson",
    value: function () {
      var _loadDeploymentsJson = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(type, project, instance, name) {
        var URL_TYPE, data, res;
        return regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                URL_TYPE = {
                  deploymentVOS: "deployment_detail_json?deployment_name=".concat(name),
                  statefulSetVOS: "stateful_set_detail_json?stateful_set_name=".concat(name),
                  daemonSetVOS: "daemon_set_detail_json?daemon_set_name=".concat(name)
                };
                _context.prev = 1;
                _context.next = 4;
                return axios.get("devops/v1/projects/".concat(project, "/app_service_instances/").concat(instance, "/").concat(URL_TYPE[type]));

              case 4:
                data = _context.sent;
                res = handlePromptError(data);

                if (!res) {
                  _context.next = 9;
                  break;
                }

                this.setDeployments(data);
                return _context.abrupt("return", true);

              case 9:
                return _context.abrupt("return", false);

              case 12:
                _context.prev = 12;
                _context.t0 = _context["catch"](1);
                Choerodon.handleResponseError(_context.t0);
                return _context.abrupt("return", false);

              case 16:
              case "end":
                return _context.stop();
            }
          }
        }, _callee, this, [[1, 12]]);
      }));

      function loadDeploymentsJson(_x, _x2, _x3, _x4) {
        return _loadDeploymentsJson.apply(this, arguments);
      }

      return loadDeploymentsJson;
    }()
    /**
     * 根据实例id获取更多部署详情(Yaml格式)
     * @param type
     * @param project
     * @param instance
     * @param name
     */

  }, {
    key: "loadDeploymentsYaml",
    value: function () {
      var _loadDeploymentsYaml = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(type, project, instance, name) {
        var URL_TYPE, data, res;
        return regeneratorRuntime.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                URL_TYPE = {
                  deploymentVOS: "deployment_detail_yaml?deployment_name=".concat(name),
                  statefulSetVOS: "stateful_set_detail_yaml?stateful_set_name=".concat(name),
                  daemonSetVOS: "daemon_set_detail_yaml?daemon_set_name=".concat(name)
                };
                _context2.prev = 1;
                _context2.next = 4;
                return axios.get("devops/v1/projects/".concat(project, "/app_service_instances/").concat(instance, "/").concat(URL_TYPE[type]));

              case 4:
                data = _context2.sent;
                res = handlePromptError(data);

                if (!res) {
                  _context2.next = 9;
                  break;
                }

                this.setDeploymentsYaml(data.detail || '');
                return _context2.abrupt("return", true);

              case 9:
                this.setDeploymentsYaml('');
                return _context2.abrupt("return", false);

              case 13:
                _context2.prev = 13;
                _context2.t0 = _context2["catch"](1);
                this.setDeploymentsYaml('');
                Choerodon.handleResponseError(_context2.t0);
                return _context2.abrupt("return", false);

              case 18:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2, this, [[1, 13]]);
      }));

      function loadDeploymentsYaml(_x5, _x6, _x7, _x8) {
        return _loadDeploymentsYaml.apply(this, arguments);
      }

      return loadDeploymentsYaml;
    }()
  }, {
    key: "operatePodCount",
    value: function operatePodCount(projectId, envId, name, num) {
      return axios.put("devops/v1/projects/".concat(projectId, "/app_service_instances/operate_pod_count?envId=").concat(envId, "&name=").concat(name, "&count=").concat(num));
    }
  }, {
    key: "loadResource",
    value: function () {
      var _loadResource = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(projectId, instanceId) {
        var data, res;
        return regeneratorRuntime.wrap(function _callee3$(_context3) {
          while (1) {
            switch (_context3.prev = _context3.next) {
              case 0:
                this.setLoading(true);
                _context3.prev = 1;
                _context3.next = 4;
                return axios.get("/devops/v1/projects/".concat(projectId, "/app_service_instances/").concat(instanceId, "/resources"));

              case 4:
                data = _context3.sent;
                res = handlePromptError(data);

                if (res) {
                  this.setResources(data);
                }

                this.setLoading(false);
                _context3.next = 13;
                break;

              case 10:
                _context3.prev = 10;
                _context3.t0 = _context3["catch"](1);
                this.setLoading(false);

              case 13:
              case "end":
                return _context3.stop();
            }
          }
        }, _callee3, this, [[1, 10]]);
      }));

      function loadResource(_x9, _x10) {
        return _loadResource.apply(this, arguments);
      }

      return loadResource;
    }()
  }]);

  return InstanceDetails;
}(), (_descriptor = _applyDecoratedDescriptor(_class.prototype, "resources", [observable], {
  configurable: true,
  enumerable: true,
  writable: true,
  initializer: function initializer() {
    return {};
  }
}), _descriptor2 = _applyDecoratedDescriptor(_class.prototype, "loading", [observable], {
  configurable: true,
  enumerable: true,
  writable: true,
  initializer: function initializer() {
    return true;
  }
}), _descriptor3 = _applyDecoratedDescriptor(_class.prototype, "deployments", [observable], {
  configurable: true,
  enumerable: true,
  writable: true,
  initializer: function initializer() {
    return {};
  }
}), _descriptor4 = _applyDecoratedDescriptor(_class.prototype, "deploymentsYaml", [observable], {
  configurable: true,
  enumerable: true,
  writable: true,
  initializer: function initializer() {
    return '';
  }
}), _descriptor5 = _applyDecoratedDescriptor(_class.prototype, "targetCount", [observable], {
  configurable: true,
  enumerable: true,
  writable: true,
  initializer: function initializer() {
    return {};
  }
}), _applyDecoratedDescriptor(_class.prototype, "setTargetCount", [action], Object.getOwnPropertyDescriptor(_class.prototype, "setTargetCount"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "getTargetCount", [computed], Object.getOwnPropertyDescriptor(_class.prototype, "getTargetCount"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "setResources", [action], Object.getOwnPropertyDescriptor(_class.prototype, "setResources"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "getResources", [computed], Object.getOwnPropertyDescriptor(_class.prototype, "getResources"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "setLoading", [action], Object.getOwnPropertyDescriptor(_class.prototype, "setLoading"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "getLoading", [computed], Object.getOwnPropertyDescriptor(_class.prototype, "getLoading"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "setDeployments", [action], Object.getOwnPropertyDescriptor(_class.prototype, "setDeployments"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "getDeployments", [computed], Object.getOwnPropertyDescriptor(_class.prototype, "getDeployments"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "setDeploymentsYaml", [action], Object.getOwnPropertyDescriptor(_class.prototype, "setDeploymentsYaml"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "getDeploymentsYaml", [computed], Object.getOwnPropertyDescriptor(_class.prototype, "getDeploymentsYaml"), _class.prototype)), _class);
export { InstanceDetails as default };