import {
  generateId,
  TIPTAP_CARD_FOOTER_TYPE,
  TIPTAP_CARD_HEADER_TYPE,
  TIPTAP_LAYOUT_TYPE
} from "./../ricos-content/index.js";
import { Fragment, Slice } from "./../tiptap/pm/model.js";
import { Plugin, PluginKey } from "./../tiptap/pm/state.js";
const isCardWrapperType = (typeName) => typeName === TIPTAP_CARD_HEADER_TYPE || typeName === TIPTAP_CARD_FOOTER_TYPE;
const transformPastedNode = (node) => {
  if (node.isText) {
    return [node];
  }
  const newChildren = [];
  node.content.forEach((child) => {
    newChildren.push(...transformPastedNode(child));
  });
  if (isCardWrapperType(node.type.name)) {
    return newChildren;
  }
  let attrs = node.attrs;
  if (node.type.name === TIPTAP_LAYOUT_TYPE && attrs?.banner) {
    attrs = { ...attrs, banner: null };
  }
  if (attrs?.id) {
    attrs = { ...attrs, id: generateId() };
  }
  return [node.type.create(attrs, Fragment.from(newChildren), node.marks)];
};
const createCardPastePlugin = () => {
  return new Plugin({
    key: new PluginKey("card-paste"),
    props: {
      transformPasted(slice) {
        const newChildren = [];
        slice.content.forEach((child) => {
          newChildren.push(...transformPastedNode(child));
        });
        return new Slice(Fragment.from(newChildren), slice.openStart, slice.openEnd);
      }
    }
  });
};
export {
  createCardPastePlugin
};
//# sourceMappingURL=card-paste-plugin.js.map
