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 _checkPermission from "../../../../../../utils/checkPermission";
import { handlePromptError } from "../../../../../../utils";
export default function useStore(_ref) {
  var defaultTab = _ref.defaultTab;
  return useLocalStore(function () {
    return {
      tabKey: defaultTab,
      setTabKey: function setTabKey(data) {
        this.tabKey = data;
      },

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

      hasPermission: false,
      setPermission: function setPermission(data) {
        this.hasPermission = data;
      },

      get getPermission() {
        return this.hasPermission;
      },

      value: '',
      setValue: function setValue(value) {
        this.value = value;
      },

      get getValue() {
        return this.value;
      },

      hasInstance: false,
      setHasInstance: function setHasInstance(data) {
        this.hasInstance = data;
      },

      get getHasInstance() {
        return this.hasInstance;
      },

      polarisLoading: true,
      setPolarisLoading: function setPolarisLoading(flag) {
        this.polarisLoading = flag;
      },

      get getPolarisLoading() {
        return this.polarisLoading;
      },

      checkPermission: function checkPermission(_ref2) {
        var _this = this;

        return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
          var projectId, organizationId, resourceType, res;
          return regeneratorRuntime.wrap(function _callee$(_context) {
            while (1) {
              switch (_context.prev = _context.next) {
                case 0:
                  projectId = _ref2.projectId, organizationId = _ref2.organizationId, resourceType = _ref2.resourceType;
                  _context.next = 3;
                  return _checkPermission({
                    code: 'choerodon.code.project.deploy.app-deployment.resource.ps.permission',
                    organizationId: organizationId,
                    projectId: projectId,
                    resourceType: resourceType
                  });

                case 3:
                  res = _context.sent;

                  _this.setPermission(res);

                case 5:
                case "end":
                  return _context.stop();
              }
            }
          }, _callee);
        }))();
      },
      loadValue: function loadValue(projectId, id) {
        var _this2 = this;

        return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
          var res;
          return regeneratorRuntime.wrap(function _callee2$(_context2) {
            while (1) {
              switch (_context2.prev = _context2.next) {
                case 0:
                  _context2.prev = 0;
                  _context2.next = 3;
                  return axios.get("/devops/v1/projects/".concat(projectId, "/app_service_versions/value?app_service_id=").concat(id));

                case 3:
                  res = _context2.sent;

                  if (handlePromptError(res)) {
                    _this2.setValue(res);
                  }

                  _context2.next = 10;
                  break;

                case 7:
                  _context2.prev = 7;
                  _context2.t0 = _context2["catch"](0);
                  Choerodon.handleResponseError(_context2.t0);

                case 10:
                case "end":
                  return _context2.stop();
              }
            }
          }, _callee2, null, [[0, 7]]);
        }))();
      },
      checkDelete: function checkDelete(projectId, id) {
        return axios.get("/devops/v1/projects/".concat(projectId, "/deploy_value/check_delete?value_id=").concat(id));
      },
      deleteRecord: function deleteRecord(projectId, id) {
        return axios["delete"]("/devops/v1/projects/".concat(projectId, "/deploy_value?value_id=").concat(id));
      },
      checkHasInstance: function checkHasInstance(projectId, envId) {
        var _this3 = this;

        return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() {
          var res, result;
          return regeneratorRuntime.wrap(function _callee3$(_context3) {
            while (1) {
              switch (_context3.prev = _context3.next) {
                case 0:
                  _context3.prev = 0;

                  _this3.setPolarisLoading(true);

                  _context3.next = 4;
                  return axios.get("devops/v1/projects/".concat(projectId, "/app_service_instances/count_by_options?env_id=").concat(envId, "&status=&app_service_id="));

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

                  _this3.setHasInstance(result);

                  _this3.setPolarisLoading(false);

                  return _context3.abrupt("return", result);

                case 11:
                  _context3.prev = 11;
                  _context3.t0 = _context3["catch"](0);
                  Choerodon.handleResponseError(_context3.t0);

                  _this3.setPolarisLoading(false);

                  return _context3.abrupt("return", false);

                case 16:
                case "end":
                  return _context3.stop();
              }
            }
          }, _callee3, null, [[0, 11]]);
        }))();
      },
      ManualScan: function ManualScan(projectId, envId) {
        return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() {
          var res, result;
          return regeneratorRuntime.wrap(function _callee4$(_context4) {
            while (1) {
              switch (_context4.prev = _context4.next) {
                case 0:
                  _context4.prev = 0;
                  _context4.next = 3;
                  return axios.post("/devops/v1/projects/".concat(projectId, "/polaris/envs/").concat(envId));

                case 3:
                  res = _context4.sent;
                  result = handlePromptError(res);
                  return _context4.abrupt("return", result);

                case 8:
                  _context4.prev = 8;
                  _context4.t0 = _context4["catch"](0);
                  Choerodon.handleResponseError(_context4.t0);
                  return _context4.abrupt("return", false);

                case 12:
                case "end":
                  return _context4.stop();
              }
            }
          }, _callee4, null, [[0, 8]]);
        }))();
      }
    };
  });
}