"use client";

import { useEffect } from "react";
import { useBranding } from "@school/ui";
import { brandingLogoUrl } from "@/lib/branding";

export function BrandingFavicon() {
  const branding = useBranding();
  const href = brandingLogoUrl(branding.favicon_url);

  useEffect(() => {
    if (!href) return;

    const rels = ["icon", "shortcut icon"];
    for (const rel of rels) {
      let link = document.querySelector<HTMLLinkElement>(`link[rel='${rel}']`);
      if (!link) {
        link = document.createElement("link");
        link.rel = rel;
        document.head.appendChild(link);
      }
      link.href = href;
    }
  }, [href]);

  return null;
}
