import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Avatar } from "../../components/ui/avatar";
import { Badge } from "../../components/ui/badge";
import { Button } from "../../components/ui/button";
import { DeleteIcon } from "../../components/ui/delete-icon";
import { ClipboardCheck, Globe, Lock, Users } from "lucide-react";
import { formatDate } from "../utils";
import { formatReviewSummaryList, getReviewDecisionProgressStatus, getReviewDisplayStatus, getReviewLanguages, getReviewerDisplayName, normalizeReviewIdentity, } from "./reviewListPresentation";
const reviewerAvatarClasses = [
    "bg-feedback-blue text-white",
    "bg-feedback-orange text-white",
    "bg-highlight-100 text-white",
    "bg-feedback-green text-white",
    "bg-feedback-red text-white",
];
function getReviewerAvatarClassName(reviewer) {
    const identity = reviewer.email || reviewer.userName || reviewer.name || "reviewer";
    const index = Array.from(identity).reduce((total, character) => total + character.charCodeAt(0), 0) % reviewerAvatarClasses.length;
    return reviewerAvatarClasses[index];
}
function getStatusPresentation(review) {
    const status = getReviewDisplayStatus(review);
    switch (status) {
        case "approved":
            return {
                label: "Approved",
                className: "bg-feedback-green-light text-feedback-green",
            };
        case "rejected":
            return {
                label: "Rejected",
                className: "bg-feedback-red-light text-feedback-red",
            };
        case "expired":
            return {
                label: "Expired",
                className: "bg-neutral-grey-10 text-neutral-grey-50",
            };
        default:
            return {
                label: "In progress",
                className: "bg-feedback-orange-light text-feedback-orange",
            };
    }
}
function getDecisionProgressPresentation(review) {
    const status = getReviewDecisionProgressStatus(review);
    switch (status) {
        case "completed":
            return {
                label: "Completed",
                className: "bg-neutral-grey-100 text-white",
            };
        case "in-progress":
            return {
                label: "In progress",
                className: "bg-feedback-orange-light text-feedback-orange",
            };
        case "open":
            return {
                label: "Open",
                className: "bg-neutral-grey-10 text-neutral-grey-50",
            };
        default:
            return null;
    }
}
function reviewerMatchesUser(reviewer, user) {
    const identities = [user?.email, user?.name, user?.fullName]
        .map(normalizeReviewIdentity)
        .filter(Boolean);
    return [reviewer.email, reviewer.userName, reviewer.name]
        .map(normalizeReviewIdentity)
        .some((identity) => identity && identities.includes(identity));
}
export function ReviewCard({ review, currentUser, onClick, onDelete, canDelete, }) {
    const status = getStatusPresentation(review);
    const reviewers = review.reviewers ?? [];
    const visibleReviewers = reviewers.slice(0, 3);
    const remainingReviewers = Math.max(review.reviewersCount - visibleReviewers.length, 0);
    const languages = getReviewLanguages(review);
    const decisionProgress = getDecisionProgressPresentation(review);
    return (_jsxs("div", { "data-testid": "review-card", "data-review-id": review.reviewId, onClick: () => onClick(review.reviewId), className: "group border-border-default cursor-pointer overflow-hidden rounded-xl border bg-white transition-[border-color,box-shadow] hover:shadow-sm", children: [_jsxs("div", { className: "p-3.5", children: [_jsxs("div", { className: "mb-2.5 flex min-h-6 items-center gap-2", children: [_jsx(Badge, { variant: "secondary", size: "sm", className: status.className, "data-testid": "review-card-status", children: status.label }), review.requiresSecret && (_jsx(Lock, { className: "text-neutral-grey-50 h-3.5 w-3.5", strokeWidth: 1, "aria-label": "Secret required" })), _jsxs("div", { className: "ml-auto flex items-center gap-1.5", children: [decisionProgress && (_jsx(Badge, { variant: "secondary", className: decisionProgress.className, "data-testid": "review-card-decision-progress", children: decisionProgress.label })), canDelete && (_jsx(Button, { variant: "ghost", size: "iconXs", onClick: (event) => onDelete(review.reviewId, event), "aria-label": `Delete ${review.title || "review"}`, title: "Delete review", className: "text-neutral-grey-50 hover:bg-feedback-red-light hover:text-feedback-red -my-1 -mr-1 opacity-0 transition-opacity group-hover:opacity-100 focus-visible:opacity-100", children: _jsx(DeleteIcon, { size: "sm" }) }))] })] }), _jsx("h4", { "data-testid": "review-card-title", className: "text-neutral-grey-100 truncate text-sm font-medium", title: review.title || undefined, children: review.title || `Review ${review.reviewId.slice(0, 8)}` }), _jsxs("div", { className: "mt-3 flex min-w-0 items-center gap-3 text-xs", children: [_jsx("div", { className: "flex shrink-0 items-center", title: reviewers.length > 0
                                    ? reviewers.map(getReviewerDisplayName).join(", ")
                                    : `${review.reviewersCount} reviewers`, children: visibleReviewers.length > 0 ? (_jsxs(_Fragment, { children: [_jsx("div", { className: "flex -space-x-2", children: visibleReviewers.map((reviewer, index) => (_jsx(Avatar, { src: reviewerMatchesUser(reviewer, currentUser)
                                                    ? currentUser?.avatarUrl
                                                    : undefined, name: getReviewerDisplayName(reviewer), fallbackClassName: getReviewerAvatarClassName(reviewer), bordered: true, className: "size-7 text-[10px]" }, reviewer.email ||
                                                reviewer.userName ||
                                                reviewer.name ||
                                                index))) }), remainingReviewers > 0 && (_jsxs("span", { className: "text-neutral-grey-100 ml-1.5 font-medium", children: ["+", remainingReviewers] }))] })) : (_jsxs("span", { className: "text-neutral-grey-50 flex items-center gap-1.5", children: [_jsx(Users, { className: "h-4 w-4", strokeWidth: 1 }), review.reviewersCount] })) }), _jsxs("div", { className: "text-neutral-grey-100 flex min-w-0 items-center gap-1.5", title: languages.join(", "), children: [_jsx(Globe, { className: "h-4 w-4 shrink-0", strokeWidth: 1 }), _jsx("span", { className: "truncate", children: formatReviewSummaryList(languages, 2) })] }), _jsxs("div", { className: "text-neutral-grey-100 flex shrink-0 items-center gap-1.5", title: `${review.pagesCount} page${review.pagesCount === 1 ? "" : "s"}`, children: [_jsx(ClipboardCheck, { className: "h-4 w-4", strokeWidth: 1 }), _jsx("span", { children: review.pagesCount })] })] })] }), _jsxs("div", { className: "border-border-default text-neutral-grey-50 border-t px-3.5 py-2.5 text-[11px] leading-relaxed", children: [_jsxs("div", { className: "truncate", children: ["Created: ", formatDate(new Date(review.createdAt)), " by", " ", review.createdBy] }), review.expiresAtUtc && !review.isExpired && (_jsxs("div", { className: "truncate", children: ["Expires: ", formatDate(new Date(review.expiresAtUtc))] }))] })] }));
}
//# sourceMappingURL=ReviewCard.js.map