// (C) 2021-2026 GoodData Corporation
import { call, put, select } from "redux-saga/effects";
import { invariant } from "ts-invariant";
import { findDateFilterOptionByValue } from "../../../_staging/dateFilterConfig/dateFilterOptionMapping.js";
import { removeAttributeFilters } from "../../commands/filters.js";
import { filterContextChanged, filterContextWorkingSelectionApplied } from "../../events/filters.js";
import { dispatchDashboardEvent } from "../../store/_infra/eventDispatcher.js";
import { selectEnableImmediateAttributeFilterDisplayAsLabelMigration } from "../../store/config/configSelectors.js";
import { selectCrossFilteringFiltersLocalIdentifiers, selectIsCrossFiltering, } from "../../store/drill/drillSelectors.js";
import { drillActions } from "../../store/drill/index.js";
import { selectAttributeFilterConfigsOverrides, selectAttributeFilterConfigsOverridesByTab, } from "../../store/tabs/attributeFilterConfigs/attributeFilterConfigsSelectors.js";
import { selectEffectiveDateFilterOptions } from "../../store/tabs/dateFilterConfig/dateFilterConfigSelectors.js";
import { selectFilterContextDefinition, selectFilterContextDefinitionsByTab, } from "../../store/tabs/filterContext/filterContextSelectors.js";
import { tabsActions } from "../../store/tabs/index.js";
import { selectActiveOrDefaultTabLocalIdentifier } from "../../store/tabs/tabsSelectors.js";
export function* dispatchFilterContextChanged(ctx, cmd, tabLocalIdentifier) {
    let filterContext;
    let attributeFilterConfigs;
    if (tabLocalIdentifier) {
        // Read from specific tab
        const filterContextsByTab = yield select(selectFilterContextDefinitionsByTab);
        const attributeFilterConfigsByTab = yield select(selectAttributeFilterConfigsOverridesByTab);
        invariant(filterContextsByTab[tabLocalIdentifier], `Filter context not found for tab ${tabLocalIdentifier}`);
        filterContext = filterContextsByTab[tabLocalIdentifier];
        attributeFilterConfigs = attributeFilterConfigsByTab[tabLocalIdentifier] ?? [];
    }
    else {
        // Read from active tab (default behavior)
        filterContext = (yield select(selectFilterContextDefinition));
        attributeFilterConfigs = (yield select(selectAttributeFilterConfigsOverrides));
    }
    yield dispatchDashboardEvent(filterContextChanged(ctx, filterContext, attributeFilterConfigs, cmd.correlationId, tabLocalIdentifier));
}
export function* canApplyDateFilter(dateFilter) {
    const effectiveDateFilterOptions = yield select(selectEffectiveDateFilterOptions);
    const targetOption = findDateFilterOptionByValue(dateFilter, effectiveDateFilterOptions);
    if (!targetOption) {
        if (dateFilter.dateFilter.type === "absolute") {
            return !!effectiveDateFilterOptions?.absoluteForm?.visible;
        }
        else if (dateFilter.dateFilter.type === "relative") {
            if (dateFilter.dateFilter.granularity === "GDC.time.date" &&
                dateFilter.dateFilter.from === undefined &&
                dateFilter.dateFilter.to === undefined) {
                return !!effectiveDateFilterOptions?.allTime?.visible;
            }
            return !!effectiveDateFilterOptions?.relativeForm?.visible;
        }
    }
    return !!targetOption;
}
export function* resetCrossFiltering(cmd) {
    const virtualFilters = yield select(selectCrossFilteringFiltersLocalIdentifiers);
    yield put(removeAttributeFilters(virtualFilters, cmd.correlationId));
    const activeTabId = yield select(selectActiveOrDefaultTabLocalIdentifier);
    yield put(drillActions.resetCrossFiltering(activeTabId));
}
export function* applyWorkingSelectionHandler(ctx, cmd) {
    const enableImmediateAttributeFilterDisplayAsLabelMigration = yield select(selectEnableImmediateAttributeFilterDisplayAsLabelMigration);
    yield put(tabsActions.applyWorkingSelection({ enableImmediateAttributeFilterDisplayAsLabelMigration }));
    const isCrossFiltering = yield select(selectIsCrossFiltering);
    if (isCrossFiltering) {
        yield call(resetCrossFiltering, cmd);
    }
    yield dispatchDashboardEvent(filterContextWorkingSelectionApplied(ctx, cmd.correlationId));
    yield call(dispatchFilterContextChanged, ctx, cmd);
}
export function* resetWorkingSelectionHandler() {
    yield put(tabsActions.resetWorkingSelection());
}
