"use client"; import { useMemo } from "react"; import { mergeCompactCardIdsWithPinnedSelected } from "../../../../lib/create/methodCardDisplayOrder"; import { deriveCompactCards, groupAnswersForRecommendations, type RecommendationSection, } from "./useFacetRecommendations"; import { useCreateFlow } from "../context/CreateFlowContext"; type MethodEntry = { id: string; label: string; supportText: string }; function orderByIds( methods: readonly T[], orderedIds: readonly string[], ): T[] { const byId = new Map(methods.map((method) => [method.id, method])); const ordered = orderedIds.flatMap((id) => { const method = byId.get(id); return method ? [method] : []; }); const seen = new Set(ordered.map((method) => method.id)); return [...ordered, ...methods.filter((method) => !seen.has(method.id))]; } /** * Deck order and the compact stack come from the job rule. Current screens * show five cards; a deck screen passes seven. Selected cards still pin * into the compact stack. */ export function useMethodCardDeckOrdering( section: RecommendationSection, methods: readonly MethodEntry[], selectedIds: readonly string[], ) { const { state } = useCreateFlow(); const answers = useMemo( () => groupAnswersForRecommendations(state), [state], ); const deck = useMemo( () => deriveCompactCards(section, answers, 5), [section, answers], ); const rankedMethods = useMemo( () => orderByIds(methods, deck.orderedIds), [methods, deck.orderedIds], ); const selectionShowcaseActive = selectedIds.length > 0; const { recommendedIds } = deck; const compactCardIds = useMemo( () => mergeCompactCardIdsWithPinnedSelected( rankedMethods.map((method) => method.id), deck.compactCardIds, selectedIds, selectionShowcaseActive, 5, ), [ rankedMethods, deck.compactCardIds, selectedIds, selectionShowcaseActive, ], ); const sampleCards = useMemo( () => rankedMethods.map((entry) => ({ id: entry.id, label: entry.label, supportText: entry.supportText, recommended: recommendedIds.has(entry.id), })), [rankedMethods, recommendedIds], ); const methodById = useMemo( () => new Map(rankedMethods.map((entry) => [entry.id, entry])), [rankedMethods], ); return { rankedMethods, displayMethods: rankedMethods, compactCardIds, recommendedIds, sampleCards, methodById, recommendationsReady: true, }; }