import _message from "antd/es/message";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import { IPublicEnumContextMenuType, IPublicEnumDragObjectType, IPublicEnumTransformStage } from '@gant-lowcode/lowcode-types';
import { isProjectSchema } from '@gant-lowcode/lowcode-utils';
import { intl } from '../locale';
function getNodesSchema(nodes) {
  var componentsTree = nodes.map(function (node) {
    return node === null || node === void 0 ? void 0 : node.exportSchema(IPublicEnumTransformStage.Clone);
  });
  var data = {
    type: 'nodeSchema',
    componentsMap: {},
    componentsTree: componentsTree
  };
  return data;
}
function getClipboardText() {
  return _getClipboardText.apply(this, arguments);
}
function _getClipboardText() {
  _getClipboardText = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
    return _regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) switch (_context3.prev = _context3.next) {
        case 0:
          return _context3.abrupt("return", new Promise(function (resolve, reject) {
            // 使用 Clipboard API 读取剪贴板内容
            navigator.clipboard.readText().then(function (text) {
              try {
                var data = JSON.parse(text);
                if (isProjectSchema(data)) {
                  resolve(data.componentsTree);
                } else {
                  _message.error(intl('NotValidNodeData'));
                  reject(new Error(intl('NotValidNodeData')));
                }
              } catch (error) {
                _message.error(intl('NotValidNodeData'));
                reject(error);
              }
            }, function (err) {
              reject(err);
            });
          }));
        case 1:
        case "end":
          return _context3.stop();
      }
    }, _callee3);
  }));
  return _getClipboardText.apply(this, arguments);
}
export var defaultContextMenu = function defaultContextMenu(ctx) {
  var material = ctx.material,
    canvas = ctx.canvas,
    common = ctx.common;
  var clipboard = canvas.clipboard;
  var utilsIntl = common.utils.intl;
  return {
    init: function init() {
      material.addContextMenuOption({
        name: 'selectComponent',
        title: intl('SelectComponents'),
        condition: function condition(nodes) {
          if (nodes === void 0) {
            nodes = [];
          }
          return nodes.length === 1;
        },
        items: [{
          name: 'nodeTree',
          type: IPublicEnumContextMenuType.NODE_TREE
        }]
      });
      material.addContextMenuOption({
        name: 'copyAndPaste',
        title: intl('CopyAndPaste'),
        disabled: function disabled(nodes) {
          var _nodes;
          if (nodes === void 0) {
            nodes = [];
          }
          return ((_nodes = nodes) === null || _nodes === void 0 ? void 0 : _nodes.filter(function (node) {
            return !(node !== null && node !== void 0 && node.canPerformAction('copy'));
          }).length) > 0;
        },
        condition: function condition(nodes) {
          return (nodes === null || nodes === void 0 ? void 0 : nodes.length) === 1;
        },
        action: function action(nodes) {
          var node = nodes === null || nodes === void 0 ? void 0 : nodes[0];
          if (!node) {
            return;
          }
          var doc = node.document,
            parent = node.parent,
            index = node.index;
          var data = getNodesSchema(nodes);
          clipboard.setData(data);
          if (parent) {
            var newNode = doc === null || doc === void 0 ? void 0 : doc.insertNode(parent, node, (index !== null && index !== void 0 ? index : 0) + 1, true);
            if (!node.isAnchored) {
              newNode === null || newNode === void 0 ? void 0 : newNode.select();
            }
          }
        }
      });
      material.addContextMenuOption({
        name: 'copy',
        title: intl('Copy'),
        disabled: function disabled(nodes) {
          var _nodes2;
          if (nodes === void 0) {
            nodes = [];
          }
          return ((_nodes2 = nodes) === null || _nodes2 === void 0 ? void 0 : _nodes2.filter(function (node) {
            return !(node !== null && node !== void 0 && node.canPerformAction('copy'));
          }).length) > 0;
        },
        condition: function condition(nodes) {
          var _nodes3;
          if (nodes === void 0) {
            nodes = [];
          }
          return ((_nodes3 = nodes) === null || _nodes3 === void 0 ? void 0 : _nodes3.length) > 0;
        },
        action: function action(nodes) {
          if (!nodes || nodes.length < 1) {
            return;
          }
          var data = getNodesSchema(nodes);
          clipboard.setData(data);
        }
      });
      material.addContextMenuOption({
        name: 'pasteToBottom',
        title: intl('PasteToTheBottom'),
        condition: function condition(nodes) {
          return (nodes === null || nodes === void 0 ? void 0 : nodes.length) === 1;
        },
        action: function action(nodes) {
          return _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
            var node, doc, parent, index, nodeSchema, canAddNodes, _nodes4;
            return _regeneratorRuntime.wrap(function _callee$(_context) {
              while (1) switch (_context.prev = _context.next) {
                case 0:
                  if (!(!nodes || nodes.length < 1)) {
                    _context.next = 2;
                    break;
                  }
                  return _context.abrupt("return");
                case 2:
                  node = nodes[0];
                  doc = node.document, parent = node.parent, index = node.index;
                  _context.prev = 4;
                  _context.next = 7;
                  return getClipboardText();
                case 7:
                  nodeSchema = _context.sent;
                  if (!(nodeSchema.length === 0)) {
                    _context.next = 10;
                    break;
                  }
                  return _context.abrupt("return");
                case 10:
                  if (!parent) {
                    _context.next = 18;
                    break;
                  }
                  canAddNodes = nodeSchema.filter(function (nodeSchema) {
                    var dragNodeObject = {
                      type: IPublicEnumDragObjectType.NodeData,
                      data: nodeSchema
                    };
                    return doc === null || doc === void 0 ? void 0 : doc.checkNesting(parent, dragNodeObject);
                  });
                  if (!(canAddNodes.length === 0)) {
                    _context.next = 15;
                    break;
                  }
                  Message.error(nodeSchema.map(function (d) {
                    return utilsIntl(d.title || d.componentName);
                  }).join(',') + "\u7B49\u7EC4\u4EF6\u65E0\u6CD5\u653E\u7F6E\u5230" + utilsIntl(parent.title || parent.componentName) + "\u5185");
                  return _context.abrupt("return");
                case 15:
                  _nodes4 = [];
                  canAddNodes.forEach(function (schema, schemaIndex) {
                    var node = doc === null || doc === void 0 ? void 0 : doc.insertNode(parent, schema, (index !== null && index !== void 0 ? index : 0) + 1 + schemaIndex, true);
                    node && _nodes4.push(node);
                  });
                  doc === null || doc === void 0 ? void 0 : doc.selection.selectAll(_nodes4.map(function (node) {
                    return node === null || node === void 0 ? void 0 : node.id;
                  }));
                case 18:
                  _context.next = 23;
                  break;
                case 20:
                  _context.prev = 20;
                  _context.t0 = _context["catch"](4);
                  console.error(_context.t0);
                case 23:
                case "end":
                  return _context.stop();
              }
            }, _callee, null, [[4, 20]]);
          }))();
        }
      });
      material.addContextMenuOption({
        name: 'pasteToInner',
        title: intl('PasteToTheInside'),
        condition: function condition(nodes) {
          return (nodes === null || nodes === void 0 ? void 0 : nodes.length) === 1;
        },
        disabled: function disabled(nodes) {
          var _nodes5;
          if (nodes === void 0) {
            nodes = [];
          }
          // 获取粘贴数据
          var node = (_nodes5 = nodes) === null || _nodes5 === void 0 ? void 0 : _nodes5[0];
          return !node.isContainerNode;
        },
        action: function action(nodes) {
          return _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
            var node, doc, _node$children, nodeSchema, index, canAddNodes, _nodes6;
            return _regeneratorRuntime.wrap(function _callee2$(_context2) {
              while (1) switch (_context2.prev = _context2.next) {
                case 0:
                  node = nodes === null || nodes === void 0 ? void 0 : nodes[0];
                  if (node) {
                    _context2.next = 3;
                    break;
                  }
                  return _context2.abrupt("return");
                case 3:
                  doc = node.document;
                  _context2.prev = 4;
                  _context2.next = 7;
                  return getClipboardText();
                case 7:
                  nodeSchema = _context2.sent;
                  index = ((_node$children = node.children) === null || _node$children === void 0 ? void 0 : _node$children.size) || 0;
                  if (!(nodeSchema.length === 0)) {
                    _context2.next = 11;
                    break;
                  }
                  return _context2.abrupt("return");
                case 11:
                  canAddNodes = nodeSchema.filter(function (nodeSchema) {
                    var dragNodeObject = {
                      type: IPublicEnumDragObjectType.NodeData,
                      data: nodeSchema
                    };
                    return doc === null || doc === void 0 ? void 0 : doc.checkNesting(node, dragNodeObject);
                  });
                  if (!(canAddNodes.length === 0)) {
                    _context2.next = 15;
                    break;
                  }
                  Message.error(nodeSchema.map(function (d) {
                    return utilsIntl(d.title || d.componentName);
                  }).join(',') + "\u7B49\u7EC4\u4EF6\u65E0\u6CD5\u653E\u7F6E\u5230" + utilsIntl(node.title || node.componentName) + "\u5185");
                  return _context2.abrupt("return");
                case 15:
                  _nodes6 = [];
                  nodeSchema.forEach(function (schema, schemaIndex) {
                    var newNode = doc === null || doc === void 0 ? void 0 : doc.insertNode(node, schema, (index !== null && index !== void 0 ? index : 0) + 1 + schemaIndex, true);
                    newNode && _nodes6.push(newNode);
                  });
                  doc === null || doc === void 0 ? void 0 : doc.selection.selectAll(_nodes6.map(function (node) {
                    return node === null || node === void 0 ? void 0 : node.id;
                  }));
                  _context2.next = 23;
                  break;
                case 20:
                  _context2.prev = 20;
                  _context2.t0 = _context2["catch"](4);
                  console.error(_context2.t0);
                case 23:
                case "end":
                  return _context2.stop();
              }
            }, _callee2, null, [[4, 20]]);
          }))();
        }
      });
      material.addContextMenuOption({
        name: 'delete',
        title: intl('Delete'),
        disabled: function disabled(nodes) {
          var _nodes7;
          if (nodes === void 0) {
            nodes = [];
          }
          return ((_nodes7 = nodes) === null || _nodes7 === void 0 ? void 0 : _nodes7.filter(function (node) {
            return !(node !== null && node !== void 0 && node.canPerformAction('remove'));
          }).length) > 0;
        },
        condition: function condition(nodes) {
          if (nodes === void 0) {
            nodes = [];
          }
          return nodes.length > 0;
        },
        action: function action(nodes) {
          nodes === null || nodes === void 0 ? void 0 : nodes.forEach(function (node) {
            node.remove();
          });
        }
      });
    }
  };
};
defaultContextMenu.pluginName = '___default_context_menu___';