import React, { useContext } from "react";
import {
  renderGapSpacerAfterLastNodeIfNeeded,
  renderGapSpacerBeforeNodeIfNeeded
} from "./../../ricos-common-plugins/gap-spacer/gap-spacer-viewer.js";
import { ContentTraverserContext } from "./../../ricos-context/index.js";
import { NodeTreeMapContext } from "./../../ricos-context/viewer/node-tree-map-context.js";
import { getAnchor } from "./components/anchor.js";
import { nodeViewContainer as NodeViewContainer } from "./rericos/renderers/plugins/node-view-container.js";
const Fallback = ({ node }) => {
  return false ? /* @__PURE__ */ React.createElement("div", null, `Type ${node.type} is not supported yet (:`) : null;
};
const RicosNodeDecorated = ({
  node,
  index,
  isRoot,
  shouldParagraphApplyTextStyle
}) => {
  const { renderers, nodeViewDecorators } = useContext(ContentTraverserContext);
  const Renderer = renderers[node.type];
  if (!Renderer) {
    console.error(`RicosNodeDedcorated Error: no renderer passed for node type - ${node.type}`);
    return /* @__PURE__ */ React.createElement(Fallback, { node });
  }
  return nodeViewDecorators.apply(
    /* @__PURE__ */ React.createElement(
      Renderer,
      {
        node,
        key: node.id || index.toString(),
        RicosNodesRenderer,
        isRoot,
        nodeIndex: index,
        NodeViewContainer,
        shouldParagraphApplyTextStyle
      }
    ),
    node,
    !!isRoot
  );
};
const RicosNodesRenderer = ({
  nodes,
  isRoot,
  shouldParagraphApplyTextStyle,
  addAnchorsPrefix
}) => {
  const { nodeTreeMap, nodeRefMap } = useContext(NodeTreeMapContext);
  return /* @__PURE__ */ React.createElement(React.Fragment, null, nodes.flatMap((node, index) => {
    if (!node) {
      console.error(`RicosNodesRenderer Error: node is undefined at index ${index}`);
      return [];
    }
    return [
      ...renderGapSpacerBeforeNodeIfNeeded(node, index, nodeTreeMap, nodeRefMap),
      /* @__PURE__ */ React.createElement(
        RicosNodeDecorated,
        {
          isRoot,
          node,
          index,
          key: node.id || index.toString(),
          shouldParagraphApplyTextStyle
        }
      ),
      ...addAnchorsPrefix ? [getAnchor(node, index, addAnchorsPrefix)] : []
    ];
  }), renderGapSpacerAfterLastNodeIfNeeded(nodes, nodeTreeMap, nodeRefMap));
};
export {
  RicosNodesRenderer
};
//# sourceMappingURL=ricos-nodes-renderer.js.map
