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 concat from 'lodash/concat';
import { handlePromptError } from "../../../../../../utils";
export default function useStore() {
  return useLocalStore(function () {
    return {
      branchData: [],

      get getBranchData() {
        return this.branchData;
      },

      setBranchData: function setBranchData(data, isFirstPage) {
        if (isFirstPage) {
          this.branchData = data;
        } else {
          this.branchData = concat(this.branchData.slice(), data);
        }
      },
      tagData: [],

      get getTagData() {
        return this.tagData;
      },

      setTagData: function setTagData(data, isFirstPage) {
        if (isFirstPage) {
          this.tagData = data;
        } else {
          this.tagData = concat(this.tagData.slice(), data);
        }
      },
      hasMoreBranch: false,

      get getHasMoreBranch() {
        return this.hasMoreBranch;
      },

      setHasMoreBranch: function setHasMoreBranch(data) {
        this.hasMoreBranch = data;
      },
      hasMoreTag: false,

      get getHasMoreTag() {
        return this.hasMoreTag;
      },

      setHasMoreTag: function setHasMoreTag(data) {
        this.hasMoreTag = data;
      },

      /**
       * 加载分支数据
       */
      loadBranchData: function loadBranchData(_ref) {
        var _this = this;

        return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
          var projectId, appServiceId, searchValue, _ref$page, page, postData, res;

          return regeneratorRuntime.wrap(function _callee$(_context) {
            while (1) {
              switch (_context.prev = _context.next) {
                case 0:
                  projectId = _ref.projectId, appServiceId = _ref.appServiceId, searchValue = _ref.searchValue, _ref$page = _ref.page, page = _ref$page === void 0 ? 1 : _ref$page;
                  _context.prev = 1;
                  postData = {
                    searchParam: {
                      branchName: searchValue
                    },
                    param: []
                  };
                  _context.next = 5;
                  return axios.post("devops/v1/projects/".concat(projectId, "/app_service/").concat(appServiceId, "/git/page_branch_by_options?page=").concat(page, "&size=5"), postData);

                case 5:
                  res = _context.sent;

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

                  _this.setBranchData(res.list, res.pageNum === 1);

                  _this.setHasMoreBranch(res.hasNextPage);

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

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

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

                case 17:
                case "end":
                  return _context.stop();
              }
            }
          }, _callee, null, [[1, 13]]);
        }))();
      },

      /**
       * 加载标记数据
       */
      loadTagData: function loadTagData(_ref2) {
        var _this2 = this;

        return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
          var projectId, appServiceId, searchValue, _ref2$page, page, postData, res;

          return regeneratorRuntime.wrap(function _callee2$(_context2) {
            while (1) {
              switch (_context2.prev = _context2.next) {
                case 0:
                  projectId = _ref2.projectId, appServiceId = _ref2.appServiceId, searchValue = _ref2.searchValue, _ref2$page = _ref2.page, page = _ref2$page === void 0 ? 1 : _ref2$page;
                  _context2.prev = 1;
                  postData = {
                    searchParam: {
                      tagName: searchValue
                    },
                    param: []
                  };
                  _context2.next = 5;
                  return axios.post("devops/v1/projects/".concat(projectId, "/app_service/").concat(appServiceId, "/git/page_tags_by_options?page=").concat(page, "&size=5"), postData);

                case 5:
                  res = _context2.sent;

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

                  _this2.setTagData(res.list, res.pageNum === 1);

                  _this2.setHasMoreTag(res.hasNextPage);

                  return _context2.abrupt("return", res);

                case 10:
                  return _context2.abrupt("return", false);

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

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