import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2025-2026 GoodData Corporation
import { useId, useMemo } from "react";
import cx from "classnames";
import { Dialog, OverlayController, OverlayControllerProvider } from "@gooddata/sdk-ui-kit";
import { KdaContent } from "../components/KdaContent.js";
import { KdaErrorComponent, KdaErrorType } from "../components/KdaErrorComponent.js";
import { KdaFooter } from "../components/KdaFooter.js";
import { KdaHeader } from "../components/KdaHeader.js";
import { FiltersBar } from "../composition/FiltersBar.js";
import { KeyDriversFooter } from "../composition/KeyDriversFooter.js";
import { KeyDriversOverview } from "../composition/KeyDriversOverview.js";
import { KeyDriversPanel } from "../composition/KeyDriversPanel.js";
import { MetricsBar } from "../composition/MetricsBar.js";
import { KDA_DIALOG_OVERS_Z_INDEX } from "../const.js";
import { useKdaState } from "../providers/KdaState.js";
import { useChangeAnalysis } from "./hooks/useChangeAnalysis.js";
import { useKdaDialogAccessibility } from "./hooks/useKdaDialogAccessibility.js";
import { useKdaDialogTooltipsOverride } from "./hooks/useKdaDialogTooltipsOverride.js";
import { useValidAttributes } from "./hooks/useValidAttributes.js";
import { KdaDialogControls } from "./KdaDialogControls.js";
import { KdaDialogFloatingStatusBar, getFloatingStatus } from "./KdaDialogFloatingStatusBar.js";
import { KdaDialogSections } from "./KdaDialogSections.js";
const overlayController = OverlayController.getInstance(KDA_DIALOG_OVERS_Z_INDEX);
const KDA_DIALOG_MINIMIZED_DATA_TEST_ID = "kda-dialog-minimized";
const KDA_DIALOG_EXPANDED_DATA_TEST_ID = "kda-dialog-expanded";
/**
 * @internal
 */
export function KdaDialog({ className, showCloseButton = true, parentOverlayController, onClose, titleElementId, }) {
    const { state } = useKdaState();
    const { isMinimized } = state;
    const effectiveOverlayController = useMemo(() => parentOverlayController ?? overlayController, [parentOverlayController]);
    const metric = state.definition?.metric.measure;
    const title = metric?.alias ?? metric?.title ?? "";
    const detailsId = useId();
    const minimizedAlignPoints = useMemo(() => (isMinimized ? [{ align: "tc tc", offset: { x: 0, y: 30 } }] : undefined), [isMinimized]);
    const accessibilityConfig = useKdaDialogAccessibility(title, isMinimized, titleElementId);
    const dialogBaseClassName = cx(accessibilityConfig.dialogId, className);
    const displayCloseButton = showCloseButton ? !isMinimized : undefined;
    const status = getFloatingStatus(state.relevantStatus, state.itemsStatus, state.selectedStatus);
    useChangeAnalysis();
    useValidAttributes();
    useKdaDialogTooltipsOverride();
    return (_jsx(OverlayControllerProvider, { overlayController: effectiveOverlayController, children: isMinimized ? (_jsx(Dialog, { autofocusOnOpen: true, initialFocus: accessibilityConfig.descriptionElementId, className: cx(dialogBaseClassName, "gd-kda-dialog--minimized", {
                [`gd-kda-dialog--minimized--${status}`]: true,
            }), closeOnEscape: false, isModal: false, alignPoints: minimizedAlignPoints, accessibilityConfig: {
                ...accessibilityConfig,
                title: undefined,
            }, displayCloseButton: displayCloseButton, onClose: onClose, CloseButton: KdaDialogControls, dataTestId: KDA_DIALOG_MINIMIZED_DATA_TEST_ID, children: _jsx(KdaDialogFloatingStatusBar, { titleElementId: accessibilityConfig.descriptionElementId, status: status, onClose: onClose }) })) : (_jsx(Dialog, { className: cx(dialogBaseClassName, "gd-kda-dialog--expanded"), closeOnEscape: !state.attributesDropdownOpen &&
                !state.addFilterDropdownOpen &&
                !state.trendDropdownOpen, isModal: true, accessibilityConfig: {
                ...accessibilityConfig,
                title: undefined,
            }, displayCloseButton: displayCloseButton, onClose: onClose, CloseButton: KdaDialogControls, dataTestId: KDA_DIALOG_EXPANDED_DATA_TEST_ID, children: _jsx(KdaDialogSections, { header: _jsx(KdaHeader, { titleId: accessibilityConfig.descriptionElementId, title: accessibilityConfig.title, bars: _jsxs(_Fragment, { children: [
                            _jsx(MetricsBar, {}), _jsx(FiltersBar, {})
                        ] }) }), content: _jsx(KdaContent, { contentError: (err) => (_jsx(KdaErrorComponent, { type: KdaErrorType.Items, error: err })), leftContent: _jsx(KeyDriversPanel, { detailsId: detailsId }), leftLoader: _jsx(KeyDriversPanel, { detailsId: detailsId, loading: true }), rightContent: _jsx(KeyDriversOverview, { detailsId: detailsId }), rightLoader: _jsx(KeyDriversOverview, { loading: true }), rightError: (err) => (_jsx(KdaErrorComponent, { type: KdaErrorType.Details, error: err })) }), footer: _jsx(KdaFooter, { content: _jsx(KeyDriversFooter, {}), onClose: onClose }) }) })) }));
}
