import { useEffect, useState, useMemo, useCallback } from "react";
import { getReviews } from "../services/reviewsService";
export function useReviews({ enabled, currentItemDescriptor, addSocketMessageListener, }) {
    const [reviews, setReviews] = useState([]);
    const loadReviews = useCallback(async () => {
        if (!enabled || !currentItemDescriptor)
            return;
        const reviewsResult = await getReviews(currentItemDescriptor.id, currentItemDescriptor.language, currentItemDescriptor.version);
        if (reviewsResult.data) {
            setReviews(reviewsResult.data);
        }
    }, [enabled, currentItemDescriptor]);
    useEffect(() => {
        if (!enabled) {
            setReviews([]);
            return;
        }
        void loadReviews();
    }, [enabled, loadReviews]);
    useEffect(() => {
        if (!enabled) {
            return;
        }
        return addSocketMessageListener((message) => {
            if (message.type === "reviews-updated") {
                void loadReviews();
            }
        });
    }, [enabled, addSocketMessageListener, loadReviews]);
    return useMemo(() => ({ reviews, loadReviews }), [reviews, loadReviews]);
}
//# sourceMappingURL=useReviews.js.map