"use client"

import { useState } from "react"

import type { GradeListItem } from "../types"

export type UseGradeDataReturn = {
  createOpen: boolean
  editItem: GradeListItem | null
  deleteItem: GradeListItem | null
  setCreateOpen: (open: boolean) => void
  setEditItem: (item: GradeListItem | null) => void
  setDeleteItem: (item: GradeListItem | null) => void
  isWorking: boolean
}

/**
 * 年级管理客户端的数据/状态 Hook。
 *
 * 集中管理创建/编辑/删除对话框的开关状态以及当前操作的年级项，
 * 供 GradesClient 组合容器及其子组件共享。
 *
 * `isWorking` 表示任意对话框处于打开状态，用于禁用工具栏按钮与行内操作菜单，
 * 避免并发打开多个对话框；各对话框内部的 mutation loading 由对应组件自行管理。
 */
export function useGradeData(): UseGradeDataReturn {
  const [createOpen, setCreateOpen] = useState(false)
  const [editItem, setEditItem] = useState<GradeListItem | null>(null)
  const [deleteItem, setDeleteItem] = useState<GradeListItem | null>(null)

  const isWorking = createOpen || Boolean(editItem) || Boolean(deleteItem)

  return {
    createOpen,
    editItem,
    deleteItem,
    setCreateOpen,
    setEditItem,
    setDeleteItem,
    isWorking,
  }
}
