import { TIPTAP_FIGURE_TYPE } from "./../../ricos-content/index.js";
import { TOP_LEVEL_NODES } from "./../../_shared/top-level-nodes.js";
import { Plugin, PluginKey } from "./../../tiptap/pm/state.js";
import { Decoration, DecorationSet } from "./../../tiptap/pm/view.js";
import { createRicosFunctionalExtension } from "./../../plugin-commons/index.js";
const breakout = createRicosFunctionalExtension({
  type: "extension",
  groups: [],
  name: "breakout",
  reconfigure(config, _extensions, ricosProps, _settings, services) {
    const breakoutRule = ricosProps.breakoutRule;
    return {
      ...config,
      ...breakoutRule && {
        addProseMirrorPlugins() {
          return [
            new Plugin({
              key: new PluginKey("breakout"),
              props: {
                decorations(state) {
                  const decorations = [];
                  const applyBreakoutToNode = (node, pos) => {
                    if (!node) {
                      return;
                    }
                    const ricosNode = services.converters.fromPmNode(node.toJSON());
                    if (!ricosNode) {
                      return;
                    }
                    const breakoutType = breakoutRule({
                      node: ricosNode
                    });
                    if (breakoutType === "fullWidth" && node.type.name === TIPTAP_FIGURE_TYPE) {
                      node.descendants((childNode, childPos) => {
                        if (childNode.type.spec.group?.includes("figureFirstChildContent")) {
                          const childPosInDoc = childPos + pos + 1;
                          decorations.push(
                            Decoration.node(childPosInDoc, childPosInDoc + childNode.nodeSize, {
                              "data-breakout": breakoutType
                            })
                          );
                        }
                      });
                    }
                    decorations.push(
                      Decoration.node(pos, pos + node.nodeSize, { "data-breakout": breakoutType })
                    );
                  };
                  const applyBreakoutToContentBlocks = (node, pos) => {
                    if (TOP_LEVEL_NODES.includes(node.type.name)) {
                      node.forEach((childNode, childOffset) => {
                        const childPos = pos + 1 + childOffset;
                        applyBreakoutToContentBlocks(childNode, childPos);
                      });
                      return;
                    }
                    applyBreakoutToNode(node, pos);
                  };
                  state.doc.forEach((node, pos) => {
                    applyBreakoutToContentBlocks(node, pos);
                  });
                  return DecorationSet.create(state.doc, decorations);
                }
              }
            })
          ];
        }
      }
    };
  },
  createExtensionConfig() {
    return {
      name: this.name,
      priority: 3
    };
  }
});
export {
  breakout
};
//# sourceMappingURL=extension.js.map
