/**
 * JSON-LD schema builders for Schedule pages.
 *
 * Produces schema-dts typed objects ready for <JsonLdRenderer>.
 * Uses schema.org Flight and ItemList types.
 *
 * Reuses online-board's buildFlightJsonLd for individual flight mapping,
 * since the flight data model is shared.
 *
 * @module
 */

import type { Flight, ItemList, ListItem } from "schema-dts";
import { buildFlightJsonLd } from "../online-board/json-ld.js";
import type { ISimpleFlight } from "./types.js";

// ---------------------------------------------------------------------------
// Public JSON-LD builders
// ---------------------------------------------------------------------------

/**
 * Build a schema.org Flight JSON-LD object for a schedule flight.
 * Delegates to the shared online-board builder.
 */
export function buildScheduleFlightJsonLd(flight: ISimpleFlight): Flight {
  return buildFlightJsonLd(flight);
}

/**
 * Build a schema.org ItemList of Flight JSON-LD objects from schedule search results.
 *
 * Each flight becomes a ListItem with a position (1-indexed).
 */
export function buildScheduleFlightListJsonLd(
  flights: ISimpleFlight[],
  searchDescription: string,
): ItemList {
  const items: ListItem[] = flights.map((flight, index) => ({
    "@type": "ListItem" as const,
    position: index + 1,
    item: buildScheduleFlightJsonLd(flight),
  }));

  return {
    "@type": "ItemList",
    description: searchDescription,
    itemListElement: items,
  };
}
