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

/* eslint-disable */
import { useLocalStore } from 'mobx-react-lite';
import { axios, Choerodon } from '@choerodon/boot';
import map from 'lodash/map';
import { handlePromptError } from "../../../utils";
import getTablePostData from "../../../utils/getTablePostData";
export default function useStore() {
  return useLocalStore(function () {
    return {
      appServiceId: null,
      setAppServiceId: function setAppServiceId(data) {
        this.appServiceId = data;
      },

      get getAppServiceId() {
        return this.appServiceId;
      },

      appService: [],
      setAppService: function setAppService(data) {
        this.appService = data;
      },

      get getAppService() {
        return this.appService.slice();
      },

      version: [],
      setVersion: function setVersion(data) {
        this.version = data;
      },

      get getVersion() {
        return this.version.slice();
      },

      projectRole: '',
      setProjectRole: function setProjectRole(flag) {
        this.projectRole = flag ? 'owner' : 'member';
      },

      get getProjectRole() {
        return this.projectRole;
      },

      allProject: [],
      setAllProject: function setAllProject(data) {
        this.allProject = data;
      },

      get getAllProject() {
        return this.allProject.slice();
      },

      loadAppById: function loadAppById(projectId, id) {
        return axios.get("/devops/v1/projects/".concat(projectId, "/app_service/").concat(id));
      },
      checkChart: function checkChart(projectId, url) {
        return axios.get("/devops/v1/projects/".concat(projectId, "/app_service/check_chart?url=").concat(url));
      },
      checkHarbor: function checkHarbor(projectId, postData) {
        var url = '';
        map(postData, function (value, key) {
          value && (url = "".concat(url, "&").concat(key, "=").concat(value));
        });
        return axios.get("/devops/v1/projects/".concat(projectId, "/app_service/check_harbor?").concat(url.substr(1)));
      },
      changeActive: function changeActive(projectId, id, active) {
        return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
          var res;
          return regeneratorRuntime.wrap(function _callee$(_context) {
            while (1) {
              switch (_context.prev = _context.next) {
                case 0:
                  _context.prev = 0;
                  _context.next = 3;
                  return axios.put("/devops/v1/projects/".concat(projectId, "/app_service/").concat(id, "?active=").concat(active));

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

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

                case 10:
                case "end":
                  return _context.stop();
              }
            }
          }, _callee, null, [[0, 7]]);
        }))();
      },
      checkAppService: function checkAppService(projectId, id) {
        return axios.get("/devops/v1/projects/".concat(projectId, "/app_service/check/").concat(id, "?active=true"));
      },
      batchCheck: function batchCheck(projectId, listCode, listName) {
        return axios.post("/devops/v1/projects/".concat(projectId, "/app_service/batch_check"), JSON.stringify({
          listCode: listCode,
          listName: listName
        }));
      },
      loadShareById: function loadShareById(projectId, id) {
        return axios.get("/devops/v1/projects/".concat(projectId, "/app_service_share/").concat(id));
      },
      loadAppService: function loadAppService(projectId, type) {
        var _this = 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/list_all_app_services?deploy_only=false&type=").concat(type));

                case 3:
                  res = _context2.sent;

                  if (handlePromptError(res)) {
                    _this.setAppService(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]]);
        }))();
      },
      loadVersion: function loadVersion(projectId, id) {
        var _this2 = this;

        return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() {
          var res;
          return regeneratorRuntime.wrap(function _callee3$(_context3) {
            while (1) {
              switch (_context3.prev = _context3.next) {
                case 0:
                  _context3.prev = 0;
                  _context3.next = 3;
                  return axios.post("/devops/v1/projects/".concat(projectId, "/app_service_versions/page_by_options?app_service_id=").concat(id, "&deploy_only=false&do_page=false"));

                case 3:
                  res = _context3.sent;

                  if (!handlePromptError(res)) {
                    _context3.next = 7;
                    break;
                  }

                  _this2.setVersion(res.list);

                  return _context3.abrupt("return", res.list);

                case 7:
                  _context3.next = 12;
                  break;

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

                case 12:
                case "end":
                  return _context3.stop();
              }
            }
          }, _callee3, null, [[0, 9]]);
        }))();
      },
      judgeRole: function judgeRole(organizationId, projectId) {
        var _this3 = this;

        return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() {
          var data, res, _ref, approve;

          return regeneratorRuntime.wrap(function _callee4$(_context4) {
            while (1) {
              switch (_context4.prev = _context4.next) {
                case 0:
                  data = ['choerodon.code.project.develop.app-service.ps.create'];
                  _context4.prev = 1;
                  _context4.next = 4;
                  return axios({
                    method: 'post',
                    url: '/iam/choerodon/v1/permissions/menus/check-permissions',
                    data: data,
                    params: {
                      tenantId: organizationId,
                      projectId: projectId
                    }
                  });

                case 4:
                  res = _context4.sent;

                  if (handlePromptError(res)) {
                    _ref = res[0] || {}, approve = _ref.approve;

                    _this3.setProjectRole(approve);
                  }

                  _context4.next = 11;
                  break;

                case 8:
                  _context4.prev = 8;
                  _context4.t0 = _context4["catch"](1);
                  Choerodon.handleResponseError(_context4.t0);

                case 11:
                case "end":
                  return _context4.stop();
              }
            }
          }, _callee4, null, [[1, 8]]);
        }))();
      },
      updatePermission: function updatePermission(projectId, id, data) {
        return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5() {
          var res;
          return regeneratorRuntime.wrap(function _callee5$(_context5) {
            while (1) {
              switch (_context5.prev = _context5.next) {
                case 0:
                  _context5.prev = 0;
                  _context5.next = 3;
                  return axios.post("/devops/v1/projects/".concat(projectId, "/app_service/").concat(id, "/update_permission"), JSON.stringify(data));

                case 3:
                  res = _context5.sent;
                  return _context5.abrupt("return", handlePromptError(res, false));

                case 7:
                  _context5.prev = 7;
                  _context5.t0 = _context5["catch"](0);
                  return _context5.abrupt("return", false);

                case 10:
                case "end":
                  return _context5.stop();
              }
            }
          }, _callee5, null, [[0, 7]]);
        }))();
      },
      loadAllProject: function loadAllProject(projectId, isShare) {
        var _this4 = this;

        return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee6() {
          var res;
          return regeneratorRuntime.wrap(function _callee6$(_context6) {
            while (1) {
              switch (_context6.prev = _context6.next) {
                case 0:
                  _context6.prev = 0;
                  _context6.next = 3;
                  return axios.get("/devops/v1/projects/".concat(projectId, "/app_service/list_project_by_share?share=").concat(isShare));

                case 3:
                  res = _context6.sent;

                  if (handlePromptError(res)) {
                    _this4.setAllProject(res);
                  }

                  _context6.next = 10;
                  break;

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

                case 10:
                case "end":
                  return _context6.stop();
              }
            }
          }, _callee6, null, [[0, 7]]);
        }))();
      },
      loading: true,
      setLoading: function setLoading(data) {
        this.loading = data;
      },

      get getLoading() {
        return this.loading;
      },

      hasApp: false,
      setHasApp: function setHasApp(data) {
        this.hasApp = data;
      },

      get getHasApp() {
        return this.hasApp;
      },

      checkHasApp: function checkHasApp(projectId) {
        var _this5 = this;

        return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee7() {
          var postData, res, hasData;
          return regeneratorRuntime.wrap(function _callee7$(_context7) {
            while (1) {
              switch (_context7.prev = _context7.next) {
                case 0:
                  _this5.setLoading(true);

                  postData = getTablePostData();
                  _context7.prev = 2;
                  _context7.next = 5;
                  return axios.post("/devops/v1/projects/".concat(projectId, "/app_service/page_by_options?checkMember=true"), JSON.stringify(postData));

                case 5:
                  res = _context7.sent;

                  if (res && !res.failed) {
                    hasData = res.list && res.list.length;

                    _this5.setHasApp(hasData);
                  }

                  _this5.setLoading(false);

                  _context7.next = 14;
                  break;

                case 10:
                  _context7.prev = 10;
                  _context7.t0 = _context7["catch"](2);

                  _this5.setLoading(false);

                  Choerodon.handleResponseError(_context7.t0);

                case 14:
                case "end":
                  return _context7.stop();
              }
            }
          }, _callee7, null, [[2, 10]]);
        }))();
      }
    };
  });
}