import type { Geometry, Point } from '@faunosteo/core-wasm/types' import { useEditorStore } from '../state/editorStore' import { HANDLE_CAP } from './polygonEdit' import { useAnnotationWork } from './workStore' /** A polygon's vertices when it gets handles (R-7: at most HANDLE_CAP), else null. */ export function handledPoints(geometry: Geometry | null): Point[] | null { if (geometry?.type !== 'polygon') return null return geometry.points.length > HANDLE_CAP ? null : geometry.points } /** The vertex `vertex` of `annotationId` is chosen, stamped with the current text (R-13). */ export function chooseVertex(annotationId: string, vertex: number): void { const base = useEditorStore.getState().documentText useAnnotationWork.setState({ chosen: { annotationId, vertex, base } }) } /** The chosen vertex of `annotationId`, only when it was chosen on the current text. */ export function freshChosen(annotationId: string): number | null { const { chosen } = useAnnotationWork.getState() if (chosen?.annotationId !== annotationId) return null const current = useEditorStore.getState().documentText return chosen.base === current ? chosen.vertex : null } /** * The polygon of `annotationId` in the current document, read from the store: a commit * made earlier in the same event (a pending series committed first) counts. */ export function currentPoints(annotationId: string): Point[] | null { const doc = useEditorStore.getState().previewState?.document const geometry = doc?.annotations.find((a) => a.id === annotationId)?.geometry return geometry?.type === 'polygon' ? geometry.points : null }