import {useCallback, useEffect, useMemo, useRef, useState, type ReactNode} from 'react' import {api, docHref, href, normalized, safeURL, shortDate, useApi, type Anchor, type Fragment, type GraphEdge, type ReaderData, type RelatedData, type Route} from '../lib/api' import {Icon} from '../components/Icon' import {Sheet, SheetContent, SheetDescription, SheetTitle} from '../components/ui/sheet' type DocumentData = ReaderData & {fragments?: Fragment[]; body?: string} type ReaderProps = {route: Route; onNotify: (message: string) => void; onUpdate: (id: string) => Promise} const emptyDocument: Fragment[] = [] function labelKind(kind: string) { return ({code: 'Кодекс', 'federal-law': 'Федеральный закон', bylaw: 'Подзаконный акт', plenum: 'Пленум', review: 'Обзор практики'} as Record)[kind] || 'Правовой документ' } function renderAnchoredText(text: string, anchors: Anchor[] = [], query = ''): ReactNode[] { const sorted = anchors.filter(anchor => Number.isFinite(anchor.offset) && anchor.offset >= 0).slice().sort((a, b) => a.offset - b.offset) const chunks: ReactNode[] = [] let cursor = 0 const addText = (value: string, key: string) => { if (!value) return if (!query) { chunks.push({value}); return } const parts = value.split(new RegExp(`(${query.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')})`, 'ig')) chunks.push(...parts.map((part, index) => part.toLocaleLowerCase('ru') === query.toLocaleLowerCase('ru') ? {part} : {part})) } sorted.forEach((anchor, index) => { const offset = Math.min(text.length, Math.max(cursor, anchor.offset)) addText(text.slice(cursor, offset), `text-${index}`) chunks.push(