"use client"

import type { useKpDialogState } from "./use-kp-dialog-state"
import { useKpCreate } from "./use-kp-create"
import { useKpDelete } from "./use-kp-delete"
import { useKpUpdate } from "./use-kp-update"

type DialogState = ReturnType<typeof useKpDialogState>

interface UseKpCrudArgs {
  textbookId: string | undefined
  selectedChapterId: string | null
  selectedChapterTextbookId: string | undefined
  highlightedKpId: string | null
  setHighlightedKpId: (id: string | null) => void
  onKpCreated?: () => void
  dialog: DialogState
}

/**
 * 知识点 CRUD 操作 Hook（门面）。
 *
 * 组合 useKpCreate / useKpDelete / useKpUpdate 三个子 Hook，
 * 对外保持原有 API 不变。拆分后每个子 Hook 均在 80 行限制内。
 */
export function useKpCrud({
  textbookId,
  selectedChapterId,
  selectedChapterTextbookId,
  highlightedKpId,
  setHighlightedKpId,
  onKpCreated,
  dialog,
}: UseKpCrudArgs) {
  const { handleCreateKnowledgePoint } = useKpCreate({
    selectedChapterId,
    selectedChapterTextbookId,
    onKpCreated,
  })

  const { requestDeleteKnowledgePoint, confirmDeleteKnowledgePoint } = useKpDelete({
    textbookId,
    highlightedKpId,
    setHighlightedKpId,
    dialog,
  })

  const { handleUpdateKnowledgePoint } = useKpUpdate({
    textbookId,
    dialog,
  })

  return {
    handleCreateKnowledgePoint,
    requestDeleteKnowledgePoint,
    confirmDeleteKnowledgePoint,
    handleUpdateKnowledgePoint,
  }
}
