import { jsx as _jsx } from "react/jsx-runtime";
import { useState, useEffect, useRef } from "react";
import { ReviewsList } from "./ReviewsList";
import { ReviewDetail } from "./ReviewDetail";
import { useMultiReview } from "./useMultiReview";
import { useEditContext } from "../client/editContext";
export function MultiReviewManager() {
    const editContext = useEditContext();
    const [selectedReviewId, setSelectedReviewId] = useState(null);
    const [hasPinnedReviewPanel, setHasPinnedReviewPanel] = useState(false);
    const hasStackedReviewSidebars = useRef(false);
    const multiReview = useMultiReview({
        language: editContext?.currentItemDescriptor?.language || "en",
        addSocketMessageListener: editContext?.addSocketMessageListener || (() => () => { }),
    });
    // Pin the review panel for reviewers opening via review link
    useEffect(() => {
        if (!editContext || hasPinnedReviewPanel)
            return;
        const isLimitedPreviewUser = editContext.user?.isLimitedPreviewUser ?? false;
        const urlParams = new URLSearchParams(window.location.search);
        const reviewIdFromUrl = urlParams.get("reviewId");
        // Only pin for reviewers (limited preview users) opening via review link
        if (isLimitedPreviewUser && reviewIdFromUrl) {
            const reviewsViewName = "reviews";
            const isAlreadyPinned = editContext.pinnedSidebars?.includes(reviewsViewName) ?? false;
            if (!isAlreadyPinned && editContext.toggleSidebarPin) {
                editContext.toggleSidebarPin(reviewsViewName);
                setHasPinnedReviewPanel(true);
            }
            else if (isAlreadyPinned) {
                // Already pinned, mark as done to avoid re-checking
                setHasPinnedReviewPanel(true);
            }
        }
    }, [editContext, hasPinnedReviewPanel]);
    // For reviewers opening direct review links, keep feedback + review in a single stacked column.
    useEffect(() => {
        if (!editContext || hasStackedReviewSidebars.current)
            return;
        const isLimitedPreviewUser = editContext.user?.isLimitedPreviewUser ?? false;
        const urlParams = new URLSearchParams(window.location.search);
        const reviewIdFromUrl = urlParams.get("reviewId");
        if (!isLimitedPreviewUser || !reviewIdFromUrl) {
            return;
        }
        // Ensure both sidebars are present, then stack reviews under feedback.
        editContext.openSidebar("feedback");
        editContext.openSidebar("reviews");
        editContext.stackSidebar("reviews", "feedback", "after");
        hasStackedReviewSidebars.current = true;
    }, [editContext]);
    // Handle deep-linking via URL query parameter
    useEffect(() => {
        const urlParams = new URLSearchParams(window.location.search);
        const reviewIdFromUrl = urlParams.get("reviewId");
        const isLimitedPreviewUser = editContext?.user?.isLimitedPreviewUser ?? false;
        if (reviewIdFromUrl && !selectedReviewId) {
            // For limited preview users (reviewers), always try to open the review from URL
            // even if it's not in their reviews list yet (e.g., secret-protected reviews)
            if (isLimitedPreviewUser) {
                // Preview users should always be able to open a review via direct link
                setSelectedReviewId(reviewIdFromUrl);
                multiReview.setSelectedReviewId(reviewIdFromUrl);
            }
            else if (multiReview.reviews.length > 0) {
                // For regular users, verify the review exists in the list
                const reviewExists = multiReview.reviews.some((r) => r.reviewId === reviewIdFromUrl);
                if (reviewExists) {
                    setSelectedReviewId(reviewIdFromUrl);
                    multiReview.setSelectedReviewId(reviewIdFromUrl);
                }
            }
        }
    }, [
        multiReview.reviews,
        selectedReviewId,
        multiReview,
        editContext?.user?.isLimitedPreviewUser,
    ]);
    // Handle browser back/forward navigation
    useEffect(() => {
        const handlePopState = () => {
            const urlParams = new URLSearchParams(window.location.search);
            const reviewIdFromUrl = urlParams.get("reviewId");
            if (reviewIdFromUrl) {
                // Navigated forward to a review
                setSelectedReviewId(reviewIdFromUrl);
                multiReview.setSelectedReviewId(reviewIdFromUrl);
            }
            else {
                // Navigated back to list view
                setSelectedReviewId(null);
                multiReview.setSelectedReviewId(null);
            }
        };
        window.addEventListener("popstate", handlePopState);
        return () => window.removeEventListener("popstate", handlePopState);
    }, [multiReview]);
    const handleReviewSelect = (reviewId) => {
        setSelectedReviewId(reviewId);
        multiReview.setSelectedReviewId(reviewId);
        // Update URL without reloading
        const url = new URL(window.location.href);
        url.searchParams.set("reviewId", reviewId);
        window.history.pushState({}, "", url.toString());
    };
    const handleBackToList = () => {
        setSelectedReviewId(null);
        multiReview.setSelectedReviewId(null);
        // Remove reviewId from URL
        const url = new URL(window.location.href);
        url.searchParams.delete("reviewId");
        window.history.pushState({}, "", url.toString());
    };
    if (selectedReviewId) {
        return (_jsx(ReviewDetail, { reviewId: selectedReviewId, onBack: handleBackToList, multiReview: multiReview }));
    }
    return (_jsx(ReviewsList, { reviews: multiReview.reviews, loading: multiReview.loading, error: multiReview.error, onSelectReview: handleReviewSelect, onCreateReview: () => {
            // Will be handled by CreateReviewDialog
        }, onRefresh: multiReview.refreshReviews }));
}
//# sourceMappingURL=MultiReviewManager.js.map