// (C) 2021-2026 GoodData Corporation
import { call, select } from "redux-saga/effects";
import { bucketsAttributes, insightBuckets, insightFilters, insightMeasures, } from "@gooddata/sdk-model";
import { invalidArgumentsProvided } from "../../../events/general.js";
import { selectAccessibleDashboardsMap } from "../../../store/accessibleDashboards/accessibleDashboardsSelectors.js";
import { selectDrillTargetsByWidgetRef } from "../../../store/drillTargets/drillTargetsSelectors.js";
import { selectInaccessibleDashboardsMap } from "../../../store/inaccessibleDashboards/inaccessibleDashboardsSelectors.js";
import { selectInsightByWidgetRef } from "../../../store/insights/insightsSelectors.js";
import { selectDashboardRef } from "../../../store/meta/metaSelectors.js";
import { selectFilterContextFilters } from "../../../store/tabs/filterContext/filterContextSelectors.js";
import { selectTabs } from "../../../store/tabs/tabsSelectors.js";
import { resolveDisplayFormMetadata, } from "../../../utils/displayFormResolver.js";
import { resolveInsights } from "../../../utils/insightResolver.js";
import { extractDisplayFormIdentifiers, extractInsightRefs, validateDrillDefinitionOrigin, validateInsightDrillDefinition, } from "./insightDrillDefinitionUtils.js";
export function validateDrillDefinition(drillDefinition, validationData, ctx, cmd) {
    let item = drillDefinition;
    // validate drill targets
    if (!validationData.drillTargets?.availableDrillTargets) {
        throw invalidArgumentsProvided(ctx, cmd, `Drill targets not set`);
    }
    // validate drills origin
    try {
        item = validateDrillDefinitionOrigin(item, validationData.drillTargets.availableDrillTargets);
    }
    catch (ex) {
        const messageDetail = ex.message;
        throw invalidArgumentsProvided(ctx, cmd, `Invalid drill origin for InsightDrillDefinition. Error: ${messageDetail}`);
    }
    // validate drill
    const validationContext = {
        widgetInsightAttributes: validationData.widgetInsightAttributes,
        sourceInsightFilters: validationData.sourceInsightFilters,
        sourceInsightMeasures: validationData.sourceInsightMeasures,
        dashboardFilters: validationData.dashboardFilters,
        dashboardsMap: validationData.accessibleDashboardMap,
        insightsMap: validationData.resolvedInsights.resolved,
        displayFormsMap: validationData.resolvedDisplayForms.resolved,
        availableDrillTargets: validationData.drillTargets.availableDrillTargets,
        inaccessibleDashboardsMap: validationData.inaccessibleDashboardsMap,
        currentDashboardRef: validationData.currentDashboardRef,
        currentDashboardTabs: validationData.currentDashboardTabs,
    };
    try {
        item = validateInsightDrillDefinition(item, validationContext);
    }
    catch (ex) {
        const messageDetail = ex.message;
        throw invalidArgumentsProvided(ctx, cmd, `Invalid InsightDrillDefinition. Error: ${messageDetail}`);
    }
    return item;
}
export function* getValidationData(widgetRef, drillsToModify, ctx) {
    const widgetInsight = yield select(selectInsightByWidgetRef(widgetRef));
    const widgetInsightAttributes = widgetInsight ? bucketsAttributes(insightBuckets(widgetInsight)) : [];
    const sourceInsightFilters = widgetInsight ? insightFilters(widgetInsight) : [];
    const sourceInsightMeasures = widgetInsight ? insightMeasures(widgetInsight) : [];
    const dashboardFilters = yield select(selectFilterContextFilters);
    const selectDrillTargetsByWidgetRefSelector = selectDrillTargetsByWidgetRef(widgetRef);
    const drillTargets = yield select(selectDrillTargetsByWidgetRefSelector);
    const accessibleDashboardMap = yield select(selectAccessibleDashboardsMap);
    const inaccessibleDashboardsMap = yield select(selectInaccessibleDashboardsMap);
    const currentDashboardRef = yield select(selectDashboardRef);
    const tabs = yield select(selectTabs);
    // Convert TabState to minimal IDashboardTab structure needed for validation
    const currentDashboardTabs = tabs?.map((tab) => ({
        localIdentifier: tab.localIdentifier,
        title: tab.title ?? "",
    }));
    const insightRefs = extractInsightRefs(drillsToModify);
    const resolvedInsights = yield call(resolveInsights, ctx, insightRefs);
    const displayFormIds = extractDisplayFormIdentifiers(drillsToModify);
    const resolvedDisplayForms = yield call(resolveDisplayFormMetadata, ctx, displayFormIds);
    return {
        widgetInsightAttributes,
        sourceInsightFilters,
        sourceInsightMeasures,
        dashboardFilters,
        drillTargets,
        accessibleDashboardMap,
        resolvedInsights,
        resolvedDisplayForms,
        inaccessibleDashboardsMap,
        currentDashboardRef,
        currentDashboardTabs,
    };
}
