"use client"

import * as React from "react"
import { type Table } from "@tanstack/react-table"

import { DocumentClassOptionAddDialog } from "./document-class-option-add-dialog"
import { DeleteDocumentClassOptionDialog } from "./delete-document-class-option-dialog"
import { documentClasses, documentClassOptions } from "@/db/schema/documentClasses"

interface DocumentClassOptionsTableToolbarActionsProps<TData> {
  table: Table<TData>
  selectedDocumentClass: typeof documentClasses.$inferSelect | null
  onSuccess?: () => void
}

export function DocumentClassOptionsTableToolbarActions<TData>({
  table,
  selectedDocumentClass,
  onSuccess,
}: DocumentClassOptionsTableToolbarActionsProps<TData>) {
  const selectedRows = table.getFilteredSelectedRowModel().rows
  const selectedOptions = selectedRows.map((row) => row.original as typeof documentClassOptions.$inferSelect)

  return (
    <div className="flex items-center gap-2">
      {/** 선택된 로우가 있으면 삭제 다이얼로그 */}
      {selectedOptions.length > 0 ? (
        <DeleteDocumentClassOptionDialog
          options={selectedOptions}
          onSuccess={() => {
            table.toggleAllRowsSelected(false)
            onSuccess?.()
          }}
        />
      ) : null}

      <DocumentClassOptionAddDialog
        selectedDocumentClass={selectedDocumentClass}
        onSuccess={onSuccess}
      />
    </div>
  )
} 