import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2020-2026 GoodData Corporation
import { useCallback, useMemo, useState } from "react";
import { isIdentifierRef } from "@gooddata/sdk-model";
import { createFocusHighchartsDatapointEvent } from "@gooddata/sdk-ui";
import { KdaDialogController } from "../../../../kdaDialog/dialog/KdaDialogController.js";
import { useDashboardSelector } from "../../../../model/react/DashboardStoreProvider.js";
import { selectCatalogAttributeDisplayFormsById } from "../../../../model/store/catalog/catalogSelectors.js";
import { selectEnableDrilledTooltip, selectLocale, selectObjectAvailabilityConfig, selectSeparators, } from "../../../../model/store/config/configSelectors.js";
import { isDrillDownDefinition } from "../../../../types.js";
import { getDrillDownTitle } from "../../../drill/utils/drillDownUtils.js";
import { DashboardInsightWithDrillSelect } from "./Insight/DashboardInsightWithDrillSelect.js";
import { InsightDrillDialog } from "./InsightDrillDialog/InsightDrillDialog.js";
/**
 * @internal
 */
export function DashboardInsightWithDrillDialog(props) {
    const [drillSteps, setDrillSteps] = useState([]);
    const activeDrillStep = drillSteps.at(-1);
    const insight = activeDrillStep?.insight;
    const widget = props.widget;
    const separators = useDashboardSelector(selectSeparators);
    const attributeDisplayForms = useDashboardSelector(selectCatalogAttributeDisplayFormsById);
    const enableDrillDescription = useDashboardSelector(selectEnableDrilledTooltip);
    const breadcrumbs = useMemo(() => drillSteps
        .filter((s) => isDrillDownDefinition(s.drillDefinition))
        .map((s) => getDrillDownTitle(s.drillDefinition, s.drillEvent, widget.drillDownIntersectionIgnoredAttributes, attributeDisplayForms[isIdentifierRef(s.drillDefinition.target)
        ? s.drillDefinition.target.identifier
        : s.drillDefinition.target.uri]) ?? "NULL"), [drillSteps, attributeDisplayForms, widget.drillDownIntersectionIgnoredAttributes]);
    const locale = useDashboardSelector(selectLocale);
    const objectAvailability = useDashboardSelector(selectObjectAvailabilityConfig);
    const setNextDrillStep = useCallback((drillStep) => {
        setDrillSteps((s) => [...s, drillStep]);
    }, []);
    const [returnFocusInfo, setReturnFocusInfo] = useState(null);
    const returnFocusToInsight = useCallback((force) => {
        const isNavigatingByKeyboard = force || document.querySelector(":focus-visible") !== null;
        if (!returnFocusInfo || !isNavigatingByKeyboard) {
            return;
        }
        if (returnFocusInfo.type === "chart") {
            window.dispatchEvent(createFocusHighchartsDatapointEvent(returnFocusInfo));
        }
        if (returnFocusInfo.type === "table") {
            returnFocusInfo.element?.focus();
        }
        setReturnFocusInfo(null);
    }, [returnFocusInfo]);
    const goBack = useCallback(() => setDrillSteps(([firstDrill]) => [firstDrill]), []);
    const onClose = useCallback(() => {
        returnFocusToInsight();
        setDrillSteps([]);
    }, [returnFocusToInsight]);
    const storeDatapointInfo = useCallback((drillContext) => {
        // TABLE
        if (drillContext.type === "table") {
            setReturnFocusInfo({ type: "table", element: document.activeElement });
            return;
        }
        // CHART
        const { chartId, seriesIndex, pointIndex } = drillContext;
        if (chartId === undefined || seriesIndex === undefined || pointIndex === undefined) {
            return;
        }
        setReturnFocusInfo({ type: "chart", chartId, seriesIndex, pointIndex });
    }, []);
    const onDrillDown = useCallback((evt) => setNextDrillStep(evt.payload), [setNextDrillStep]);
    const onDrillToInsight = useCallback((evt) => setNextDrillStep(evt.payload), [setNextDrillStep]);
    const onDrillStart = useCallback((evt) => storeDatapointInfo(evt.payload.drillEvent.drillContext), [storeDatapointInfo]);
    const [keyDriveInfo, setKeyDriveInfo] = useState(undefined);
    const onKeyDriverAnalysisSuccess = useCallback((evt) => {
        setKeyDriveInfo(evt.payload);
    }, []);
    const onCloseKeyDriverAnalysis = useCallback(() => {
        returnFocusToInsight();
        setKeyDriveInfo(undefined);
    }, [returnFocusToInsight]);
    const onRequestedDefinitionChange = useCallback((definition) => {
        // Keep the drill-derived state in sync with the controller
        if (!definition) {
            setKeyDriveInfo(undefined);
            return;
        }
        setKeyDriveInfo((prev) => (prev ? { ...prev, keyDriveDefinition: definition } : prev));
    }, []);
    return (_jsxs(_Fragment, { children: [
            _jsx(DashboardInsightWithDrillSelect, { ...props, onDrillDown: onDrillDown, onDrillToInsight: onDrillToInsight, onKeyDriverAnalysisSuccess: onKeyDriverAnalysisSuccess, drillStep: activeDrillStep, returnFocusToInsight: returnFocusToInsight, onDrillStart: onDrillStart }), activeDrillStep ? (_jsx(InsightDrillDialog, { locale: locale, breadcrumbs: breadcrumbs, widget: widget, insight: insight, onDrillDown: onDrillDown, onBackButtonClick: goBack, onClose: onClose, enableDrillDescription: enableDrillDescription, drillStep: activeDrillStep, returnFocusToInsight: returnFocusToInsight, onDrillStart: onDrillStart })) : null, _jsx(KdaDialogController, { requestedDefinition: keyDriveInfo?.keyDriveDefinition, separators: separators, showCloseButton: true, includeTags: objectAvailability?.includeObjectsWithTags, excludeTags: objectAvailability?.excludeObjectsWithTags, onRequestedDefinitionChange: onRequestedDefinitionChange, onClose: onCloseKeyDriverAnalysis })
        ] }));
}
//# sourceMappingURL=DashboardInsightWithDrillDialog.js.map