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

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

import React, { useEffect, useMemo, useState } from 'react';
import { observer } from 'mobx-react-lite';
import { Spin, Button } from 'choerodon-ui/pro/lib';
var timer;
var RemoveForm = observer(function (_ref) {
  var nodeName = _ref.nodeName,
      modal = _ref.modal,
      nodeId = _ref.nodeId,
      projectId = _ref.projectId,
      roleType = _ref.roleType,
      contentStore = _ref.contentStore,
      afterOk = _ref.afterOk,
      formatMessage = _ref.formatMessage,
      intlPrefix = _ref.intlPrefix;

  var _useState = useState(''),
      _useState2 = _slicedToArray(_useState, 2),
      message = _useState2[0],
      setMessage = _useState2[1];

  var _useState3 = useState(true),
      _useState4 = _slicedToArray(_useState3, 2),
      loading = _useState4[0],
      setLoading = _useState4[1];

  useEffect(function () {
    return function () {
      if (timer) {
        clearInterval(timer);
      }
    };
  }, []);
  var successModalProps = {
    okProps: {
      color: 'red'
    },
    cancelProps: {
      color: 'dark'
    },
    onOk: handleSubmit,
    okText: '移除',
    title: formatMessage({
      id: "".concat(intlPrefix, ".node.action.removeRole")
    }),
    footer: function footer(okbtn, cancelbtn) {
      return /*#__PURE__*/React.createElement(React.Fragment, null, cancelbtn, okbtn);
    }
  };
  var failedModalProps = {
    title: '无法移除',
    onOk: function onOk() {
      return modal.close();
    },
    okText: formatMessage({
      id: 'iknow'
    }),
    footer: function footer(okbtn, cancelbtn) {
      return /*#__PURE__*/React.createElement(React.Fragment, null, okbtn);
    }
  };
  var loadingModalProps = {
    cancelProps: {
      color: 'dark'
    },
    title: formatMessage({
      id: "".concat(intlPrefix, ".node.action.removeRole")
    }),
    footer: function footer(okbtn, cancelbtn) {
      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Button, {
        color: "dark",
        disabled: true
      }, "\u53D6\u6D88"), /*#__PURE__*/React.createElement(Button, {
        color: "red",
        loading: true
      }, "\u79FB\u9664"));
    }
  };
  var loadingModalFailedProps = {
    okProps: {
      color: 'red',
      loading: false
    },
    cancelProps: {
      color: 'dark'
    },
    okText: '移除',
    onOk: handleSubmit,
    title: formatMessage({
      id: "".concat(intlPrefix, ".node.action.removeRole")
    }),
    footer: function footer(okbtn, cancelbtn) {
      return /*#__PURE__*/React.createElement(React.Fragment, null, cancelbtn, okbtn);
    }
  };

  var handleDeleteFinal = function handleDeleteFinal(id) {
    modal.update(loadingModalProps);
    timer = setInterval( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
      var res, status;
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.prev = 0;
              _context.next = 3;
              return contentStore.handleNodeIdDelele(projectId, id);

            case 3:
              res = _context.sent;

              if (!(res && res.failed)) {
                _context.next = 6;
                break;
              }

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

            case 6:
              status = res.status;

              if (res && status !== 'operating') {
                clearInterval(timer);
                modal.close();
                afterOk();
              }

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

            case 11:
              _context.prev = 11;
              _context.t0 = _context["catch"](0);
              modal.update(loadingModalFailedProps);
              return _context.abrupt("return", true);

            case 15:
            case "end":
              return _context.stop();
          }
        }
      }, _callee, null, [[0, 11]]);
    })), 2000);
  };

  function handleSubmit() {
    return _handleSubmit.apply(this, arguments);
  }

  function _handleSubmit() {
    _handleSubmit = _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 contentStore.removeRole(projectId, nodeId, roleType === 'master' ? 4 : 2);

            case 3:
              res = _context2.sent;

              if (!(res && res.failed)) {
                _context2.next = 7;
                break;
              }

              modal.update(loadingModalFailedProps);
              return _context2.abrupt("return", false);

            case 7:
              handleDeleteFinal(res);
              return _context2.abrupt("return", false);

            case 11:
              _context2.prev = 11;
              _context2.t0 = _context2["catch"](0);
              modal.update(loadingModalFailedProps);
              return _context2.abrupt("return", false);

            case 15:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2, null, [[0, 11]]);
    }));
    return _handleSubmit.apply(this, arguments);
  }

  function loadPermission() {
    return _loadPermission.apply(this, arguments);
  }

  function _loadPermission() {
    _loadPermission = _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 contentStore.getDeleteRolePemissionUrl(projectId, nodeId, roleType === 'master' ? 4 : 2);

            case 3:
              res = _context3.sent;

              if (!(res && res.failed)) {
                _context3.next = 6;
                break;
              }

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

            case 6:
              setLoading(false);

              if (res) {
                _context3.next = 11;
                break;
              }

              modal.update(failedModalProps);
              setMessage("\u96C6\u7FA4\u4E0B\u552F\u4E00\u7684".concat(roleType, "\u89D2\u8272\u65E0\u6CD5\u79FB\u9664"));
              return _context3.abrupt("return", true);

            case 11:
              setMessage("\u786E\u8BA4\u8981\u79FB\u9664\u8282\u70B9\"".concat(nodeName, "\"\u7684\u3010").concat(roleType, "\u3011\u89D2\u8272\u5417?"));
              modal.update(successModalProps);
              return _context3.abrupt("return", true);

            case 16:
              _context3.prev = 16;
              _context3.t0 = _context3["catch"](0);
              return _context3.abrupt("return", _context3.t0);

            case 19:
            case "end":
              return _context3.stop();
          }
        }
      }, _callee3, null, [[0, 16]]);
    }));
    return _loadPermission.apply(this, arguments);
  }

  useEffect(function () {
    loadPermission();
  }, []);

  if (loading) {
    return /*#__PURE__*/React.createElement(Spin, {
      spinning: true
    });
  }

  return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("p", null, message));
});
export default RemoveForm;