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 } from '@choerodon/boot';
import { handlePromptError } from "../../../../../utils";
export default function useStore() {
  return useLocalStore(function () {
    return {
      checked: false,
      setChecked: function setChecked(value) {
        this.checked = value;
      },
      permissionUsers: [undefined],
      setPermissionUsers: function setPermissionUsers(action) {
        var type = action.type,
            value = action.value,
            index = action.index;

        switch (type) {
          case 'add':
            this.permissionUsers.push(undefined);
            break;

          case 'change':
            if (this.permissionUsers.indexOf(value) < 0) {
              this.permissionUsers[index] = value;
            } else {
              handlePromptError('您已经选择过此用户，请勿重复选择');
            }

            return;

          case 'sub':
            this.permissionUsers = this.permissionUsers.filter(function (v, k) {
              return k !== index;
            });
            return;

          default:
            throw new Error('action type not in [add,sub,change]');
        }
      },
      clearPermissionUsers: function clearPermissionUsers() {
        this.permissionUsers = [undefined];
      },
      addUsers: function addUsers(_ref) {
        var _this = this;

        return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
          var projectId, id, userIds, data, res;
          return regeneratorRuntime.wrap(function _callee$(_context) {
            while (1) {
              switch (_context.prev = _context.next) {
                case 0:
                  projectId = _ref.projectId, id = _ref.id;
                  userIds = _this.permissionUsers;

                  if (!userIds[userIds.length - 1]) {
                    userIds = userIds.slice(0, userIds.length - 1);
                  }

                  data = {
                    userIds: userIds,
                    skipCheckPermission: _this.checked
                  };
                  _context.next = 6;
                  return axios.post("/devops/v1/projects/".concat(projectId, "/app_service/").concat(id, "/update_permission"), JSON.stringify(data));

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

                case 8:
                case "end":
                  return _context.stop();
              }
            }
          }, _callee);
        }))();
      }
    };
  });
}