"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.defaultContextMenu = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _notification = _interopRequireDefault(require("@alifd/next/lib/notification"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _lowcodeTypes = require("@alilc/lowcode-types");
var _lowcodeUtils = require("@alilc/lowcode-utils");
var _locale = require("../locale");
function getNodesSchema(nodes) {
  var componentsTree = nodes.map(function (node) {
    return node === null || node === void 0 ? void 0 : node.exportSchema(_lowcodeTypes.IPublicEnumTransformStage.Clone);
  });
  var data = {
    type: 'nodeSchema',
    componentsMap: {},
    componentsTree: componentsTree
  };
  return data;
}
function getClipboardText() {
  return _getClipboardText.apply(this, arguments);
}
function _getClipboardText() {
  _getClipboardText = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3() {
    return _regenerator["default"].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 ((0, _lowcodeUtils.isProjectSchema)(data)) {
                  resolve(data.componentsTree);
                } else {
                  _notification["default"].open({
                    content: (0, _locale.intl)('NotValidNodeData'),
                    type: 'error'
                  });
                  reject(new Error((0, _locale.intl)('NotValidNodeData')));
                }
              } catch (error) {
                _notification["default"].open({
                  content: (0, _locale.intl)('NotValidNodeData'),
                  type: 'error'
                });
                reject(error);
              }
            }, function (err) {
              reject(err);
            });
          }));
        case 1:
        case "end":
          return _context3.stop();
      }
    }, _callee3);
  }));
  return _getClipboardText.apply(this, arguments);
}
var defaultContextMenu = exports.defaultContextMenu = function defaultContextMenu(ctx) {
  var material = ctx.material,
    canvas = ctx.canvas;
  var clipboard = canvas.clipboard;
  return {
    init: function init() {
      material.addContextMenuOption({
        name: 'selectComponent',
        title: (0, _locale.intl)('SelectComponents'),
        condition: function condition(nodes) {
          return nodes.length === 1;
        },
        items: [{
          name: 'nodeTree',
          type: _lowcodeTypes.IPublicEnumContextMenuType.NODE_TREE
        }]
      });
      material.addContextMenuOption({
        name: 'copyAndPaste',
        title: (0, _locale.intl)('Copy'),
        condition: function condition(nodes) {
          return nodes.length === 1;
        },
        action: function action(nodes) {
          var node = nodes[0];
          var doc = node.document,
            parent = node.parent,
            index = node.index;
          if (parent) {
            var newNode = doc === null || doc === void 0 ? void 0 : doc.insertNode(parent, node, (index !== null && index !== void 0 ? index : 0) + 1, true);
            newNode === null || newNode === void 0 ? void 0 : newNode.select();
          }
        }
      });
      material.addContextMenuOption({
        name: 'copy',
        title: (0, _locale.intl)('Copy.1'),
        action: function action(nodes) {
          if (!nodes || nodes.length < 1) {
            return;
          }
          var data = getNodesSchema(nodes);
          clipboard.setData(data);
        }
      });
      material.addContextMenuOption({
        name: 'zhantieToBottom',
        title: (0, _locale.intl)('PasteToTheBottom'),
        condition: function condition(nodes) {
          return nodes.length === 1;
        },
        action: function action(nodes) {
          return (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
            var node, doc, parent, index, nodeSchema;
            return _regenerator["default"].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 (parent) {
                    nodeSchema.forEach(function (schema, schemaIndex) {
                      doc === null || doc === void 0 ? void 0 : doc.insertNode(parent, schema, (index !== null && index !== void 0 ? index : 0) + 1 + schemaIndex, true);
                    });
                  }
                  _context.next = 14;
                  break;
                case 11:
                  _context.prev = 11;
                  _context.t0 = _context["catch"](4);
                  console.error(_context.t0);
                case 14:
                case "end":
                  return _context.stop();
              }
            }, _callee, null, [[4, 11]]);
          }))();
        }
      });
      material.addContextMenuOption({
        name: 'zhantieToInner',
        title: (0, _locale.intl)('PasteToTheInside'),
        condition: function condition(nodes) {
          return nodes.length === 1;
        },
        disabled: function disabled(nodes) {
          // 获取粘贴数据
          var node = nodes[0];
          return !node.isContainerNode;
        },
        action: function action(nodes) {
          return (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2() {
            var node, doc, parent, nodeSchema, _node$children, index;
            return _regenerator["default"].wrap(function _callee2$(_context2) {
              while (1) switch (_context2.prev = _context2.next) {
                case 0:
                  node = nodes[0];
                  doc = node.document, parent = node.parent;
                  _context2.prev = 2;
                  _context2.next = 5;
                  return getClipboardText();
                case 5:
                  nodeSchema = _context2.sent;
                  if (parent) {
                    index = ((_node$children = node.children) === null || _node$children === void 0 ? void 0 : _node$children.size) || 0;
                    if (parent) {
                      nodeSchema.forEach(function (schema, schemaIndex) {
                        doc === null || doc === void 0 ? void 0 : doc.insertNode(node, schema, (index !== null && index !== void 0 ? index : 0) + 1 + schemaIndex, true);
                      });
                    }
                  }
                  _context2.next = 12;
                  break;
                case 9:
                  _context2.prev = 9;
                  _context2.t0 = _context2["catch"](2);
                  console.error(_context2.t0);
                case 12:
                case "end":
                  return _context2.stop();
              }
            }, _callee2, null, [[2, 9]]);
          }))();
        }
      });
      material.addContextMenuOption({
        name: 'delete',
        title: (0, _locale.intl)('Delete'),
        action: function action(nodes) {
          nodes.forEach(function (node) {
            node.remove();
          });
        }
      });
    }
  };
};
defaultContextMenu.pluginName = '___default_context_menu___';