'use client';
import { jsxs as _jsxs, jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
import { useState, useCallback } from 'react';
import { X, ChevronDown, Trash2, AlertCircle } from 'lucide-react';
import { Priority } from '../../../../../../packages/core/src/domain/generated/output.js';
import { Button } from '../../ui/button.js';
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger, } from '../../ui/dropdown-menu.js';
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from '../../ui/alert-dialog.js';
import { cn } from '../../../lib/utils.js';
import { useBulkSelection } from './bulk-selection-context.js';
const PRIORITY_VALUES = [
    Priority.Urgent,
    Priority.High,
    Priority.Medium,
    Priority.Low,
    Priority.None,
];
const PRIORITY_COLORS = {
    Urgent: 'text-red-500',
    High: 'text-orange-500',
    Medium: 'text-yellow-500',
    Low: 'text-blue-500',
    None: 'text-muted-foreground',
};
export function BulkActionToolbar({ states, onBulkAction }) {
    const { selectedIds, clearSelection } = useBulkSelection();
    const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
    const [feedback, setFeedback] = useState({ type: 'idle' });
    const selectedCount = selectedIds.size;
    const handleAction = useCallback(async (operation) => {
        const ids = Array.from(selectedIds);
        const actionLabel = operation.type === 'changeState'
            ? 'Changing state'
            : operation.type === 'changePriority'
                ? 'Changing priority'
                : 'Deleting';
        setFeedback({ type: 'loading', message: `${actionLabel}...` });
        const result = await onBulkAction(ids, operation);
        if (result.ok) {
            const count = result.succeeded.length;
            setFeedback({
                type: 'success',
                message: `Updated ${count} item${count !== 1 ? 's' : ''} successfully`,
            });
            clearSelection();
        }
        else if (result.error) {
            setFeedback({ type: 'error', message: result.error });
        }
        else {
            const failCount = result.failed.length;
            setFeedback({
                type: 'error',
                message: `${failCount} item${failCount !== 1 ? 's' : ''} failed to update`,
            });
        }
        setTimeout(() => setFeedback({ type: 'idle' }), 3000);
    }, [selectedIds, onBulkAction, clearSelection]);
    const handleDeleteConfirm = useCallback(() => {
        setDeleteDialogOpen(false);
        void handleAction({ type: 'delete' });
    }, [handleAction]);
    if (selectedCount === 0) {
        return null;
    }
    return (_jsxs(_Fragment, { children: [_jsxs("div", { "data-testid": "bulk-action-toolbar", className: cn('bg-background fixed bottom-6 left-1/2 z-50 flex -translate-x-1/2 items-center gap-2', 'rounded-lg border px-4 py-2 shadow-lg'), children: [_jsxs("span", { "data-testid": "bulk-selection-count", className: "text-sm font-medium", children: [selectedCount, " selected"] }), feedback.type !== 'idle' ? (_jsxs("span", { "data-testid": "bulk-action-feedback", className: cn('text-xs', feedback.type === 'loading' && 'text-muted-foreground', feedback.type === 'success' && 'text-green-600', feedback.type === 'error' && 'text-destructive'), children: [feedback.type === 'error' ? _jsx(AlertCircle, { className: "mr-1 inline size-3" }) : null, feedback.message] })) : null, _jsxs(DropdownMenu, { children: [_jsx(DropdownMenuTrigger, { asChild: true, children: _jsxs(Button, { variant: "outline", size: "sm", "data-testid": "bulk-change-state-trigger", disabled: feedback.type === 'loading', children: ["Change State", _jsx(ChevronDown, { className: "size-3" })] }) }), _jsxs(DropdownMenuContent, { align: "center", children: [_jsx(DropdownMenuLabel, { children: "Set state to" }), _jsx(DropdownMenuSeparator, {}), states.map((state) => (_jsxs(DropdownMenuItem, { "data-testid": `bulk-state-option-${state.id}`, onClick: () => void handleAction({ type: 'changeState', stateId: state.id }), children: [_jsx("span", { className: "h-2 w-2 shrink-0 rounded-full", style: { backgroundColor: state.color } }), state.name] }, state.id)))] })] }), _jsxs(DropdownMenu, { children: [_jsx(DropdownMenuTrigger, { asChild: true, children: _jsxs(Button, { variant: "outline", size: "sm", "data-testid": "bulk-change-priority-trigger", disabled: feedback.type === 'loading', children: ["Change Priority", _jsx(ChevronDown, { className: "size-3" })] }) }), _jsxs(DropdownMenuContent, { align: "center", children: [_jsx(DropdownMenuLabel, { children: "Set priority to" }), _jsx(DropdownMenuSeparator, {}), PRIORITY_VALUES.map((priority) => (_jsx(DropdownMenuItem, { "data-testid": `bulk-priority-option-${priority}`, onClick: () => void handleAction({ type: 'changePriority', priority }), children: _jsx("span", { className: cn('text-sm', PRIORITY_COLORS[priority]), children: priority }) }, priority)))] })] }), _jsxs(Button, { variant: "destructive", size: "sm", "data-testid": "bulk-delete-trigger", disabled: feedback.type === 'loading', onClick: () => setDeleteDialogOpen(true), children: [_jsx(Trash2, { className: "size-3" }), "Delete"] }), _jsx(Button, { variant: "ghost", size: "icon-sm", "data-testid": "bulk-clear-selection", onClick: clearSelection, "aria-label": "Clear selection", children: _jsx(X, { className: "size-4" }) })] }), _jsx(AlertDialog, { open: deleteDialogOpen, onOpenChange: setDeleteDialogOpen, children: _jsxs(AlertDialogContent, { children: [_jsxs(AlertDialogHeader, { children: [_jsxs(AlertDialogTitle, { children: ["Delete ", selectedCount, " work item", selectedCount !== 1 ? 's' : '', "?"] }), _jsx(AlertDialogDescription, { children: "This will delete the selected work items. This action cannot be easily undone." })] }), _jsxs(AlertDialogFooter, { children: [_jsx(AlertDialogCancel, { children: "Cancel" }), _jsx(AlertDialogAction, { variant: "destructive", "data-testid": "bulk-delete-confirm", onClick: handleDeleteConfirm, children: "Delete" })] })] }) })] }));
}
