import { useState } from 'react' import Dialog from '@/components/ui/Dialog' import Button from '@/components/ui/Button' import Chip from '@/components/ui/Chip' import type { Vertebra, VertebralRegion } from '@/domain/schema/observation' import type { VertebralFormula } from '@/domain/species/types' import { useT } from '@/i18n/useT' const REGIONS: VertebralRegion[] = ['C', 'T', 'L', 'S', 'Cd'] const REGION_KEY: Record = { C: 'vertebra.region.C', T: 'vertebra.region.T', L: 'vertebra.region.L', S: 'vertebra.region.S', Cd: 'vertebra.region.Cd', } /** * Infers the matching vertebral region from a free-form anatomical label * (zone name in FR or EN). Matches on substring roots, case-insensitive, * accent-insensitive. Returns undefined when no root matches. */ export function regionFromLabel(label: string): VertebralRegion | undefined { const norm = label .normalize('NFD') .replace(/\p{Diacritic}/gu, '') .toLowerCase() if (norm.includes('cervic')) return 'C' if (norm.includes('thorac') || norm.includes('dors')) return 'T' if (norm.includes('lombair') || norm.includes('lumbar')) return 'L' if (norm.includes('sacral') || norm.includes('sacre')) return 'S' if (norm.includes('caudal') || norm.includes('coccyg')) return 'Cd' return undefined } export interface VertebraPickerProps { open: boolean onClose: () => void onConfirm: (vertebrae: Vertebra[]) => void initial: Vertebra[] formula: VertebralFormula title: string /** Preselected tab. Overridden by the region of the first existing selection. */ initialRegion?: VertebralRegion } export default function VertebraPicker({ open, onClose, onConfirm, initial, formula, title, initialRegion, }: VertebraPickerProps) { const t = useT() const [selected, setSelected] = useState(initial) const [activeRegion, setActiveRegion] = useState( () => initial[0]?.region ?? initialRegion ?? 'C', ) const has = (r: VertebralRegion, i: number) => selected.some((v) => v.region === r && v.index === i) const toggle = (r: VertebralRegion, i: number) => { setSelected((cur) => has(r, i) ? cur.filter((v) => !(v.region === r && v.index === i)) : [...cur, { region: r, index: i }], ) } const all = (r: VertebralRegion, count: number) => { setSelected((cur) => { const rest = cur.filter((v) => v.region !== r) return [...rest, ...Array.from({ length: count }, (_, k) => ({ region: r, index: k + 1 }))] }) } const none = (r: VertebralRegion) => setSelected((cur) => cur.filter((v) => v.region !== r)) const count = formula[activeRegion] return (
{REGIONS.map((r) => ( ))}
{Array.from({ length: count }, (_, k) => k + 1).map((i) => ( toggle(activeRegion, i)} > {activeRegion}{i} ))}
) }