/**
 * Writes evidence links to per-session capture storage whenever the
 * binder mutates links.
 */

import { useEffect } from "react";

import type { SessionId } from "@shared/ids";

import { useBinder } from "@binder/index";
import { useEngineEventTick } from "@work/index";

import { persistCapturePatch } from "./capture-persistence";

export function CaptureLinkPersister({ sessionId }: { sessionId: SessionId }) {
  const { links } = useBinder();
  const linkTick = useEngineEventTick("EvidenceLinkCreated");
  const unlinkTick = useEngineEventTick("EvidenceLinkRemoved");
  const updateTick = useEngineEventTick("EvidenceLinkUpdated");

  useEffect(() => {
    void linkTick;
    void unlinkTick;
    void updateTick;
    persistCapturePatch(sessionId, { evidenceLinks: links.list() });
  }, [sessionId, links, linkTick, unlinkTick, updateTick]);

  return null;
}