• Builds minimal selectedMatch with format, genres, tags, and optional confidence fields from raw object. Extracts confidence score if present in the raw match data.

    Parameters

    • raw: unknown

      Raw object to build from.

    Returns undefined | SelectedMatchLite

    SelectedMatchLite or undefined if no valid fields present.

    export function buildSelectedMatch(
    raw: unknown,
    ): SelectedMatchLite | undefined {
    if (typeof raw !== "object" || raw === null) return undefined;

    const obj = raw as Record<string, unknown>;
    const format =
    typeof obj.format === "string" && obj.format.trim() !== ""
    ? obj.format
    : undefined;
    const genres = Array.isArray(obj.genres)
    ? obj.genres.filter((g) => typeof g === "string")
    : [];

    // Extract tags from tag objects
    const tags = Array.isArray(obj.tags)
    ? obj.tags
    .map((t) =>
    typeof t === "object" && t !== null && "name" in t
    ? (t as { name: string }).name
    : "",
    )
    .filter((name) => name.trim() !== "")
    : [];

    // Extract confidence score if present
    const confidence =
    typeof obj.confidence === "number" &&
    obj.confidence >= 0 &&
    obj.confidence <= 100
    ? obj.confidence
    : undefined;

    if (!format && genres.length === 0 && tags.length === 0) return undefined;

    return {
    format,
    genres,
    tags,
    ...(confidence !== undefined && { confidence }),
    };
    }