/**
 * Shared JSON-LD schema builders for schema.org types used across features.
 *
 * Feature-specific builders live alongside their feature (e.g.
 * src/features/online-board/json-ld.ts). This module provides builders
 * for types shared across multiple features (WebSite, WebPage).
 *
 * All functions are pure — no side effects, no imports of React.
 *
 * @module
 */

import type { WebSite, WebPage } from "schema-dts";

// ---------------------------------------------------------------------------
// WebSite
// ---------------------------------------------------------------------------

export interface BuildWebsiteJsonLdInput {
  name: string;
  url: string;
  description: string;
  /** Optional URL template for a SearchAction, e.g. "{url}?q={search_term}" */
  searchUrl?: string;
}

/**
 * Build a schema.org WebSite JSON-LD object.
 *
 * When `searchUrl` is provided a SearchAction potentialAction is attached
 * per the sitelinks searchbox spec.
 */
export function buildWebsiteJsonLd(input: BuildWebsiteJsonLdInput): WebSite {
  const base: WebSite = {
    "@type": "WebSite",
    name: input.name,
    url: input.url,
    description: input.description,
  };

  if (input.searchUrl) {
    // schema-dts types SearchAction via the Action union — double cast needed
    // because query-input is a schema.org extension property not in schema-dts.
    (base as unknown as Record<string, unknown>).potentialAction = {
      "@type": "SearchAction",
      target: input.searchUrl,
      "query-input": "required name=search_term",
    };
  }

  return base;
}

// ---------------------------------------------------------------------------
// WebPage
// ---------------------------------------------------------------------------

export interface BuildWebpageJsonLdInput {
  name: string;
  url: string;
  description: string;
  inLanguage?: string;
}

/**
 * Build a schema.org WebPage JSON-LD object.
 */
export function buildWebpageJsonLd(input: BuildWebpageJsonLdInput): WebPage {
  const base: WebPage = {
    "@type": "WebPage",
    name: input.name,
    url: input.url,
    description: input.description,
  };

  if (input.inLanguage) {
    base.inLanguage = input.inLanguage;
  }

  return base;
}
