"use client";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { useState } from "react";
import { cn } from "../../lib/utils";
import { Button } from "../ui/button";
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue
} from "../ui/select";
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle
} from "../ui/dialog";
import { CheckSquare, Pencil, Trash2, X, Loader2 } from "lucide-react";
import { createCyId } from "../../lib/test";
function EntityBulkActions({
  entitySlug,
  selectedIds,
  onClearSelection,
  config
}) {
  var _a;
  const [isStatusDialogOpen, setIsStatusDialogOpen] = useState(false);
  const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false);
  const [selectedStatus, setSelectedStatus] = useState("");
  const [isProcessing, setIsProcessing] = useState(false);
  const selectedCount = selectedIds.size;
  const itemLabel = config.itemLabel || "item";
  const itemLabelPlural = config.itemLabelPlural || "items";
  if (selectedCount === 0) {
    return null;
  }
  const handleChangeStatusClick = () => {
    setSelectedStatus("");
    setIsStatusDialogOpen(true);
  };
  const handleDeleteClick = () => {
    setIsDeleteDialogOpen(true);
  };
  const handleConfirmStatusChange = async () => {
    if (!selectedStatus || !config.onChangeStatus) return;
    setIsProcessing(true);
    try {
      await config.onChangeStatus(selectedStatus, Array.from(selectedIds));
      setIsStatusDialogOpen(false);
      onClearSelection();
    } catch (error) {
      console.error("[EntityBulkActions] Error changing status:", error);
    } finally {
      setIsProcessing(false);
    }
  };
  const handleConfirmDelete = async () => {
    if (!config.onDelete) return;
    setIsProcessing(true);
    try {
      await config.onDelete(Array.from(selectedIds));
      setIsDeleteDialogOpen(false);
      onClearSelection();
    } catch (error) {
      console.error("[EntityBulkActions] Error deleting items:", error);
    } finally {
      setIsProcessing(false);
    }
  };
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsxs(
      "div",
      {
        className: cn(
          "fixed bottom-6 left-1/2 -translate-x-1/2 z-50",
          "flex items-center gap-2 px-4 py-3",
          "bg-background border rounded-lg shadow-lg",
          "animate-in slide-in-from-bottom-4 duration-200"
        ),
        "data-cy": createCyId(entitySlug, "bulk-bar"),
        children: [
          /* @__PURE__ */ jsxs(
            "span",
            {
              className: "text-sm font-medium px-2 py-1 bg-primary/10 text-primary rounded",
              "data-cy": createCyId(entitySlug, "bulk-count"),
              children: [
                selectedCount,
                " selected"
              ]
            }
          ),
          /* @__PURE__ */ jsx("div", { className: "h-6 w-px bg-border mx-1" }),
          config.enableSelectAll && config.onSelectAll && /* @__PURE__ */ jsxs(
            Button,
            {
              variant: "ghost",
              size: "sm",
              onClick: config.onSelectAll,
              className: "gap-2",
              "data-cy": createCyId(entitySlug, "bulk-select-all"),
              children: [
                /* @__PURE__ */ jsx(CheckSquare, { className: "h-4 w-4" }),
                "Select all",
                config.totalItems !== void 0 && /* @__PURE__ */ jsxs("span", { className: "text-muted-foreground", children: [
                  "(",
                  config.totalItems,
                  ")"
                ] })
              ]
            }
          ),
          config.enableSelectAll && (config.enableChangeStatus || config.enableDelete) && /* @__PURE__ */ jsx("div", { className: "h-6 w-px bg-border mx-1" }),
          config.enableChangeStatus && config.statusOptions && config.statusOptions.length > 0 && /* @__PURE__ */ jsxs(
            Button,
            {
              variant: "ghost",
              size: "sm",
              onClick: handleChangeStatusClick,
              className: "gap-2",
              "data-cy": createCyId(entitySlug, "bulk-status"),
              children: [
                /* @__PURE__ */ jsx(Pencil, { className: "h-4 w-4" }),
                "Change status"
              ]
            }
          ),
          config.enableDelete && config.onDelete && /* @__PURE__ */ jsxs(
            Button,
            {
              variant: "ghost",
              size: "sm",
              onClick: handleDeleteClick,
              className: "gap-2 text-destructive hover:text-destructive hover:bg-destructive/10",
              "data-cy": createCyId(entitySlug, "bulk-delete"),
              children: [
                /* @__PURE__ */ jsx(Trash2, { className: "h-4 w-4" }),
                "Delete"
              ]
            }
          ),
          /* @__PURE__ */ jsx("div", { className: "h-6 w-px bg-border mx-1" }),
          /* @__PURE__ */ jsx(
            Button,
            {
              variant: "ghost",
              size: "icon",
              onClick: onClearSelection,
              className: "h-8 w-8",
              "aria-label": "Clear selection",
              "data-cy": createCyId(entitySlug, "bulk-clear"),
              children: /* @__PURE__ */ jsx(X, { className: "h-4 w-4" })
            }
          )
        ]
      }
    ),
    /* @__PURE__ */ jsx(Dialog, { open: isStatusDialogOpen, onOpenChange: setIsStatusDialogOpen, children: /* @__PURE__ */ jsxs(DialogContent, { "data-cy": createCyId(entitySlug, "bulk-status-dialog"), children: [
      /* @__PURE__ */ jsxs(DialogHeader, { children: [
        /* @__PURE__ */ jsx(DialogTitle, { children: "Change Status" }),
        /* @__PURE__ */ jsxs(DialogDescription, { children: [
          "Change the status of ",
          selectedCount,
          " ",
          selectedCount === 1 ? itemLabel : itemLabelPlural,
          "."
        ] })
      ] }),
      /* @__PURE__ */ jsxs("div", { className: "py-4", children: [
        /* @__PURE__ */ jsx("label", { className: "text-sm font-medium mb-2 block", children: "New Status" }),
        /* @__PURE__ */ jsxs(Select, { value: selectedStatus, onValueChange: setSelectedStatus, children: [
          /* @__PURE__ */ jsx(SelectTrigger, { "data-cy": createCyId(entitySlug, "bulk-status-select"), children: /* @__PURE__ */ jsx(SelectValue, { placeholder: "Select a status..." }) }),
          /* @__PURE__ */ jsx(SelectContent, { children: (_a = config.statusOptions) == null ? void 0 : _a.map((option) => /* @__PURE__ */ jsx(
            SelectItem,
            {
              value: option.value,
              "data-cy": createCyId(entitySlug, `bulk-status-option-${option.value}`),
              children: option.label
            },
            option.value
          )) })
        ] })
      ] }),
      /* @__PURE__ */ jsxs(DialogFooter, { children: [
        /* @__PURE__ */ jsx(
          Button,
          {
            variant: "outline",
            onClick: () => setIsStatusDialogOpen(false),
            disabled: isProcessing,
            "data-cy": createCyId(entitySlug, "bulk-status-cancel"),
            children: "Cancel"
          }
        ),
        /* @__PURE__ */ jsx(
          Button,
          {
            onClick: handleConfirmStatusChange,
            disabled: !selectedStatus || isProcessing,
            "data-cy": createCyId(entitySlug, "bulk-status-confirm"),
            children: isProcessing ? /* @__PURE__ */ jsxs(Fragment, { children: [
              /* @__PURE__ */ jsx(Loader2, { className: "mr-2 h-4 w-4 animate-spin" }),
              "Updating..."
            ] }) : "Update Status"
          }
        )
      ] })
    ] }) }),
    /* @__PURE__ */ jsx(Dialog, { open: isDeleteDialogOpen, onOpenChange: setIsDeleteDialogOpen, children: /* @__PURE__ */ jsxs(DialogContent, { "data-cy": createCyId(entitySlug, "bulk-delete-dialog"), children: [
      /* @__PURE__ */ jsxs(DialogHeader, { children: [
        /* @__PURE__ */ jsxs(DialogTitle, { children: [
          "Delete ",
          selectedCount,
          " ",
          selectedCount === 1 ? itemLabel : itemLabelPlural,
          "?"
        ] }),
        /* @__PURE__ */ jsxs(DialogDescription, { children: [
          "Are you sure you want to delete ",
          selectedCount,
          " ",
          selectedCount === 1 ? itemLabel : itemLabelPlural,
          "? This action cannot be undone."
        ] })
      ] }),
      /* @__PURE__ */ jsxs(DialogFooter, { children: [
        /* @__PURE__ */ jsx(
          Button,
          {
            variant: "outline",
            onClick: () => setIsDeleteDialogOpen(false),
            disabled: isProcessing,
            "data-cy": createCyId(entitySlug, "bulk-delete-cancel"),
            children: "Cancel"
          }
        ),
        /* @__PURE__ */ jsx(
          Button,
          {
            variant: "destructive",
            onClick: handleConfirmDelete,
            disabled: isProcessing,
            "data-cy": createCyId(entitySlug, "bulk-delete-confirm"),
            children: isProcessing ? /* @__PURE__ */ jsxs(Fragment, { children: [
              /* @__PURE__ */ jsx(Loader2, { className: "mr-2 h-4 w-4 animate-spin" }),
              "Deleting..."
            ] }) : "Delete"
          }
        )
      ] })
    ] }) })
  ] });
}
export {
  EntityBulkActions
};
