import { CheckCircle, AlertCircle, Clock } from "lucide-react";
import { createElement } from "react";
/**
 * Returns the appropriate icon for a batch status
 */
export function getBatchStatusIcon(status) {
    switch (status?.toLowerCase()) {
        case "completed":
            return createElement(CheckCircle, {
                className: "h-4 w-4 text-green-500",
                strokeWidth: 1,
            });
        case "failed":
            return createElement(AlertCircle, {
                className: "h-4 w-4 text-red-500",
                strokeWidth: 1,
            });
        case "processing":
            return createElement(Clock, {
                className: "h-4 w-4 text-blue-500",
                strokeWidth: 1,
            });
        default:
            return createElement(AlertCircle, {
                className: "h-4 w-4 text-yellow-500",
                strokeWidth: 1,
            });
    }
}
/**
 * Returns the CSS classes for a batch status label
 */
export function getBatchStatusLabelStyles(status) {
    switch (status?.toLowerCase()) {
        case "completed":
            return "bg-green-100 text-green-700";
        case "failed":
            return "bg-red-100 text-red-700";
        case "processing":
            return "bg-blue-100 text-blue-700";
        default:
            return "bg-yellow-100 text-yellow-700";
    }
}
/**
 * Returns index health status (Ready or configuration errors)
 */
export function getIndexStatus(indexStatus) {
    if (indexStatus?.messages != null &&
        indexStatus.messages.length > 0 &&
        indexStatus.messages[0]?.message) {
        return {
            statusLabel: indexStatus.messages[0].message,
            statusClass: "text-red-600",
            hasSettingsIssue: true,
        };
    }
    else {
        return {
            statusLabel: "Ready",
            statusClass: "text-green-600",
            hasSettingsIssue: false,
        };
    }
}
/**
 * Returns rebuild progress status (or null if no rebuild operation active)
 */
export function getRebuildStatus(isCollecting, isGenerating, indexStatus, importStatus, centroidsStatus, collectionStatus, queueStatus, debouncedCurrentItem) {
    const batches = indexStatus?.batches || [];
    const inProgress = batches.some((b) => {
        const s = (b.status || "").toLowerCase();
        return s === "submitted" || s === "processing" || s === "queued";
    });
    // Use backend collection status for detailed progress, fall back to local state
    const actuallyCollecting = collectionStatus?.isCollecting || isCollecting;
    if (actuallyCollecting) {
        // Build a detailed status label from the backend status
        // Only recalculate when subPhase changes, not when currentItem changes
        const subPhase = collectionStatus?.subPhase;
        let statusLabel = collectionStatus?.status || "Collecting items";
        // Create a more user-friendly status based on subPhase
        // This only changes when subPhase changes, preventing flickering
        if (subPhase === "scanning") {
            statusLabel = "Scanning content tree";
        }
        else if (subPhase === "processing") {
            const batch = collectionStatus?.currentBatch;
            statusLabel = batch ? `Processing batch ${batch}` : "Processing items";
        }
        else if (subPhase === "extracting") {
            statusLabel = "Extracting content from items";
        }
        else if (subPhase === "staging") {
            statusLabel = "Staging items for embedding generation";
        }
        else if (subPhase === "complete") {
            statusLabel = "Collection complete";
        }
        const details = [];
        if (collectionStatus?.itemsProcessed &&
            collectionStatus.itemsProcessed > 0) {
            details.push(`${collectionStatus.itemsProcessed.toLocaleString()} items staged`);
        }
        if (collectionStatus?.languageVersionsProcessed &&
            collectionStatus.languageVersionsProcessed > 0) {
            details.push(`${collectionStatus.languageVersionsProcessed.toLocaleString()} versions scanned`);
        }
        // Show current item being processed (use debounced version to prevent flickering)
        if (debouncedCurrentItem) {
            // Truncate path if too long
            const displayPath = debouncedCurrentItem.length > 40
                ? "..." + debouncedCurrentItem.slice(-37)
                : debouncedCurrentItem;
            details.push(displayPath);
        }
        return {
            statusLabel,
            statusClass: "text-indigo-600",
            errorCount: undefined,
            recentErrors: undefined,
            showSpinner: true,
            progressDetails: details.length > 0 ? details : undefined,
        };
    }
    else if (queueStatus?.isRunning && (queueStatus.pendingCount ?? 0) > 0) {
        const pending = queueStatus.pendingCount ?? 0;
        const hasErrors = (queueStatus.errorCount ?? 0) > 0;
        // Queue processor is running; only show spinner when there is actual work left.
        const details = [];
        if (pending > 0) {
            if ((queueStatus.processedCount ?? 0) > 0) {
                details.push(`${queueStatus.processedCount.toLocaleString()} processed`);
            }
            details.push(`${pending.toLocaleString()} pending`);
        }
        if ((queueStatus.errorCount ?? 0) > 0) {
            details.push(`${queueStatus.errorCount} errors`);
        }
        // If there are no pending items, treat this as idle/stopped for UI purposes.
        const showSpinner = pending > 0;
        const statusLabel = queueStatus.status || (pending > 0 ? "Generating embeddings" : "Idle");
        return {
            statusLabel,
            statusClass: hasErrors ? "text-orange-600" : "text-blue-600",
            errorCount: queueStatus.errorCount,
            recentErrors: queueStatus.recentErrors,
            showSpinner,
            progressDetails: details.length > 0 ? details : undefined,
        };
    }
    else if (isGenerating || inProgress) {
        return {
            statusLabel: "Generating Embeddings",
            statusClass: "text-blue-600",
            errorCount: undefined,
            recentErrors: undefined,
            showSpinner: true,
            progressDetails: undefined,
        };
    }
    else if (importStatus?.isImporting) {
        const hasErrors = (importStatus.errorCount ?? 0) > 0;
        return {
            statusLabel: importStatus.status || "Generating embeddings",
            statusClass: hasErrors ? "text-orange-600" : "text-purple-600",
            errorCount: importStatus.errorCount,
            recentErrors: importStatus.recentErrors,
            showSpinner: true,
            progressDetails: undefined,
        };
    }
    else if (centroidsStatus?.isPopulating) {
        return {
            statusLabel: centroidsStatus.status || "Populating centroids",
            statusClass: "text-pink-600",
            errorCount: undefined,
            recentErrors: undefined,
            showSpinner: true,
            progressDetails: undefined,
        };
    }
    else if (indexStatus?.rebuilding) {
        return {
            statusLabel: "Rebuilding",
            statusClass: "text-orange-600",
            errorCount: undefined,
            recentErrors: undefined,
            showSpinner: true,
            progressDetails: undefined,
        };
    }
    return null;
}
//# sourceMappingURL=utils.js.map