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 { useLocalStore } from 'mobx-react-lite';
import { axios, Choerodon } from '@choerodon/boot';
import { handlePromptError } from "../../../../../../utils";
export default function useStore(_ref) {
  var defaultKey = _ref.defaultKey;
  return useLocalStore(function () {
    return {
      tabKey: defaultKey || 'cases',
      detailLoading: false,
      detail: {},
      valueLoading: true,
      upgradeValue: {},
      setTabKey: function setTabKey(data) {
        this.tabKey = data;
      },

      get getTabKey() {
        return this.tabKey;
      },

      setUpgradeValue: function setUpgradeValue(value) {
        this.upgradeValue = value;
      },

      get getUpgradeValue() {
        return this.upgradeValue;
      },

      setValueLoading: function setValueLoading(data) {
        this.valueLoading = data;
      },

      get getValueLoading() {
        return this.valueLoading;
      },

      redeploy: function redeploy(projectId, id) {
        return axios.put("/devops/v1/projects/".concat(projectId, "/app_service_instances/").concat(id, "/restart"));
      },
      upgrade: function upgrade(projectId, data, isMarket, isMiddleware) {
        var url = isMiddleware ? "/devops/v1/projects/".concat(projectId, "/middleware/redis/").concat(data.instanceId) : "/devops/v1/projects/".concat(projectId, "/app_service_instances").concat(isMarket ? "/market/instances/".concat(data.instanceId) : '');
        return axios.put(url, JSON.stringify(data));
      },
      loadValue: function loadValue(projectId, id, versionId) {
        var _arguments = arguments,
            _this = this;

        return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
          var isMarket, url, data, result;
          return regeneratorRuntime.wrap(function _callee$(_context) {
            while (1) {
              switch (_context.prev = _context.next) {
                case 0:
                  isMarket = _arguments.length > 3 && _arguments[3] !== undefined ? _arguments[3] : false;

                  _this.setValueLoading(true);

                  _context.prev = 2;
                  url = isMarket ? "/devops/v1/projects/".concat(projectId, "/app_service_instances/").concat(id, "/upgrade_value?market_deploy_object_id=").concat(versionId) : "/devops/v1/projects/".concat(projectId, "/app_service_instances/").concat(id, "/appServiceVersion/").concat(versionId, "/upgrade_value");
                  _context.next = 6;
                  return axios.get(url);

                case 6:
                  data = _context.sent;
                  result = handlePromptError(data);

                  _this.setValueLoading(false);

                  if (!result) {
                    _context.next = 12;
                    break;
                  }

                  _this.setUpgradeValue(data);

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

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

                case 15:
                  _context.prev = 15;
                  _context.t0 = _context["catch"](2);

                  _this.setValueLoading(false);

                  Choerodon.handleResponseError(_context.t0);
                  return _context.abrupt("return", false);

                case 20:
                case "end":
                  return _context.stop();
              }
            }
          }, _callee, null, [[2, 15]]);
        }))();
      },
      loadUpVersion: function loadUpVersion(_ref2) {
        var projectId = _ref2.projectId,
            appId = _ref2.appId,
            page = _ref2.page,
            _ref2$param = _ref2.param,
            param = _ref2$param === void 0 ? '' : _ref2$param,
            _ref2$init = _ref2.init,
            init = _ref2$init === void 0 ? '' : _ref2$init;
        var url = '';

        if (param) {
          url = "&version=".concat(param);
        }

        return axios.post("/devops/v1/projects/".concat(projectId, "/app_service_versions/page_by_options?app_service_id=").concat(appId, "&deploy_only=true&do_page=true&page=").concat(page, "&size=15&app_service_version_id=").concat(init).concat(url));
      }
    };
  });
}