{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "ds-table-of-contents",
  "files": [
    {
      "path": "components/ds/ds-table-of-contents.tsx",
      "content": "\"use client\"\n\nimport React, { useEffect, useState } from \"react\"\nimport { cn } from \"@/lib/utils\"\n\n// ── Types ──\n\nexport interface TocItem {\n  id: string\n  title: string\n  /**\n   * The indentation level of the item (usually corresponds to h1=1, h2=2, etc.)\n   */\n  level: number\n}\n\nexport interface TableOfContentsProps extends React.HTMLAttributes<HTMLElement> {\n  items: TocItem[]\n  /**\n   * If provided, overrides automatic scroll tracking and controls the active id.\n   */\n  activeId?: string\n  /** Offset superior para acionar a troca do active element via scroll (em px) */\n  offset?: number\n  /** Se passado, intercepta o clique do link */\n  onItemClick?: (id: string, e: React.MouseEvent<HTMLAnchorElement>) => void\n}\n\n// ── Component ──\n\nexport const TableOfContents = React.forwardRef<\n  HTMLElement,\n  TableOfContentsProps\n>(\n  (\n    {\n      items,\n      activeId: externalActiveId,\n      offset = 100,\n      onItemClick,\n      className,\n      ...props\n    },\n    ref\n  ) => {\n    const [internalActiveId, setInternalActiveId] = useState<string>(\"\")\n    const activeId =\n      externalActiveId !== undefined ? externalActiveId : internalActiveId\n\n    useEffect(() => {\n      // Se estamos controlando externamente, ignorar o observer interno\n      if (externalActiveId !== undefined) return\n\n      const elements: HTMLElement[] = []\n      items.forEach((item) => {\n        const el = document.getElementById(item.id)\n        if (el) elements.push(el)\n      })\n\n      if (!elements.length) return\n\n      const observer = new IntersectionObserver(\n        (entries) => {\n          // We look for the last intersecting entry, prioritizing top visibility\n          const visibleEntries = entries.filter((e) => e.isIntersecting)\n          if (visibleEntries.length > 0) {\n            setInternalActiveId(visibleEntries[0].target.id)\n          }\n        },\n        { rootMargin: `-${offset}px 0px -40% 0px` }\n      )\n\n      elements.forEach((el) => observer.observe(el))\n\n      return () => {\n        elements.forEach((el) => observer.unobserve(el))\n        observer.disconnect()\n      }\n    }, [items, externalActiveId, offset])\n\n    const handleClick = (\n      id: string,\n      e: React.MouseEvent<HTMLAnchorElement>\n    ) => {\n      if (onItemClick) {\n        onItemClick(id, e)\n      } else {\n        // Default behavior: smooth scroll\n        const el = document.getElementById(id)\n        if (el) {\n          e.preventDefault()\n          const top = el.getBoundingClientRect().top + window.scrollY - offset\n          window.scrollTo({ top, behavior: \"smooth\" })\n          // Atualiza para refletir clique imediato\n          if (externalActiveId === undefined) {\n            setInternalActiveId(id)\n          }\n        }\n      }\n    }\n\n    // Calculate the smallest level to normalize indentation (if starting from h2, h2 will be indent 0)\n    const minLevel =\n      items.length > 0 ? Math.min(...items.map((i) => i.level)) : 1\n\n    return (\n      <nav\n        ref={ref}\n        aria-label=\"Table of contents\"\n        className={cn(\"text-sm\", className)}\n        {...props}\n      >\n        <ul className=\"flex flex-col gap-1\">\n          {items.map((item) => {\n            const isActive = item.id === activeId\n            const indentLevel = Math.max(0, item.level - minLevel)\n\n            return (\n              <li\n                key={item.id}\n                style={{ paddingLeft: `${indentLevel * 1}rem` }}\n              >\n                <a\n                  href={`#${item.id}`}\n                  onClick={(e) => handleClick(item.id, e)}\n                  className={cn(\n                    \"block border-l-2 py-1.5 pl-3 transition-colors hover:text-foreground\",\n                    isActive\n                      ? \"border-primary font-medium text-foreground\"\n                      : \"border-border text-muted-foreground\"\n                  )}\n                >\n                  {item.title}\n                </a>\n              </li>\n            )\n          })}\n        </ul>\n      </nav>\n    )\n  }\n)\n\nTableOfContents.displayName = \"TableOfContents\"\n",
      "type": "registry:ui"
    }
  ],
  "type": "registry:ui"
}
