"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.copyDomNode = void 0;
exports.getSelectedNodeOrNodeParentByNodeType = getSelectedNodeOrNodeParentByNodeType;
exports.toDOM = void 0;
var _model = require("@atlaskit/editor-prosemirror/model");
var _state = require("@atlaskit/editor-prosemirror/state");
var _utils = require("@atlaskit/editor-prosemirror/utils");
var _clipboard = require("../clipboard");
var _browser = require("../utils/browser");
function getSelectedNodeOrNodeParentByNodeType(_ref) {
  var nodeType = _ref.nodeType,
    selection = _ref.selection;
  var node = (0, _utils.findSelectedNodeOfType)(nodeType)(selection);
  if (!node) {
    node = (0, _utils.findParentNodeOfType)(nodeType)(selection);
  }
  return node;
}
var toDOM = exports.toDOM = function toDOM(node, schema) {
  return _model.DOMSerializer.fromSchema(schema).serializeNode(node);
};
var copyDomNode = exports.copyDomNode = function copyDomNode(domNode, nodeType, selection) {
  if (domNode) {
    var div = document.createElement('div');
    div.appendChild(domNode);
    var schema = selection.$from.doc.type.schema;
    var browser = (0, _browser.getBrowserInfo)();

    // if copying inline content
    if (nodeType.inlineContent) {
      // The "1 1" refers to the start and end depth of the slice
      // since we're copying the text inside a paragraph, it will always be 1 1
      // https://github.com/ProseMirror/prosemirror-view/blob/master/src/clipboard.ts#L32
      // Ignored via go/ees005
      // eslint-disable-next-line @atlaskit/editor/no-as-casting
      div.firstChild.setAttribute('data-pm-slice', '1 1 []');
    } else {
      // The "0 0" refers to the start and end depth of the slice
      // since we're copying the block node only, it will always be 0 0
      // https://github.com/ProseMirror/prosemirror-view/blob/master/src/clipboard.ts#L32
      // Ignored via go/ees005
      // eslint-disable-next-line @atlaskit/editor/no-as-casting
      div.firstChild.setAttribute('data-pm-slice', '0 0 []');
    }
    // ED-17083 safari seems have bugs for extension copy because exntension do not have a child text(innerText) and it will not recognized as html in clipboard, this could be merge into one if this extension fixed children issue or safari fix the copy bug
    // MEX-2528 safari has a bug related to the mediaSingle node with border or link. The image tag within the clipboard is not recognized as HTML when using the ClipboardItem API. To address this, we have to switch to ClipboardPolyfill
    if (browser.safari && selection instanceof _state.NodeSelection && (selection.node.type === schema.nodes.extension || selection.node.type === schema.nodes.mediaSingle)) {
      (0, _clipboard.copyHTMLToClipboardPolyfill)(div);
    } else {
      (0, _clipboard.copyHTMLToClipboard)(div);
    }
  }
};