import { useMemo, useState } from 'react'
import type {
AnatomicalSheetDocument,
Dependent,
ElementKey,
} from '@faunosteo/core-wasm/types'
import Button from '@/components/ui/Button'
import Dialog from '@/components/ui/Dialog'
import { dependentsOf } from '@/editor/core/editOps'
import { refusalMessage } from '@/editor/refusal'
import { elementLabel, useSheetDocument } from '@/editor/sheetDocument'
import { useEditorStore } from '@/editor/state/editorStore'
import { useT } from '@/i18n/useT'
import { useLocaleStore } from '@/state/localeStore'
export interface DeleteTarget {
key: ElementKey
/** The element's name as the list shows it. */
label: string
}
export interface DeleteDialogProps {
/** null = closed. The dialog asks the core for the dependents each time it opens. */
target: DeleteTarget | null
onClose: () => void
}
/**
* Delete with dependents (shared by the Général, Références, Vertèbres, Vues and Constats
* panels): the core lists what still names the element; if anything does, the dialog
* shows the list and offers no deletion (the core would refuse it); descendants go with
* the element and are listed in the confirmation.
*/
export default function DeleteDialog({ target, onClose }: DeleteDialogProps) {
const t = useT()
const doc = useSheetDocument()
const heading = t('editor.delete.title').replace(
'{name}',
target?.label ?? '',
)
return (
)
}
function DeleteBody({
target,
doc,
onClose,
}: {
target: DeleteTarget
doc: AnatomicalSheetDocument
onClose: () => void
}) {
const t = useT()
const [failure, setFailure] = useState(null)
const found = useMemo(
() => dependentsOf(useEditorStore.getState().documentText, target.key),
[target.key],
)
if (!found.ok) {
return (
)
}
const blocking = found.value.filter((d) => d.relation !== 'descendant')
const descendants = found.value.filter((d) => d.relation === 'descendant')
if (blocking.length > 0) {
return (
<>