import { useMemo } from 'react' import type { AnatomicalSheetDocument, Annotation, AnnotationCategory, } from '@faunosteo/core-wasm/types' import { folded } from '@/catalogue/useCatalogue' /** * What the overview panel counts, read once per parsed document: the annotations of each * view and the vertebral regions. A display count, no rule: no core call, no network. */ export interface OverviewCounts { annotationsByView: ReadonlyMap regions: number } export function overviewCounts(doc: AnatomicalSheetDocument): OverviewCounts { const annotationsByView = new Map() for (const annotation of doc.annotations) { const count = annotationsByView.get(annotation.view_id) ?? 0 annotationsByView.set(annotation.view_id, count + 1) } return { annotationsByView, regions: doc.vertebral_config.regions.length } } export function useOverviewCounts( doc: AnatomicalSheetDocument, ): OverviewCounts { return useMemo(() => overviewCounts(doc), [doc]) } /** The annotations of one category in the shown view; `category` is null for strays. */ export interface AnnotationGroup { category: AnnotationCategory | null annotations: Annotation[] } /** * The annotations of a view grouped by category in document order, keeping those whose * displayed name contains `term` (case and accents ignored). A group « Sans catégorie » * exists only when an annotation names a category the document does not hold. */ export function groupAnnotations( doc: AnatomicalSheetDocument, viewId: string, term: string, nameOf: (annotation: Annotation) => string, ): AnnotationGroup[] { const wanted = folded(term.trim()) const shown = doc.annotations.filter( (a) => a.view_id === viewId && folded(nameOf(a)).includes(wanted), ) const known = new Set(doc.categories.map((category) => category.id)) const groups: AnnotationGroup[] = doc.categories.map((category) => ({ category, annotations: shown.filter((a) => a.category_id === category.id), })) groups.push({ category: null, annotations: shown.filter((a) => !known.has(a.category_id)), }) return groups.filter((group) => group.annotations.length > 0) }