// (C) 2024-2026 GoodData Corporation
import { call, select } from "redux-saga/effects";
import { ensureAllTimeFilterForExport } from "../../../_staging/exportUtils/filterUtils.js";
import { invalidArgumentsProvided } from "../../events/general.js";
import { insightWidgetExportResolved, } from "../../events/insight.js";
import { selectExportResultPollingTimeout } from "../../store/config/configSelectors.js";
import { selectDashboardRef } from "../../store/meta/metaSelectors.js";
import { selectFilterContextFilters } from "../../store/tabs/filterContext/filterContextSelectors.js";
import { selectActiveTabLocalIdentifier } from "../../store/tabs/tabsSelectors.js";
export function* exportImageInsightWidgetHandler(ctx, cmd) {
    const { ref, filename } = cmd.payload;
    const { workspace, backend } = ctx;
    const dashboardRef = yield select(selectDashboardRef);
    if (!dashboardRef) {
        throw invalidArgumentsProvided(ctx, cmd, "Dashboard to export to image must have an ObjRef.");
    }
    const filterContextFilters = yield select(selectFilterContextFilters);
    const effectiveFilters = ensureAllTimeFilterForExport(filterContextFilters);
    const activeTabLocalIdentifier = yield select(selectActiveTabLocalIdentifier);
    const timeout = yield select(selectExportResultPollingTimeout);
    // for single widget export active tabs filters are enough
    const effectiveFiltersByTab = activeTabLocalIdentifier
        ? {
            [activeTabLocalIdentifier]: effectiveFilters,
        }
        : undefined;
    const exportDashboardToImage = backend.workspace(workspace).dashboards().exportDashboardToImage;
    const result = yield call(exportDashboardToImage, dashboardRef, effectiveFilters, effectiveFiltersByTab, {
        widgetIds: [ref],
        filename,
        timeout,
    });
    // 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=exportImageInsightWidgetHandler.js.map