import { useMemo } from "react";
import { useGraph } from "@/hooks/useGraph";

export interface BacklinkPage {
  name: string;
  title: string | null;
}

export function useBacklinks(currentSlug: string | undefined): BacklinkPage[] {
  const { data } = useGraph();
  return useMemo(() => {
    if (!data || !currentSlug) return [];
    const nodeMap = new Map(data.nodes.map((n) => [n.id, n]));
    return data.edges
      .filter((e) => e.target === currentSlug)
      .map((e) => ({
        name: e.source,
        title: nodeMap.get(e.source)?.title ?? null,
      }));
  }, [data, currentSlug]);
}
