// (C) 2020-2026 GoodData Corporation
import { useCallback, useState } from "react";
import { isEqual } from "lodash-es";
import { DataViewFacade, isSomeHeaderPredicateMatched, } from "@gooddata/sdk-ui";
import { useDashboardSelector } from "../../../../../model/react/DashboardStoreProvider.js";
import { selectEnableDrillToUrlByDefault } from "../../../../../model/store/config/configSelectors.js";
import { selectDrillableItemsByAvailableDrillTargets, selectImplicitDrillsByAvailableDrillTargets, } from "../../../../../model/store/widgetDrills/widgetDrillSelectors.js";
/**
 * @internal
 */
export const useDrillDialogInsightDrills = ({ widget, insight, onDrill: onDrillFn, }) => {
    // Drilling
    const [drillTargets, setDrillTargets] = useState();
    const disableDrillDownOnInsight = insight.insight.properties["controls"]?.["disableDrillDown"];
    const enableDrillToUrlByDefault = useDashboardSelector(selectEnableDrillToUrlByDefault);
    const disableDrillIntoURL = insight.insight.properties["controls"]?.disableDrillIntoURL ?? !enableDrillToUrlByDefault;
    const onPushData = useCallback((data) => {
        const targets = sanitizeAvailableDrillTargets(data?.availableDrillTargets, !disableDrillDownOnInsight, !disableDrillIntoURL);
        if (targets && !isEqual(drillTargets, data.availableDrillTargets)) {
            setDrillTargets(targets);
        }
    }, [disableDrillDownOnInsight, disableDrillIntoURL, drillTargets]);
    const implicitDrillDefinitions = useDashboardSelector(selectImplicitDrillsByAvailableDrillTargets(drillTargets, widget.ignoredDrillDownHierarchies, disableDrillIntoURL));
    const drillableItems = useDashboardSelector(selectDrillableItemsByAvailableDrillTargets(drillTargets, widget.ignoredDrillDownHierarchies, disableDrillIntoURL));
    const onDrill = onDrillFn
        ? (event) => {
            const facade = DataViewFacade.for(event.dataView);
            const matchingImplicitDrillDefinitions = implicitDrillDefinitions.filter((info) => {
                return event.drillContext.intersection?.some((intersection) => isSomeHeaderPredicateMatched(info.predicates, intersection.header, facade));
            });
            const drillEvent = {
                ...event,
                widgetRef: widget.ref,
                drillDefinitions: matchingImplicitDrillDefinitions.map((info) => info.drillDefinition),
            };
            return (typeof onDrillFn === "function" &&
                onDrillFn(drillEvent, {
                    insight,
                    widget,
                }));
        }
        : undefined;
    return {
        drillableItems,
        onPushData,
        onDrill,
    };
};
const sanitizeAvailableDrillTargets = (availableDrillTargets, isDrillDownOnInsightEnabled, isDrillToUrlOnInsightEnabled) => {
    // if no drill targets went in (likely the pushData was fired in a non-drill-related case)
    // pass the undefined through, this avoids useless setting of the drill targets down the line
    if (!availableDrillTargets) {
        return availableDrillTargets;
    }
    // Keep attribute drill targets when either drill down or drill to URL is enabled,
    // as both features depend on them.
    const keepAttributes = isDrillDownOnInsightEnabled || isDrillToUrlOnInsightEnabled;
    return keepAttributes ? availableDrillTargets : { ...availableDrillTargets, attributes: undefined };
};
//# sourceMappingURL=useDrillDialogInsightDrills.js.map