import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useState, useEffect } from "react";
import { Select } from "../../components/ui/select";
import { getPreconfiguredReviewers } from "../services/reviewsService";
import { Loader2 } from "lucide-react";
export function PreconfiguredReviewerSelector({ itemIds, language, onReviewerSelected, existingReviewers, }) {
    const [preconfiguredReviewers, setPreconfiguredReviewers] = useState([]);
    const [loading, setLoading] = useState(false);
    const [selectedValue, setSelectedValue] = useState("");
    useEffect(() => {
        if (itemIds.length === 0) {
            setPreconfiguredReviewers([]);
            return;
        }
        const fetchReviewers = async () => {
            setLoading(true);
            try {
                const result = await getPreconfiguredReviewers(itemIds, language);
                if (result.data) {
                    setPreconfiguredReviewers(result.data);
                }
            }
            catch (error) {
                console.error("Failed to fetch preconfigured reviewers:", error);
                setPreconfiguredReviewers([]);
            }
            finally {
                setLoading(false);
            }
        };
        fetchReviewers();
    }, [itemIds, language]);
    // Filter out reviewers that are already added (by email, case-insensitive)
    const availableReviewers = preconfiguredReviewers.filter((reviewer) => {
        return !existingReviewers.some((existing) => existing.email.toLowerCase() === reviewer.email.toLowerCase());
    });
    // Create options for the select dropdown
    const options = availableReviewers.map((reviewer) => ({
        value: reviewer.email,
        label: `${reviewer.name} (${reviewer.email})`,
    }));
    const handleSelection = (value) => {
        if (!value)
            return;
        const selectedReviewer = availableReviewers.find((r) => r.email === value);
        if (selectedReviewer) {
            onReviewerSelected(selectedReviewer);
            setSelectedValue(""); // Reset selection after adding
        }
    };
    return (_jsxs("div", { className: "my-2 flex flex-col gap-1", children: [_jsx("label", { className: "text-neutral-grey-100 text-xs font-medium", children: "Predefined Reviewers" }), loading ? (_jsxs("div", { className: "text-neutral-grey-50 flex items-center gap-2 text-xs", children: [_jsx(Loader2, { className: "h-4 w-4 animate-spin" }), _jsx("span", { children: "Loading reviewers..." })] })) : availableReviewers.length === 0 ? (_jsx("div", { className: "text-neutral-grey-50 text-xs", children: preconfiguredReviewers.length === 0
                    ? "No predefined reviewers configured for these items"
                    : "All predefined reviewers have been added" })) : (_jsx(Select, { value: selectedValue, onValueChange: handleSelection, options: options, placeholder: "Select a reviewer...", disabled: options.length === 0, emptyMessage: "No reviewers available" }))] }));
}
//# sourceMappingURL=PreconfiguredReviewerSelector.js.map