import type { Editor } from '@tiptap/core'
import { isAtEndOfNode, isNodeActive } from '@tiptap/core'
import { joinTextblockForward } from '@tiptap/pm/commands'

import { nextListIsDeeper } from './nextListIsDeeper.js'
import { nextListIsHigher } from './nextListIsHigher.js'

export const handleDelete = (editor: Editor, name: string) => {
  // if the cursor is not inside the current node type
  // do nothing and proceed
  if (!isNodeActive(editor.state, name)) {
    return false
  }

  // if the cursor is not at the end of a node
  // do nothing and proceed
  if (!isAtEndOfNode(editor.state, name)) {
    return false
  }

  // if the selection is not collapsed
  // we can rely on the default delete behavior
  if (!editor.state.selection.empty) {
    return false
  }

  // check if the next node is a list with a deeper depth
  if (nextListIsDeeper(name, editor.state)) {
    return editor
      .chain()
      .focus(editor.state.selection.from + 4)
      .lift(name)
      .joinBackward()
      .run()
  }

  if (nextListIsHigher(name, editor.state)) {
    return editor.commands.command(({ state, dispatch }) => joinTextblockForward(state, dispatch))
  }

  return editor.commands.joinItemForward()
}
