'use client';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useMemo } from 'react';
import { AlertCircle, ExternalLink, FileCode2, MessageSquare } from 'lucide-react';
import { Button } from '../../ui/button.js';
import { cn } from '../../../lib/utils.js';
import { CodeReviewStatus } from '../../../../../../packages/core/src/domain/generated/output.js';
import { ReviewStatusBadge } from './review-status-badge.js';
import { ReviewCommentCard } from './review-comment-card.js';
/** Group comments by file path, preserving order of first appearance. */
function groupByFile(comments) {
    const grouped = new Map();
    for (const comment of comments) {
        const existing = grouped.get(comment.path);
        if (existing) {
            existing.push(comment);
        }
        else {
            grouped.set(comment.path, [comment]);
        }
    }
    return grouped;
}
export function CodeReviewPanel({ review, loading, error, onPostToGitHub, postingInProgress, className, }) {
    const groupedComments = useMemo(() => {
        if (!review?.comments?.length)
            return new Map();
        return groupByFile(review.comments);
    }, [review?.comments]);
    const commentCount = review?.comments?.length ?? 0;
    // Loading state
    if (loading) {
        return (_jsxs("div", { className: cn('space-y-4 p-4', className), children: [_jsxs("div", { className: "flex items-center gap-3", children: [_jsx("div", { className: "bg-muted h-5 w-20 animate-pulse rounded-full" }), _jsx("div", { className: "bg-muted h-4 w-32 animate-pulse rounded" })] }), _jsx("div", { className: "bg-muted h-16 animate-pulse rounded-lg" }), _jsxs("div", { className: "space-y-3", children: [_jsx("div", { className: "bg-muted h-24 animate-pulse rounded-lg" }), _jsx("div", { className: "bg-muted h-24 animate-pulse rounded-lg" })] })] }));
    }
    // Error state
    if (error) {
        return (_jsxs("div", { className: cn('flex items-center gap-2 rounded-lg border border-red-200 bg-red-50 p-4 text-sm text-red-700 dark:border-red-800 dark:bg-red-950/30 dark:text-red-400', className), children: [_jsx(AlertCircle, { className: "h-4 w-4 shrink-0" }), error] }));
    }
    // No review
    if (!review) {
        return null;
    }
    // Empty review (completed but no comments)
    const isEmpty = review.status === CodeReviewStatus.Completed && commentCount === 0;
    const canPost = review.status === CodeReviewStatus.Completed && onPostToGitHub;
    return (_jsxs("div", { className: cn('space-y-4', className), children: [_jsxs("div", { className: "flex items-center gap-3", children: [_jsx(ReviewStatusBadge, { status: review.status }), _jsxs("span", { className: "text-muted-foreground flex items-center gap-1 text-xs", children: [_jsx(MessageSquare, { className: "h-3.5 w-3.5" }), commentCount, " ", commentCount === 1 ? 'finding' : 'findings'] }), review.reviewUrl ? (_jsxs("a", { href: review.reviewUrl, target: "_blank", rel: "noopener noreferrer", className: "ml-auto flex items-center gap-1 text-xs text-blue-600 hover:underline dark:text-blue-400", children: [_jsx(ExternalLink, { className: "h-3 w-3" }), "View on GitHub"] })) : null, canPost ? (_jsx(Button, { size: "sm", variant: "outline", className: "ml-auto", onClick: () => onPostToGitHub(review.id), disabled: postingInProgress, children: postingInProgress ? 'Posting...' : 'Post to GitHub' })) : null] }), review.summary ? (_jsx("div", { className: "bg-muted/50 rounded-lg border p-3 text-sm", children: review.summary })) : null, review.status === CodeReviewStatus.Failed && review.errorMessage ? (_jsxs("div", { className: "flex items-center gap-2 rounded-lg border border-red-200 bg-red-50 p-3 text-sm text-red-700 dark:border-red-800 dark:bg-red-950/30 dark:text-red-400", children: [_jsx(AlertCircle, { className: "h-4 w-4 shrink-0" }), review.errorMessage] })) : null, isEmpty ? (_jsxs("div", { className: "text-muted-foreground flex flex-col items-center gap-2 py-8 text-center", children: [_jsx(FileCode2, { className: "h-8 w-8" }), _jsx("p", { className: "text-sm", children: "No findings \u2014 the code looks good!" })] })) : null, groupedComments.size > 0 ? (_jsx("div", { className: "space-y-4", children: Array.from(groupedComments.entries()).map(([filePath, comments]) => (_jsxs("div", { className: "space-y-2", children: [_jsxs("h4", { className: "text-muted-foreground flex items-center gap-1.5 text-xs font-medium", children: [_jsx(FileCode2, { className: "h-3.5 w-3.5" }), filePath, _jsx("span", { className: "bg-muted rounded px-1 py-0.5", children: comments.length })] }), _jsx("div", { className: "space-y-2", children: comments.map((comment) => (_jsx(ReviewCommentCard, { comment: comment }, `${comment.path}:${comment.line}:${comment.side}`))) })] }, filePath))) })) : null] }));
}
