import React, { useContext } from "react";
import { NodeTreeMapContext } from "./../../ricos-context/viewer/node-tree-map-context.js";
import { TOC_DEFAULTS } from "../defaults.js";
import { extractHeadingsFromRicosNodes } from "../heading-extraction.js";
import { getScrollBehavior, TocView } from "../toc-view.js";
const TocRenderer = ({ node, nodeIndex, NodeViewContainer }) => {
  const { nodeTreeMap } = useContext(NodeTreeMapContext);
  const data = {
    ...TOC_DEFAULTS,
    ...node.tocData
  };
  const headings = extractHeadingsFromRicosNodes(
    Object.values(nodeTreeMap).filter((entry) => !entry.parent).map((entry) => entry.node),
    data.includedHeadings ?? TOC_DEFAULTS.includedHeadings
  );
  return /* @__PURE__ */ React.createElement(NodeViewContainer, { node, nodeIndex }, /* @__PURE__ */ React.createElement(
    TocView,
    {
      tocId: String(node.id || ""),
      data,
      headings,
      mode: "viewer",
      onNavigate: (heading) => {
        if (typeof document === "undefined") {
          return;
        }
        const targetAnchor = document.getElementById(heading.anchor);
        const targetHeading = targetAnchor?.closest?.("h1, h2, h3, h4, h5, h6") ?? targetAnchor;
        targetHeading?.scrollIntoView?.({
          behavior: getScrollBehavior(),
          block: "start"
        });
        targetHeading?.focus?.({ preventScroll: true });
        window.history?.pushState?.(null, "", `#${heading.anchor}`);
      }
    }
  ));
};
export {
  TocRenderer
};
//# sourceMappingURL=toc.js.map

import "@wix/ricos/css/plugin-toc-viewer.global.css";