import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { InboxIcon, Rocket } from "lucide-react";
import { Button } from "../../components/ui/button";
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "../../components/ui/dropdown-menu";
import { Tooltip, TooltipContent, TooltipTrigger, } from "../../components/ui/tooltip";
import { cn } from "../../lib/utils";
import { useEditContext } from "../client/editContext";
import { Spinner } from "../ui/Spinner";
import { PublishDialog } from "../ui/PublishDialog";
export function WorkflowButton() {
    const editContext = useEditContext();
    const indicatorClassName = "shrink-0";
    if (!editContext?.currentItemDescriptor)
        return null;
    if (editContext?.user?.isLimitedPreviewUser)
        return null;
    // Find the current item in workboxItems to get its workflow commands
    const currentWorkboxItem = editContext.workboxItems?.find((item) => item.item.id === editContext.item?.id &&
        item.item.language === editContext.item?.language &&
        item.item.version === editContext.item?.version);
    if (editContext.isWorkboxLoading) {
        return (_jsx("div", { className: indicatorClassName, "data-testid": "publish-status-indicator", children: _jsx(Spinner, { size: "sm" }) }));
    }
    const workflowCommands = currentWorkboxItem?.workflowCommands || [];
    // Create tooltip content with publish status information
    const getTooltipContent = () => {
        if (!currentWorkboxItem)
            return "No status information available";
        const lines = [];
        // Add workflow state
        if (currentWorkboxItem.workflowState) {
            lines.push(`Workflow: ${currentWorkboxItem.workflowState}`);
        }
        // Add publish status for each target
        if (currentWorkboxItem.publishStatus) {
            const targets = Object.keys(currentWorkboxItem.publishStatus);
            if (targets.length > 0) {
                if (lines.length)
                    lines.push(""); // Empty line separator
                targets.forEach((target) => {
                    const status = currentWorkboxItem.publishStatus[target];
                    if (targets.length > 1) {
                        lines.push(`${target}: ${status}`);
                    }
                    else {
                        lines.push(`Status: ${status}`);
                    }
                });
            }
        }
        // Add additional status flags
        const statusFlags = [];
        if (currentWorkboxItem.isFinalState)
            statusFlags.push("Final State");
        if (currentWorkboxItem.isPublishable)
            statusFlags.push("Publishable");
        if (currentWorkboxItem.isPublished)
            statusFlags.push("Published");
        if (statusFlags.length > 0) {
            lines.push(""); // Empty line separator
            lines.push(statusFlags.join(", "));
        }
        return lines.join("\n") || "No status information available";
    };
    const isMobile = editContext?.isMobile ?? false;
    const workflowButtonColorClassName = currentWorkboxItem?.isFinalState
        ? "bg-feedback-green hover:bg-feedback-green"
        : undefined;
    // Opens the Publish dialog (workbox items list); it publishes the current
    // item directly.
    const openPublishDialog = () => {
        editContext.openDialog(PublishDialog, {});
    };
    // If no workflow commands, show simple publish button
    if (workflowCommands.length === 0) {
        if (isMobile) {
            return (_jsx("div", { className: indicatorClassName, "data-testid": "publish-status-indicator", children: _jsxs(Tooltip, { children: [_jsx(TooltipTrigger, { asChild: true, children: _jsx(Button, { size: "iconXs", className: cn("flex", workflowButtonColorClassName), "data-testid": "WorkboxNoCommandsButton", onClick: openPublishDialog, children: _jsx(Rocket, { className: "h-3.5 w-3.5", strokeWidth: 1.5 }) }) }), _jsx(TooltipContent, { children: _jsx("div", { className: "whitespace-pre-line", children: getTooltipContent() }) })] }) }));
        }
        return (_jsx("div", { className: indicatorClassName, "data-testid": "publish-status-indicator", children: _jsxs(Tooltip, { children: [_jsx(TooltipTrigger, { asChild: true, children: _jsxs(Button, { className: cn("flex", workflowButtonColorClassName), "data-testid": "WorkboxNoCommandsButton", onClick: openPublishDialog, children: [_jsx(Rocket, { className: "h-3.5 w-3.5", strokeWidth: 1.5 }), "Publish"] }) }), _jsx(TooltipContent, { children: _jsx("div", { className: "whitespace-pre-line", children: getTooltipContent() }) })] }) }));
    }
    // If only one workflow command, show it as a simple button
    if (workflowCommands.length === 1) {
        const command = workflowCommands[0];
        if (!command)
            return null;
        if (isMobile) {
            return (_jsx("div", { className: indicatorClassName, "data-testid": "publish-status-indicator", children: _jsxs(Tooltip, { children: [_jsx(TooltipTrigger, { asChild: true, children: _jsx(Button, { size: "iconXs", className: cn("flex", workflowButtonColorClassName), "data-testid": "WorkboxSingleCommandButton", onClick: () => {
                                    editContext.operations.executeWorkflowCommand(editContext.currentItemDescriptor, command.id);
                                }, children: _jsx(InboxIcon, { className: "h-3.5 w-3.5", strokeWidth: 1.5 }) }) }), _jsx(TooltipContent, { children: _jsx("div", { className: "whitespace-pre-line", children: getTooltipContent() }) })] }) }));
        }
        return (_jsx("div", { className: indicatorClassName, "data-testid": "publish-status-indicator", children: _jsxs(Tooltip, { children: [_jsx(TooltipTrigger, { asChild: true, children: _jsx(Button, { className: cn("flex", workflowButtonColorClassName), "data-testid": "WorkboxSingleCommandButton", onClick: () => {
                                editContext.operations.executeWorkflowCommand(editContext.currentItemDescriptor, command.id);
                            }, children: command.name }) }), _jsx(TooltipContent, { children: _jsx("div", { className: "whitespace-pre-line", children: getTooltipContent() }) })] }) }));
    }
    // Multiple workflow commands - show as dropdown
    const firstCommand = workflowCommands[0];
    if (!firstCommand)
        return null;
    const splitButtonGroupHoverClass = currentWorkboxItem?.isFinalState
        ? "group-hover:!bg-feedback-green"
        : "group-hover:!bg-highlight-100/80";
    if (isMobile) {
        return (_jsx("div", { className: indicatorClassName, "data-testid": "publish-status-indicator", children: _jsxs(DropdownMenu, { children: [_jsxs(Tooltip, { children: [_jsx(TooltipTrigger, { asChild: true, children: _jsx(DropdownMenuTrigger, { asChild: true, children: _jsx(Button, { size: "iconXs", className: cn("flex", workflowButtonColorClassName), "data-testid": "WorkboxMultipleCommandsDropdownTrigger", children: _jsx(InboxIcon, { className: "h-3.5 w-3.5", strokeWidth: 1.5 }) }) }) }), _jsx(TooltipContent, { children: _jsx("div", { className: "whitespace-pre-line", children: getTooltipContent() }) })] }), _jsx(DropdownMenuContent, { children: workflowCommands.map((command) => (_jsx(DropdownMenuItem, { onClick: () => {
                                editContext.operations.executeWorkflowCommand(editContext.currentItemDescriptor, command.id);
                            }, children: command.name }, command.id))) })] }) }));
    }
    return (_jsxs("div", { className: cn("group flex", indicatorClassName), "data-testid": "publish-status-indicator", children: [_jsxs(Tooltip, { children: [_jsx(TooltipTrigger, { asChild: true, children: _jsx(Button, { className: cn("inline-flex rounded-r-none !pr-2 shadow-none", splitButtonGroupHoverClass, workflowButtonColorClassName), "data-testid": "WorkboxMultipleCommandsButton", onClick: () => {
                                editContext.operations.executeWorkflowCommand(editContext.currentItemDescriptor, firstCommand.id);
                            }, children: firstCommand.name }) }), _jsx(TooltipContent, { children: _jsx("div", { className: "whitespace-pre-line", children: getTooltipContent() }) })] }), _jsxs(DropdownMenu, { children: [_jsx(DropdownMenuTrigger, { asChild: true, children: _jsx(Button, { className: cn("inline-flex rounded-l-none shadow-none has-[>svg]:!pr-3 has-[>svg]:!pl-2", splitButtonGroupHoverClass, workflowButtonColorClassName), "data-testid": "WorkboxMultipleCommandsDropdownTrigger", children: _jsx("svg", { className: "h-4 w-4", fill: "none", stroke: "currentColor", viewBox: "0 0 24 24", children: _jsx("path", { strokeLinecap: "round", strokeLinejoin: "round", strokeWidth: 2, d: "M19 9l-7 7-7-7" }) }) }) }), _jsx(DropdownMenuContent, { children: workflowCommands.map((command) => (_jsx(DropdownMenuItem, { onClick: () => {
                                editContext.operations.executeWorkflowCommand(editContext.currentItemDescriptor, command.id);
                            }, children: command.name }, command.id))) })] })] }));
}
//# sourceMappingURL=WorkflowButton.js.map