// (C) 2021-2026 GoodData Corporation
import { compact, isEmpty } from "lodash-es";
import { all, call, select } from "redux-saga/effects";
import { invariant } from "ts-invariant";
import { generateDateFilterLocalIdentifier } from "@gooddata/sdk-backend-base";
import { areObjRefsEqual, filterLocalIdentifier, filterObjRef, idRef, insightFilters, isAttributeFilter, isDashboardAttributeFilterReference, isDashboardDateFilterReference, isDashboardMeasureValueFilterReference, isDateFilter, isInsightWidget, isMeasureValueFilter, isNoopAllTimeDateFilter, isRankingFilter, newAllTimeFilter, objRefToString, uriRef, } from "@gooddata/sdk-model";
import { invalidQueryArguments } from "../events/general.js";
import { createQueryService } from "../store/_infra/queryService.js";
import { selectSupportsMultipleDateFilters } from "../store/backendCapabilities/backendCapabilitiesSelectors.js";
import { selectEnableDateFilterIdentifiers } from "../store/config/configSelectors.js";
import { selectInsightByRef } from "../store/insights/insightsSelectors.js";
import { selectAttributeFilterConfigsDisplayAsLabelMap } from "../store/tabs/attributeFilterConfigs/attributeFilterConfigsSelectors.js";
import { selectAllFiltersForWidgetByRef, selectFilterableWidgetByRef, } from "../store/tabs/layout/layoutSelectors.js";
import { resolveDisplayFormMetadata } from "../utils/displayFormResolver.js";
export const QueryWidgetFiltersService = createQueryService("GDC.DASH/QUERY.WIDGET.FILTERS", queryService);
function refMatchesMdObject(ref, mdObject, type) {
    return (areObjRefsEqual(ref, mdObject.ref) ||
        areObjRefsEqual(ref, idRef(mdObject.id, type)) ||
        areObjRefsEqual(ref, uriRef(mdObject.uri)));
}
function* loadDisplayFormsForAttributeFilters(ctx, filters) {
    const refs = filters.map(filterObjRef);
    const resolvedObjects = yield call(resolveDisplayFormMetadata, ctx, compact(refs));
    // if some display forms could not be resolved then there is something seriously amiss
    invariant(isEmpty(resolvedObjects.missing));
    return filters.map((filter) => {
        return {
            filter,
            displayForm: resolvedObjects.resolved.get(filterObjRef(filter)),
        };
    });
}
function selectDateDatasetsForDateFilters(filters) {
    return filters.map((filter) => {
        return {
            dateDatasetLink: filterObjRef(filter),
            filter,
        };
    });
}
function* getResolvedInsightAttributeFilters(ctx, widget, dashboardAttributeFilters, insightAttributeFilters) {
    // only dashboard filters are subject to widget ignores
    const resolvedDashboardFilters = yield call(getResolvedAttributeFilters, ctx, widget, dashboardAttributeFilters);
    return [...resolvedDashboardFilters, ...insightAttributeFilters];
}
function* getResolvedAttributeFilters(ctx, widget, attributeFilters) {
    const attributeFilterDisplayFormPairs = yield call(loadDisplayFormsForAttributeFilters, ctx, attributeFilters);
    const displayAsLabelMap = yield select(selectAttributeFilterConfigsDisplayAsLabelMap);
    const attributeFilterDisplayFormPairsWithIgnoreResolved = resolveWidgetFilterIgnore(widget, attributeFilterDisplayFormPairs, displayAsLabelMap);
    return attributeFilterDisplayFormPairsWithIgnoreResolved.map((item) => item.filter);
}
function resolveWidgetFilterIgnore(widget, dashboardNonDateFilterDisplayFormPairs, displayAsLabelMap) {
    return dashboardNonDateFilterDisplayFormPairs.filter(({ filter, displayForm }) => {
        const matches = displayForm &&
            widget.ignoreDashboardFilters?.filter(isDashboardAttributeFilterReference).some((ignored) => {
                const filterLocalId = filterLocalIdentifier(filter);
                const displayAsLabel = filterLocalId ? displayAsLabelMap.get(filterLocalId) : undefined;
                return (refMatchesMdObject(ignored.displayForm, displayForm, "displayForm") ||
                    areObjRefsEqual(ignored.displayForm, displayAsLabel));
            });
        return !matches;
    });
}
function selectResolvedInsightDateFilters(widget, dashboardCommonDateFilters, dashboardDateFiltersWithDimensions, insightDateFilters, supportsMultipleDateFilters) {
    const nonIgnoredDashboardDateFilterDateDatasetPairs = selectResolveWidgetDateFilterIgnore(widget, dashboardCommonDateFilters, dashboardDateFiltersWithDimensions);
    const insightDateFilterDateDatasetPairs = selectDateDatasetsForDateFilters(insightDateFilters);
    return resolveDateFilters(insightDateFilterDateDatasetPairs, nonIgnoredDashboardDateFilterDateDatasetPairs, supportsMultipleDateFilters);
}
function selectResolveWidgetDateFilterIgnore(widget, dashboardCommonDateFilters, dashboardDateFiltersWithDimensions) {
    const commonDateFilterDateDatasetPairs = selectDateDatasetsForDateFilters(dashboardCommonDateFilters);
    const widgetDateFilterDateDatasetPairs = selectDateDatasetsForDateFilters(dashboardDateFiltersWithDimensions);
    return resolveWidgetDateFilterIgnore(widget, commonDateFilterDateDatasetPairs, widgetDateFilterDateDatasetPairs);
}
function resolveWidgetDateFilterIgnore(widget, commonDateFilterDateDatasetPairs, widgetDateFilterDateDatasetPairs) {
    const nonIgnoredCommonDateFilterDateDatasetPairs = commonDateFilterDateDatasetPairs.filter(({ dateDatasetLink }) => {
        return (!!widget.dateDataSet &&
            dateDatasetLink &&
            areObjRefsEqual(widget.dateDataSet, dateDatasetLink));
    });
    const nonIgnoredWidgetDateFilterDateDatasetPairs = widgetDateFilterDateDatasetPairs.filter(({ dateDatasetLink }) => {
        const matches = widget.ignoreDashboardFilters
            ?.filter(isDashboardDateFilterReference)
            .some((ignored) => dateDatasetLink && areObjRefsEqual(ignored.dataSet, dateDatasetLink));
        return !matches;
    });
    return [...nonIgnoredCommonDateFilterDateDatasetPairs, ...nonIgnoredWidgetDateFilterDateDatasetPairs];
}
function selectResolvedDateFilters(widget, dashboardCommonDateFilters, dashboardDateFiltersWithDimensions, supportsMultipleDateFilters) {
    const allDateFilterDateDatasetPairs = selectResolveWidgetDateFilterIgnore(widget, dashboardCommonDateFilters, dashboardDateFiltersWithDimensions);
    return resolveDateFilters([], allDateFilterDateDatasetPairs, supportsMultipleDateFilters);
}
function resolveDashboardMeasureValueFilters(widget, dashboardMeasureValueFilters) {
    const ignored = widget.ignoreDashboardFilters?.filter(isDashboardMeasureValueFilterReference) ?? [];
    if (ignored.length === 0) {
        return dashboardMeasureValueFilters;
    }
    return dashboardMeasureValueFilters.filter((filter) => {
        const measureRef = filter.measureValueFilter.measure;
        return !ignored.some((ref) => areObjRefsEqual(ref.measure, measureRef));
    });
}
function resolveDateFilters(insightDateFilterDateDatasetPairs, dashboardDateFilterDateDatasetPairs, supportsMultipleDateFilters) {
    // prioritize dashboard filters over insight ones
    // and strip useless all time filters at the end
    const init = dashboardDateFilterDateDatasetPairs
        .filter((item) => !!item.dateDatasetLink)
        .map((item) => item.filter);
    return insightDateFilterDateDatasetPairs
        .filter((item) => !!item.dateDatasetLink)
        .reduceRight((acc, curr) => {
        const alreadyPresent = acc.some((item) => areObjRefsEqual(filterObjRef(item), curr.dateDatasetLink));
        if (!alreadyPresent) {
            acc.push(curr.filter);
        }
        return acc;
    }, init)
        .filter((item) => {
        if (supportsMultipleDateFilters) {
            return true;
        }
        else {
            return !isNoopAllTimeDateFilter(item);
        }
    });
}
export function* queryWithInsight(ctx, widget, insight) {
    const widgetAwareDashboardFiltersSelector = selectAllFiltersForWidgetByRef(widget.ref);
    const [widgetAwareDashboardCommonDateFilters, widgetAwareDashboardOtherFilters] = yield select(widgetAwareDashboardFiltersSelector);
    const supportsMultipleDateFilters = yield select(selectSupportsMultipleDateFilters);
    const enableDateFilterIdentifiers = yield select(selectEnableDateFilterIdentifiers);
    // add all time filter explicitly in case the date widgetAwareDashboardFilters are empty
    // this will cause the all time filter to be used instead of the insight date filter
    // if the dashboard date filter is not ignored by the widget
    if (!widgetAwareDashboardCommonDateFilters.length && widget.dateDataSet) {
        widgetAwareDashboardCommonDateFilters.push(newAllTimeFilter(widget.dateDataSet, enableDateFilterIdentifiers
            ? generateDateFilterLocalIdentifier(0, widget.dateDataSet)
            : undefined));
    }
    const effectiveInsightFilters = insightFilters(insight);
    const [dateFilters, attributeFilters] = yield all([
        call(selectResolvedInsightDateFilters, widget, widgetAwareDashboardCommonDateFilters.filter(isDateFilter), widgetAwareDashboardOtherFilters.filter(isDateFilter), effectiveInsightFilters.filter(isDateFilter), supportsMultipleDateFilters),
        call(getResolvedInsightAttributeFilters, ctx, widget, widgetAwareDashboardOtherFilters.filter(isAttributeFilter), effectiveInsightFilters.filter(isAttributeFilter)),
    ]);
    const dashboardMeasureValueFilters = resolveDashboardMeasureValueFilters(widget, widgetAwareDashboardOtherFilters.filter(isMeasureValueFilter));
    return [
        ...dateFilters,
        ...attributeFilters,
        /**
         * Strictly speaking, there should be a resolution here that makes sure there is at most one MVF per measure.
         * This, however, is not worth the hassle: AD will not allow creating such insight, so the only way this might
         * happen is if widgetFilterOverrides have this clash (or someone created an insight manually using API directly).
         *
         * We choose to not do it here as doing it would need extension of the SPI with some getMeasures method
         * (because the catalog API cannot be used here as we do not know which dataset the given measure might come from)
         * and we do not want that extension at the moment (catalog API should still be good enough for most use cases).
         *
         * Dashboard-level MVFs and insight-level MVFs are sent to execution together (logical AND). Incompatible
         * dashboard MVFs (referencing metrics not in the widget) are silently ignored by the backend, so no
         * compatibility check is performed at execution time.
         */
        ...dashboardMeasureValueFilters,
        ...effectiveInsightFilters.filter(isMeasureValueFilter),
        // nothing to resolve for ranking filters
        ...effectiveInsightFilters.filter(isRankingFilter),
    ];
}
function* queryWithoutInsight(ctx, widget) {
    const widgetAwareDashboardFiltersSelector = selectAllFiltersForWidgetByRef(widget.ref);
    const [widgetAwareDashboardCommonDateFilters, widgetAwareDashboardOtherFilters] = yield select(widgetAwareDashboardFiltersSelector);
    const supportsMultipleDateFilters = yield select(selectSupportsMultipleDateFilters);
    const [dateFilters, attributeFilters] = yield all([
        call(selectResolvedDateFilters, widget, widgetAwareDashboardCommonDateFilters.filter(isDateFilter), widgetAwareDashboardOtherFilters.filter(isDateFilter), supportsMultipleDateFilters),
        call(getResolvedAttributeFilters, ctx, widget, widgetAwareDashboardOtherFilters.filter(isAttributeFilter)),
    ]);
    const dashboardMeasureValueFilters = resolveDashboardMeasureValueFilters(widget, widgetAwareDashboardOtherFilters.filter(isMeasureValueFilter));
    return [...dateFilters, ...attributeFilters, ...dashboardMeasureValueFilters];
}
function* queryService(ctx, query) {
    const { payload: { widgetRef, insight }, correlationId, } = query;
    const widgetSelector = selectFilterableWidgetByRef(widgetRef);
    const widget = yield select(widgetSelector);
    if (!widget) {
        throw invalidQueryArguments(ctx, `Widget with ref ${objRefToString(widgetRef)} does not exist on the dashboard`, correlationId);
    }
    if (insight) {
        return yield call(queryWithInsight, ctx, widget, insight);
    }
    else {
        if (isInsightWidget(widget)) {
            const insightRef = widget.insight;
            const insightSelector = selectInsightByRef(insightRef);
            const linkedInsight = yield select(insightSelector);
            if (!linkedInsight) {
                throw invalidQueryArguments(ctx, `Insight with ref ${objRefToString(insightRef)} does not exist on the dashboard`, correlationId);
            }
            return yield call(queryWithInsight, ctx, widget, linkedInsight);
        }
        else {
            return yield call(queryWithoutInsight, ctx, widget);
        }
    }
}
//# sourceMappingURL=queryWidgetFilters.js.map