import React, { useState, useRef, useLayoutEffect } from "react";
import { observer } from 'mobx-react';
import { observableProject } from '../../../shell';
import { UnorderedListOutlined, DeleteOutlined, CopyOutlined } from '@ant-design/icons';
import { Popover } from 'antd';
import './operation.less';
var ComponentTreeOutLook = function ComponentTreeOutLook(props) {
  if (props.node) {
    return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
      style: {
        marginRight: props.level * 8 + 'px'
      },
      className: "vitis-node-component-tree-item"
    }, props.node.title), /*#__PURE__*/React.createElement(ComponentTreeOutLook, {
      node: props.node.parent,
      level: props.level + 1
    }));
  }
  return null;
};
export default observer(function Operation() {
  var _observableProject$do, _observableProject$do2;
  var _useState = useState({}),
    style = _useState[0],
    setStyle = _useState[1];
  var rootRef = useRef(null);
  useLayoutEffect(function () {
    var rect = observableProject.designer.detection.selectedNodePosition;
    if (rect && observableProject.documentModel.currentNode) {
      var _rootRef$current;
      setStyle({
        left: rect.right - (((_rootRef$current = rootRef.current) === null || _rootRef$current === void 0 ? void 0 : _rootRef$current.clientWidth) || 0),
        top: rect.top,
        visibility: 'visible'
      });
    } else {
      setStyle({});
    }
  }, [observableProject.designer.detection.selectedNodePosition, observableProject.documentModel.currentNode]);
  var onDel = function onDel() {
    observableProject.documentModel.delNode(observableProject.documentModel.selectedNodeId);
    observableProject.designer.selectNode(undefined);
  };
  var onCopy = function onCopy() {
    var _currentNode$parent;
    var currentNode = observableProject.documentModel.currentNode;
    var node = observableProject.documentModel.copyNode(currentNode);
    (_currentNode$parent = currentNode.parent) === null || _currentNode$parent === void 0 ? void 0 : _currentNode$parent.insertAfter(node, currentNode);
    observableProject.designer.rerender();
  };
  return /*#__PURE__*/React.createElement("div", {
    className: "vitis-node-operation",
    style: style,
    ref: rootRef
  }, observableProject.documentModel.currentNode && /*#__PURE__*/React.createElement(Popover, {
    placement: "bottomRight",
    trigger: "hover",
    showArrow: false,
    content: /*#__PURE__*/React.createElement(ComponentTreeOutLook, {
      node: observableProject.documentModel.currentNode,
      level: 0
    }),
    overlayClassName: "vitis-node-operation-component-tree-popover"
  }, /*#__PURE__*/React.createElement(UnorderedListOutlined, {
    className: "icon"
  })), ((_observableProject$do = observableProject.documentModel.currentNode) === null || _observableProject$do === void 0 ? void 0 : _observableProject$do.componentSpec.unableDel) === false && /*#__PURE__*/React.createElement(DeleteOutlined, {
    className: "icon",
    onClick: onDel
  }), ((_observableProject$do2 = observableProject.documentModel.currentNode) === null || _observableProject$do2 === void 0 ? void 0 : _observableProject$do2.componentSpec.unableCopy) === false && /*#__PURE__*/React.createElement(CopyOutlined, {
    className: "icon",
    onClick: onCopy
  }));
});