// (C) 2024-2026 GoodData Corporation
import { call, select } from "redux-saga/effects";
import { invariant } from "ts-invariant";
import { defWithDimensions, defaultDimensionsGenerator, insightRef, newDefForInsight, } from "@gooddata/sdk-model";
import { fillMissingTitles, resolveDefaultDisplayFormRefForDisplayForm } from "@gooddata/sdk-ui";
import { filterContextItemsToDashboardFiltersByWidget } from "../../../converters/filterConverters.js";
import { insightWidgetExportResolved, } from "../../events/insight.js";
import { selectCatalogAttributes } from "../../store/catalog/catalogSelectors.js";
import { selectExportResultPollingTimeout, selectLocale, selectSettings, } from "../../store/config/configSelectors.js";
import { selectExecutionResultByRef } from "../../store/executionResults/executionResultsSelectors.js";
import { selectInsightByRef, selectRawExportOverridesForInsight, } from "../../store/insights/insightsSelectors.js";
import { selectFilterContextFilters, selectPreloadedAttributesWithReferences, } from "../../store/tabs/filterContext/filterContextSelectors.js";
import { prepareGeoRawExportDefinition } from "../common/prepareGeoRawExportDefinition.js";
async function exportDashboardToCSVRaw(ctx, definition, filename, overrides, options) {
    const { backend, workspace } = ctx;
    return backend
        .workspace(workspace)
        .dashboards()
        .exportDashboardToCSVRaw(definition, filename, overrides, options);
}
export function* exportRawInsightWidgetHandler(ctx, cmd) {
    const { ref, widget, insight, filename, options } = cmd.payload;
    const { workspace } = ctx;
    const executionEnvelope = yield select(selectExecutionResultByRef(ref));
    const filterContextFilters = yield select(selectFilterContextFilters);
    const dashboardFilters = filterContextItemsToDashboardFiltersByWidget(filterContextFilters, widget);
    const mergedFilters = [...insight.insight.filters, ...dashboardFilters];
    const definition = defWithDimensions(newDefForInsight(workspace, insight, mergedFilters), defaultDimensionsGenerator);
    const selectedInsight = yield select(selectInsightByRef(insightRef(insight)));
    const locale = yield select(selectLocale);
    const filledInsight = yield call(fillMissingTitles, selectedInsight, locale, undefined);
    const settings = yield select(selectSettings);
    const catalogAttributes = yield select(selectCatalogAttributes);
    const preloadedAttributesWithReferences = yield select(selectPreloadedAttributesWithReferences);
    const baseExecutionDefinition = executionEnvelope?.executionResult?.definition ?? definition;
    const { executionDefinition: preparedExecutionDefinition, filledInsight: preparedFilledInsight } = prepareGeoRawExportDefinition({
        baseExecutionDefinition,
        sourceInsight: insight,
        filledInsight,
        workspace,
        settings,
        resolveDefaultDisplayFormRef: (displayFormRef) => resolveDefaultDisplayFormRefForDisplayForm(displayFormRef, catalogAttributes, preloadedAttributesWithReferences),
    });
    const overrides = yield select(selectRawExportOverridesForInsight(preparedFilledInsight));
    // execution definition must be defined at this point
    invariant(preparedExecutionDefinition);
    const timeout = yield select(selectExportResultPollingTimeout);
    const effectiveTimeout = options?.timeout ?? timeout;
    const result = yield call(exportDashboardToCSVRaw, ctx, preparedExecutionDefinition, filename, overrides, { ...options, timeout: effectiveTimeout });
    // prepend hostname if provided so that the results are downloaded from there, not from where the app is hosted
    const fullUri = ctx.backend.config.hostname
        ? new URL(result.uri, ctx.backend.config.hostname).href
        : result.uri;
    const sanitizedResult = {
        ...result,
        uri: fullUri,
    };
    return insightWidgetExportResolved(ctx, sanitizedResult, cmd.correlationId);
}
//# sourceMappingURL=exportRawInsightWidgetHandler.js.map