/**
 * RevealManager — mounts as a child of <Article> and reactively applies
 * reveal classes (non-GM) or injects action buttons (GM) whenever the
 * content DOM, stream connection state, role, or completions change.
 */

import { Component, createEffect, onCleanup } from "solid-js";
import { useLocation } from "@solidjs/router";
import { useArticleDom } from "./Article";
import { journalStreamState } from "./stores/journalStream";
import { useJournalCompletions } from "./journal/completions";
import { addRevealedClasses, cleanupInjections } from "./stores/reveal";

export const RevealManager: Component = () => {
  const contentDom = useArticleDom();
  const location = useLocation();
  const comp = useJournalCompletions();

  createEffect(() => {
    const dom = contentDom();
    if (!dom) return;

    // Access stream state reactively so the effect re-runs whenever
    // connection status or role changes.
    const state = journalStreamState;
    void state.connected;
    void state.myRole;

    addRevealedClasses(dom, location.pathname, comp.data);
  });

  onCleanup(() => {
    const dom = contentDom();
    if (dom) cleanupInjections(dom);
  });

  return null;
};

export default RevealManager;
