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 = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useEffect, useState } from 'react';
import { Toolbar } from '@antv/x6-react-components';
import '@antv/x6-react-components/es/menu/style/index.css';
import '@antv/x6-react-components/es/toolbar/style/index.css';
import { ZoomInOutlined, ZoomOutOutlined, DeleteOutlined, SettingOutlined } from '@ant-design/icons';
import { IconFont } from '@zgfe/business-lib';
import { FlowZoomRange } from './graphConfig';
import { Tooltip } from 'antd';
import { FlowIterator } from './util';
var Toolbar_ = Toolbar;
var Item = Toolbar.Item;
var Group = Toolbar.Group;
var FlowToolbar = function FlowToolbar(_ref) {
  var graph = _ref.graph,
    readonly = _ref.readonly,
    onBack = _ref.onBack,
    onSetting = _ref.onSetting,
    onRemoveNode = _ref.onRemoveNode;
  var _useState = useState(1),
    _useState2 = _slicedToArray(_useState, 2),
    scale = _useState2[0],
    setScale = _useState2[1];
  var _useState3 = useState(graph.zoom() !== FlowZoomRange.MAX),
    _useState4 = _slicedToArray(_useState3, 2),
    zoomInAble = _useState4[0],
    setZoomInAble = _useState4[1];
  var _useState5 = useState(graph.zoom() !== FlowZoomRange.MIN),
    _useState6 = _slicedToArray(_useState5, 2),
    zoomOutAble = _useState6[0],
    setZoomOutAble = _useState6[1];
  var _useState7 = useState(false),
    _useState8 = _slicedToArray(_useState7, 2),
    delAble = _useState8[0],
    setDelAble = _useState8[1];
  var zoomStep = FlowZoomRange.STEP;
  useEffect(function () {
    graph.on('cell:selected', function () {
      setDelAble(true);
    });
    graph.on('cell:unselected', function () {
      setDelAble(graph.getSelectedCellCount() > 0);
    });
  }, []);
  function onClickMenu(menu) {
    switch (menu) {
      case 'back':
        if (onBack) onBack();
        break;
      case 'reset':
      case '100':
        setScale(function () {
          var res = 1;
          graph.zoomTo(res);
          graph.centerContent();
          setZoomOutAble(true);
          setZoomInAble(true);
          return res;
        });
        break;
      case 'zoomIn':
        setScale(function (val) {
          var res = val + zoomStep;
          res = res >= FlowZoomRange.MAX ? FlowZoomRange.MAX : res;
          graph.zoomTo(res);
          graph.centerContent();
          setZoomOutAble(true);
          if (res === FlowZoomRange.MAX) {
            setZoomInAble(false);
          }
          return res;
        });
        break;
      case 'zoomOut':
        setScale(function (val) {
          var res = val - zoomStep;
          res = res <= FlowZoomRange.MIN ? FlowZoomRange.MIN : res;
          setZoomInAble(true);
          if (res === FlowZoomRange.MIN) {
            setZoomOutAble(false);
          }
          graph.zoomTo(res);
          graph.centerContent();
          return res;
        });
        break;
      case 'undo':
        graph.undo();
        break;
      case 'redo':
        graph.redo();
        break;
      case 'delete':
        var nodes = graph.getSelectedCells();
        graph.removeCells(nodes);
        nodes.forEach(function (cell) {
          if (cell.isNode()) {
            FlowIterator(cell, 'down', function (item) {
              item.updateData({
                status: 'failed'
              });
            });
          }
        });
        if (onRemoveNode) onRemoveNode(nodes);
        break;
    }
  }
  return /*#__PURE__*/React.createElement(Toolbar_, {
    onClick: onClickMenu,
    extra: readonly ? null : /*#__PURE__*/React.createElement(Tooltip, {
      title: "\u6D3B\u52A8\u8BBE\u7F6E",
      placement: "bottom"
    }, /*#__PURE__*/React.createElement(SettingOutlined, {
      style: {
        marginRight: '16px',
        cursor: 'pointer'
      },
      onClick: function onClick() {
        graph.cleanSelection();
        if (onSetting) onSetting();
      }
    }))
  }, readonly ? null : /*#__PURE__*/React.createElement(Group, null, /*#__PURE__*/React.createElement(Item, {
    name: "back",
    icon: /*#__PURE__*/React.createElement(IconFont, {
      type: "fanhuiicon"
    })
  }, "\u8FD4\u56DE")), /*#__PURE__*/React.createElement(Group, null, /*#__PURE__*/React.createElement(Item, {
    name: "zoomIn",
    tooltip: "\u653E\u5927",
    disabled: !zoomInAble,
    icon: /*#__PURE__*/React.createElement(ZoomInOutlined, null)
  }), /*#__PURE__*/React.createElement(Item, {
    name: "100"
  }, "".concat((scale * 100).toFixed(0), "%")), /*#__PURE__*/React.createElement(Item, {
    name: "zoomOut",
    tooltip: "\u7F29\u5C0F",
    disabled: !zoomOutAble,
    icon: /*#__PURE__*/React.createElement(ZoomOutOutlined, null)
  }), /*#__PURE__*/React.createElement(Item, {
    name: "reset",
    tooltip: "\u7F29\u653E\u52301:1\u5E76\u5C45\u4E2D",
    icon: /*#__PURE__*/React.createElement(IconFont, {
      type: "quanpingzhanshi"
    })
  })), readonly ? null : /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Group, null, /*#__PURE__*/React.createElement(Item, {
    name: "delete",
    icon: /*#__PURE__*/React.createElement(DeleteOutlined, null),
    disabled: !delAble,
    tooltip: "\u5220\u9664"
  }))));
};
export default FlowToolbar;