"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _helpers = require("./helpers");
const catchSlateFragment = /data-slate-fragment="(.+?)"/m;
const getSlateFragmentAttribute = dataTransfer => {
  const htmlData = dataTransfer.getData('text/html');
  const [, fragment] = htmlData.match(catchSlateFragment) || [];
  return fragment;
};
const withNodeId = editor => {
  const {
    apply
  } = editor;
  const newEditor = editor;
  newEditor.apply = op => {
    const newOp = (0, _helpers.decorateOperation)(op);
    apply(newOp);
  };

  // rewrite insert fragment data
  newEditor.insertFragmentData = data => {
    const fragment = data.getData('application/x-slate-fragment') || getSlateFragmentAttribute(data);
    if (fragment) {
      const decoded = decodeURIComponent(window.atob(fragment));
      const parsed = JSON.parse(decoded);
      const newData = (0, _helpers.replacePastedDataId)(parsed);
      newEditor.insertFragment(newData);
      return newEditor;
    }
  };
  return newEditor;
};
var _default = exports.default = withNodeId;