"use client"

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

import { DeleteCodeGroupsDialog } from "./delete-code-groups-dialog"
import { CodeGroupsAddDialog } from "./code-groups-add-dialog"
import { codeGroups } from "@/db/schema/codeGroups"

interface CodeGroupsTableToolbarActionsProps<TData> {
  table: Table<TData>
  onSuccess?: () => void
}

export function CodeGroupsTableToolbarActions<TData>({
  table,
  onSuccess,
}: CodeGroupsTableToolbarActionsProps<TData>) {
  const selectedRows = table.getFilteredSelectedRowModel().rows
  const selectedCodeGroups = selectedRows.map((row) => row.original as typeof codeGroups.$inferSelect)

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

      <CodeGroupsAddDialog onSuccess={onSuccess} />
    </div>
  )
} 