"use client"

import * as React from "react"
import { Button } from "@/components/ui/button"
import { Trash, CopyPlus, Plus } from "lucide-react"
import { type Table } from "@tanstack/react-table"
import type { PQList } from "./pq-lists-columns"
// import { PqListForm } from "./add-pq-list-dialog"

interface PQListsToolbarActionsProps {
  table: Table<PQList>;
  onAddClick: () => void;
  onCopyClick: () => void;
  onToggleActive: (rows: PQList[], newIsDeleted: boolean) => void;
}

export function PQListsToolbarActions({
  table,
  onAddClick,
  onCopyClick,
  onToggleActive,
}: PQListsToolbarActionsProps) {
  const selected = table.getFilteredSelectedRowModel().rows.map(r => r.original);
  const allActive = selected.length > 0 && selected.every(item => !item.isDeleted);
  const allDeleted = selected.length > 0 && selected.every(item => item.isDeleted);
  
  let toggleLabel = "";
  let newState: boolean | undefined;
  if (selected.length > 0) {
    if (allActive) {
      toggleLabel = "비활성화";
      newState = true;
    } else if (allDeleted) {
      toggleLabel = "활성화";
      newState = false;
    }
  }
  
  return (
    <div className="flex items-center gap-2">
      {selected.length > 0 && (allActive || allDeleted) && newState !== undefined && (
        <Button
          variant="outline"
          size="sm"
          onClick={() => onToggleActive(selected, newState!)}
        >
          <Trash className="mr-2 h-4 w-4" />
          {toggleLabel}
        </Button>
      )}
      <Button size="sm" onClick={onAddClick}>
        <Plus className="mr-2 h-4 w-4" />
        추가
      </Button>
      <Button size="sm" variant="outline" onClick={onCopyClick}>
        <CopyPlus className="mr-2 h-4 w-4" />
        복사
      </Button>
    </div>
  );
}
