// (C) 2021-2026 GoodData Corporation
import { createSelector } from "@reduxjs/toolkit";
import { serializeObjRef } from "@gooddata/sdk-model";
import { isNonExportableError, isNonExportableErrorExceptTooLarge, } from "../../../_staging/errors/errorPredicates.js";
import { createMemoizedSelector } from "../_infra/selectors.js";
import { selectSupportsExportToCsv, selectSupportsExportToXlsx, } from "../backendCapabilities/backendCapabilitiesSelectors.js";
import { selectSettings } from "../config/configSelectors.js";
import { selectCanExecuteRaw, selectCanExportTabular } from "../permissions/permissionsSelectors.js";
import { selectAnalyticalWidgetByRef } from "../tabs/layout/layoutSelectors.js";
import { executionResultsAdapter } from "./executionResultsEntityAdapter.js";
const selectSelf = createSelector((state) => state, (state) => state.executionResults);
const adapterSelectors = executionResultsAdapter.getSelectors(selectSelf);
const selectExecutionResultEntities = adapterSelectors.selectEntities;
/**
 * @alpha
 */
export const selectExecutionResult = adapterSelectors.selectById;
/**
 * @alpha
 */
export const selectHasSomeExecutionResult = createSelector(selectExecutionResultEntities, (executionResults) => {
    // Empty results can be resolved as error, so consider error also as resolved
    return Object.values(executionResults).filter((e) => !!e?.executionResult || !!e?.error).length >= 1;
});
/**
 * @alpha
 */
export const selectExecutionResultByRef = createMemoizedSelector((ref) => createSelector(selectExecutionResultEntities, (executionResults) => {
    if (!ref) {
        return undefined;
    }
    const key = serializeObjRef(ref);
    return executionResults[key];
}));
/**
 * @alpha
 */
export const selectIsExecutionResultReadyForExportByRef = createMemoizedSelector((ref) => createSelector(selectExecutionResultByRef(ref), selectAnalyticalWidgetByRef(ref), (widgetExecution) => {
    if (!widgetExecution) {
        return false;
    }
    const { isLoading, error, executionResult } = widgetExecution;
    return !!executionResult && !isLoading && !isNonExportableError(error);
}));
/**
 * @alpha
 */
export const selectIsExecutionResultExportableToCsvByRef = createMemoizedSelector((ref) => createSelector(selectSupportsExportToCsv, selectIsExecutionResultReadyForExportByRef(ref), selectCanExportTabular, selectCanExecuteRaw, selectSettings, (supportsCapabilityCsv, isReadyForExport, canExportTabular, canExecuteRaw, settings) => {
    const isExportEnabled = Boolean(settings["enableKPIDashboardExport"] && canExportTabular);
    const isRawExportEnabled = Boolean(isExportEnabled && canExecuteRaw);
    return supportsCapabilityCsv && isReadyForExport && isRawExportEnabled;
}));
/**
 * @alpha
 */
export const selectIsExecutionResultExportableToXlsxByRef = createMemoizedSelector((ref) => createSelector(selectSupportsExportToXlsx, selectIsExecutionResultReadyForExportByRef(ref), selectCanExportTabular, selectSettings, (supportCapabilityXlsx, isReadyForExport, canExportTabular, settings) => {
    const isExportEnabled = Boolean(settings["enableKPIDashboardExport"] && canExportTabular);
    return supportCapabilityXlsx && isReadyForExport && isExportEnabled;
}));
/**
 * @alpha
 */
export const selectIsExecutionResultExportableToPdfByRef = createMemoizedSelector((ref) => createSelector(selectIsExecutionResultReadyForExportByRef(ref), selectCanExportTabular, selectSettings, (isReadyForExport, canExportTabular, settings) => {
    const isExportEnabled = Boolean(settings["enableKPIDashboardExport"] && canExportTabular);
    const isPdfExportEnabled = Boolean(settings.enableNewPdfTabularExport);
    return isReadyForExport && isExportEnabled && isPdfExportEnabled;
}));
/**
 * @alpha
 */
export const selectIsExecutionResultReadyForExportRawByRef = createMemoizedSelector((ref) => createSelector(selectExecutionResultByRef(ref), selectAnalyticalWidgetByRef(ref), (widgetExecution) => {
    if (!widgetExecution) {
        return false;
    }
    const { isLoading, error } = widgetExecution;
    return !isLoading && !isNonExportableErrorExceptTooLarge(error);
}));
/**
 * @alpha
 */
export const selectIsExecutionResultExportableToCsvRawByRef = createMemoizedSelector((ref) => createSelector(selectSupportsExportToCsv, selectIsExecutionResultReadyForExportRawByRef(ref), selectCanExportTabular, selectCanExecuteRaw, selectSettings, (supportsCapabilityCsv, isReadyForExport, canExportTabular, canExecuteRaw, settings) => {
    const isExportEnabled = Boolean(settings["enableKPIDashboardExport"] && canExportTabular);
    const isRawExportEnabled = Boolean(isExportEnabled && canExecuteRaw);
    return supportsCapabilityCsv && isReadyForExport && isRawExportEnabled;
}));
//# sourceMappingURL=executionResultsSelectors.js.map