// (C) 2021-2026 GoodData Corporation
import { areObjRefsEqual } from "@gooddata/sdk-model";
import { changeFilterContextSelectionByParams } from "../commands/filters.js";
import { changeParameterValues } from "../commands/parameters.js";
import { newDashboardEventPredicate } from "../events/index.js";
/**
 * Event handler with the default implementation for drill to the same dashboard.
 *
 * When {@link IDashboardDrillToDashboardResolved} event is fired and contains dashboard ref that matches the provided dashboard ref,
 * or dashboard ref in the event is missing, it sets relevant drill intersection filters to the current dashboard.
 *
 * Note that only filters that are already stored in the dashboard filter context will be applied
 * (attribute filters that are not visible in the filter bar will not be applied).
 *
 * @alpha
 * @param dashboardRef - reference to the current dashboard
 * @returns event handler
 */
export const newDrillToSameDashboardHandler = (dashboardRef) => ({
    eval: newDashboardEventPredicate("GDC.DASH/EVT.DRILL.DRILL_TO_DASHBOARD.RESOLVED", (e) => !e.payload.drillDefinition.target ||
        areObjRefsEqual(e.payload.drillDefinition.target, dashboardRef)),
    handler: (event, dispatch) => {
        dispatch(changeFilterContextSelectionByParams({
            filters: event.payload.filters,
            attributeFilterConfigs: event.payload.attributeFilterConfigs,
        }));
        dispatch(changeParameterValues({ parameters: event.payload.parameters }));
    },
});
