'use client';

import type { ReactNode } from 'react';
import { ListagemListaEmpty, ListagemListaError } from '@/components/listings';

type FeiraListContentSectionProps = {
    isError: boolean;
    errorMessage: string;
    errorDescription: string;
    onRetry: () => void;
    errorRetryTestId: string;
    /** Erro Axios — CTA `next_action.href` em 403 (UX-FL-G001). */
    error?: unknown;
    topSlot?: ReactNode;
    showEmptyState: boolean;
    emptyMessage: string;
    emptyDescription: string;
    emptyCtaLabel?: string;
    onEmptyCta?: () => void;
    emptyTestId: string;
    emptyType?: 'info' | 'warning';
    /**
     * Substitui o empty padrão (`ListagemListaEmpty`).
     * Usar `ListagemEmptyState` quando a listagem seguir o padrão de `/juridico/documentos`.
     */
    emptyContent?: ReactNode;
    showTableBlock: boolean;
    tableSection: ReactNode;
    inlineOverlays?: ReactNode;
};

/** Bloco principal da listagem (alertas, erro, vazio, tabela) — partilhado pela feira. */
export function FeiraListContentSection({
    isError,
    errorMessage,
    errorDescription,
    onRetry,
    errorRetryTestId,
    error,
    topSlot,
    showEmptyState,
    emptyMessage,
    emptyDescription,
    emptyCtaLabel,
    onEmptyCta,
    emptyTestId,
    emptyType,
    emptyContent,
    showTableBlock,
    tableSection,
    inlineOverlays,
}: FeiraListContentSectionProps) {
    return (
        <>
            {topSlot}

            {isError ? (
                <ListagemListaError
                    message={errorMessage}
                    description={errorDescription}
                    onRetry={onRetry}
                    data-testid={errorRetryTestId}
                    error={error}
                />
            ) : null}

            {!isError ? (
                <>
                    {showEmptyState
                        ? (emptyContent ?? (
                              <ListagemListaEmpty
                                  message={emptyMessage}
                                  description={emptyDescription}
                                  ctaLabel={emptyCtaLabel}
                                  onCta={onEmptyCta}
                                  data-testid={emptyTestId}
                                  type={emptyType}
                              />
                          ))
                        : null}
                    {showTableBlock ? tableSection : null}
                    {inlineOverlays}
                </>
            ) : null}
        </>
    );
}
