"use client"

import { useCallback } from "react"
import type { JSX } from "react"

import { AddErrorBookDialog } from "@/modules/error-book/components/add-error-book-dialog"
import { getQuestionsAction } from "@/modules/questions/actions"
import { extractQuestionPreview } from "@/shared/lib/question-content"

/**
 * 手动添加错题对话框包装组件（位于 app 层）。
 *
 * 负责从 questions 模块加载题目列表并注入到 AddErrorBookDialog，
 * 避免 error-book 模块直接 import questions 模块（解耦）。
 * app 层可以 import 任何模块，因此跨模块依赖在此合法。
 */
export function AddErrorBookDialogWithQuestions(): JSX.Element {
  const onLoadQuestions = useCallback(async (): Promise<
    Array<{ id: string; preview: string }>
  > => {
    const res = await getQuestionsAction({ pageSize: 100 })
    if (res.success && res.data) {
      return res.data.data.map((q) => ({
        id: q.id,
        preview: extractQuestionPreview(q.content, "", 60),
      }))
    }
    return []
  }, [])

  return <AddErrorBookDialog onLoadQuestions={onLoadQuestions} />
}
