'use client';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/**
 * ContributorLeaderboard — spec 097, FR-38.
 *
 * Read-only leaderboard rendering top contributors by PR count for a given
 * scope (current month / all-time). Pure presentation: data is supplied via
 * props by a server component that calls GetContributorLeaderboardUseCase.
 */
import { Trophy, Crown } from 'lucide-react';
import { Badge } from '../ui/badge.js';
import { Skeleton } from '../ui/skeleton.js';
import { ContributorLevel } from '../../../../../packages/core/src/domain/generated/output.js';
const LEVEL_VARIANT = {
    [ContributorLevel.Maintainer]: 'default',
    [ContributorLevel.Core]: 'default',
    [ContributorLevel.Contributor]: 'secondary',
    [ContributorLevel.User]: 'outline',
};
const SCOPE_LABEL = {
    month: 'This month',
    allTime: 'All time',
};
export function ContributorLeaderboard({ scope, entries, loading, error, }) {
    return (_jsxs("section", { className: "flex flex-col gap-3", "data-testid": "contributor-leaderboard", "aria-label": "Contributor leaderboard", children: [_jsxs("header", { className: "flex items-center gap-2", children: [_jsx(Trophy, { className: "text-primary size-5", "aria-hidden": true }), _jsx("h2", { className: "text-lg font-semibold", children: "Top contributors" }), _jsx(Badge, { variant: "secondary", className: "ml-1", children: SCOPE_LABEL[scope] })] }), loading ? (_jsx(LoadingState, {})) : error ? (_jsx(ErrorState, { message: error })) : entries.length === 0 ? (_jsx(EmptyState, {})) : (_jsx("ol", { className: "flex flex-col divide-y rounded-lg border", "data-testid": "leaderboard-list", children: entries.map((entry, index) => (_jsx(LeaderboardRow, { rank: index + 1, entry: entry }, entry.login))) }))] }));
}
function LeaderboardRow({ rank, entry }) {
    return (_jsxs("li", { className: "flex items-center gap-3 px-3 py-2", "data-testid": `leaderboard-row-${entry.login}`, children: [_jsx("span", { className: "text-muted-foreground w-6 shrink-0 text-right font-mono text-xs", "data-testid": `leaderboard-rank-${entry.login}`, children: rank === 1 ? (_jsx(Crown, { className: "ml-auto size-3.5 text-amber-500", "aria-hidden": true })) : (`#${rank}`) }), _jsx(Avatar, { entry: entry }), _jsxs("div", { className: "flex min-w-0 flex-1 flex-col", children: [_jsx("p", { className: "truncate text-sm font-medium", "data-testid": `leaderboard-login-${entry.login}`, children: entry.displayName ?? entry.login }), _jsxs("p", { className: "text-muted-foreground text-xs", children: ["@", entry.login, entry.lane ? ` · ${entry.lane}` : ''] })] }), _jsx(Badge, { variant: LEVEL_VARIANT[entry.level] ?? 'outline', className: "shrink-0 capitalize", "data-testid": `leaderboard-level-${entry.login}`, children: entry.level }), _jsxs("span", { className: "w-12 shrink-0 text-right text-sm tabular-nums", "data-testid": `leaderboard-prs-${entry.login}`, children: [entry.prCount, " PR", entry.prCount === 1 ? '' : 's'] })] }));
}
function Avatar({ entry }) {
    const initial = (entry.displayName ?? entry.login).slice(0, 1).toUpperCase();
    if (entry.avatarUrl) {
        return (_jsx("span", { className: "size-7 shrink-0 overflow-hidden rounded-full border", "data-testid": `leaderboard-avatar-${entry.login}`, style: {
                backgroundImage: `url(${entry.avatarUrl})`,
                backgroundSize: 'cover',
                backgroundPosition: 'center',
            }, "aria-hidden": true }));
    }
    return (_jsx("span", { className: "bg-muted text-muted-foreground flex size-7 shrink-0 items-center justify-center rounded-full border text-xs font-medium", "data-testid": `leaderboard-avatar-${entry.login}`, "aria-hidden": true, children: initial }));
}
function LoadingState() {
    return (_jsx("ul", { className: "flex flex-col divide-y rounded-lg border", "data-testid": "leaderboard-loading", children: ['l1', 'l2', 'l3'].map((k) => (_jsxs("li", { className: "flex items-center gap-3 px-3 py-2", children: [_jsx(Skeleton, { className: "h-3 w-6" }), _jsx(Skeleton, { className: "size-7 rounded-full" }), _jsx(Skeleton, { className: "h-3 flex-1" }), _jsx(Skeleton, { className: "h-5 w-16" })] }, k))) }));
}
function EmptyState() {
    return (_jsx("p", { className: "text-muted-foreground rounded-lg border border-dashed p-4 text-sm", "data-testid": "leaderboard-empty", children: "No contributors yet \u2014 be the first to merge a PR!" }));
}
function ErrorState({ message }) {
    return (_jsxs("p", { className: "text-destructive bg-destructive/5 border-destructive/40 rounded-lg border p-4 text-sm", "data-testid": "leaderboard-error", role: "alert", children: ["Couldn't load the leaderboard: ", message] }));
}
