import type { Thing, WithContext } from "schema-dts";

type JsonLdData = WithContext<Thing> | Record<string, unknown>;

/**
 * Renders a JSON-LD <script> (§9.3). dangerouslySetInnerHTML is the
 * sanctioned exception in §3.2 (JSON-LD only).
 */
export function JsonLd({ data }: { data: JsonLdData | JsonLdData[] }) {
  return (
    <script
      type="application/ld+json"
      // JSON.stringify output is safe here; escape "<" to avoid closing the script early.
      dangerouslySetInnerHTML={{
        __html: JSON.stringify(data).replace(/</g, "\\u003c"),
      }}
    />
  );
}
