import "choerodon-ui/pro/lib/spin/style";
import _Spin from "choerodon-ui/pro/lib/spin";

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, { useCallback, useEffect, useState } from 'react';
import apis from "../../../../apis";

var DeleteCheck = function DeleteCheck(props) {
  var modal = props.modal,
      projectId = props.projectId,
      hostId = props.hostId,
      handleDelete = props.handleDelete,
      formatMessage = props.formatMessage,
      hostType = props.hostType;

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

  var _useState3 = useState(''),
      _useState4 = _slicedToArray(_useState3, 2),
      text = _useState4[0],
      setText = _useState4[1];

  var checkNow = useCallback( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
    var res;
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            modal.update({
              title: '正在校验主机...'
            });
            _context.prev = 1;
            _context.next = 4;
            return apis.checkHostDeletable(projectId, hostId, hostType);

          case 4:
            res = _context.sent;
            setLoading(false);

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

            setText('确定要删除该主机配置吗？');
            modal.update({
              title: '删除主机',
              okText: formatMessage({
                id: 'delete'
              }),
              okProps: {
                color: 'red'
              },
              cancelProps: {
                color: '#000'
              },
              onOk: handleDelete,
              footer: function footer(okBtn, cancelBtn) {
                return /*#__PURE__*/React.createElement(React.Fragment, null, cancelBtn, okBtn);
              }
            });
            return _context.abrupt("return");

          case 10:
            setText(hostType !== 'distribute_test' ? '该主机含有关联的流水线主机部署任务，无法删除。' : '该主机状态已改变，请刷新后重试');
            modal.update({
              title: '删除主机',
              footer: function footer(okBtn, cancelBtn) {
                return /*#__PURE__*/React.createElement(React.Fragment, null, cancelBtn);
              },
              cancelText: '我知道了'
            });
            _context.next = 18;
            break;

          case 14:
            _context.prev = 14;
            _context.t0 = _context["catch"](1);
            modal.update({
              title: '删除主机',
              footer: function footer(okBtn, cancelBtn) {
                return /*#__PURE__*/React.createElement(React.Fragment, null, cancelBtn);
              },
              cancelText: '取消'
            });
            throw new Error(_context.t0);

          case 18:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, null, [[1, 14]]);
  })), [projectId, hostId]);
  useEffect(function () {
    checkNow();
  }, [projectId, hostId]);

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

  return /*#__PURE__*/React.createElement("div", null, text);
};

export default DeleteCheck;