// (C) 2021-2026 GoodData Corporation
import { all, call, put } from "redux-saga/effects";
import { resolveLocale } from "@gooddata/sdk-ui";
import { defaultDateFilterConfig } from "../../../../_staging/dateFilterConfig/defaultConfig.js";
import { FallbackToDefault, getValidDateFilterConfig, validateDateFilterConfig, } from "../../../../_staging/dateFilterConfig/validation.js";
import { stripUserAndWorkspaceProps } from "../../../../_staging/settings/conversion.js";
import { tabsActions } from "../../../store/tabs/index.js";
import { isResolvedConfig, } from "../../../types/commonTypes.js";
import { loadAutomationsData } from "../common/loadAutomationsData.js";
import { onDateFilterConfigValidationError } from "./onDateFilterConfigValidationError.js";
import { sanitizeUnfinishedFeatureSettings } from "./sanitizeUnfinishedFeatureSettings.js";
function loadDateFilterConfig({ backend, workspace, }) {
    return backend
        .workspace(workspace)
        .dateFilterConfigs()
        .withLimit(1)
        .query()
        .catch((e) => {
        console.warn("An error has occurred while loading date filter config. Will fall back to default date filter config.", e);
        return undefined;
    });
}
function loadSettingsForCurrentUser({ backend, workspace, }) {
    return backend.workspace(workspace).settings().getSettingsForCurrentUser();
}
///
async function loadCustomDateFilterConfig({ backend, workspace, }) {
    const customDateFilterConfig = await backend
        .workspace(workspace)
        .dateFilterConfigs()
        .withLimit(1)
        .queryCustomDateFilterConfig();
    if (!customDateFilterConfig.items[0]) {
        return undefined;
    }
    const configValidation = validateDateFilterConfig(customDateFilterConfig.items[0]);
    const validConfig = !FallbackToDefault.includes(configValidation);
    return validConfig ? customDateFilterConfig.items[0] : undefined;
}
function loadColorPalette({ backend, workspace }) {
    return backend.workspace(workspace).styling().getColorPalette();
}
function* resolveDateFilterConfig(ctx, config, cmd) {
    if (config.dateFilterConfig) {
        return config.dateFilterConfig;
    }
    else if (config?.settings?.dateFilterConfig) {
        return config.settings.dateFilterConfig;
    }
    const customDateFilterConfig = yield call(loadCustomDateFilterConfig, ctx);
    if (customDateFilterConfig) {
        return customDateFilterConfig;
    }
    else {
        const result = yield call(loadDateFilterConfig, ctx);
        if ((result?.totalCount ?? 0) > 1) {
            yield call(onDateFilterConfigValidationError, ctx, "TOO_MANY_CONFIGS", cmd.correlationId);
        }
        const firstConfig = result?.items[0];
        if (!firstConfig) {
            yield call(onDateFilterConfigValidationError, ctx, "NO_CONFIG", cmd.correlationId);
        }
        return result?.items[0] ?? defaultDateFilterConfig;
    }
}
function resolveUserSettings(ctx, config) {
    if (config.settings && config.locale && config.separators) {
        return Promise.resolve({
            locale: config.locale,
            separators: config.separators,
            settings: config.settings,
        });
    }
    return loadSettingsForCurrentUser(ctx).then((res) => ({
        locale: config.locale ?? resolveLocale(res.locale),
        separators: config.separators ?? res.separators,
        settings: config.settings ?? stripUserAndWorkspaceProps(res),
    }));
}
function resolveColorPalette(ctx, config) {
    if (config.colorPalette) {
        return Promise.resolve(config.colorPalette);
    }
    return loadColorPalette(ctx);
}
/**
 * Loads all essential dashboard configuration from the backend if needed. The load command may specify their
 * own inline config - if that is the case the config is bounced back immediately. Otherwise, the necessary
 * backend queries and post-processing is done.
 */
export function* resolveDashboardConfig(ctx, cmd) {
    const { payload: { config = {} }, } = cmd;
    yield put(tabsActions.clearDateFilterConfigValidationWarning());
    if (isResolvedConfig(config)) {
        /*
         * Config coming in props is fully specified. There is nothing to do. Bail out immediately.
         */
        if (config.allowUnfinishedFeatures || !config.settings) {
            return applyConfigDefaults(config);
        }
        return {
            ...applyConfigDefaults(config),
            settings: sanitizeUnfinishedFeatureSettings(config.settings),
        };
    }
    /*
     * Resolve the config values. The resolve* functions will take value from config if it is defined,
     * otherwise they will obtain the config from backend.
     *
     * Note: the user settings include locale, separators and the ISettings that should be in effect
     * for the current user in the context of the workspace.
     */
    const [dateFilterConfig, settings, colorPalette] = yield all([
        call(resolveDateFilterConfig, ctx, config, cmd),
        call(resolveUserSettings, ctx, config),
        call(resolveColorPalette, ctx, config),
    ]);
    const [validDateFilterConfig, configValidation] = getValidDateFilterConfig(dateFilterConfig, settings.settings);
    if (configValidation !== "Valid") {
        yield call(onDateFilterConfigValidationError, ctx, configValidation, cmd.correlationId);
    }
    const configWithDefaults = applyConfigDefaults(config);
    return {
        ...configWithDefaults,
        locale: settings.locale,
        separators: settings.separators,
        dateFilterConfig: validDateFilterConfig,
        settings: configWithDefaults.allowUnfinishedFeatures
            ? settings.settings
            : sanitizeUnfinishedFeatureSettings(settings.settings),
        colorPalette,
        // Prefer explicit config tokens, otherwise fall back to tokens available in backend settings
        mapboxToken: config.mapboxToken ?? settings.settings?.["mapboxToken"],
        agGridToken: config.agGridToken ?? settings.settings?.["agGridToken"],
        maxZoomLevel: config.maxZoomLevel === undefined
            ? settings.settings?.maxZoomLevel
            : config.maxZoomLevel,
    };
}
const CONFIG_DEFAULTS = {
    objectAvailability: {},
    isReadOnly: false,
    isEmbedded: false,
    isExport: false,
    isWhiteLabeled: false,
    disableDefaultDrills: false,
    enableFilterValuesResolutionInDrillEvents: false,
    menuButtonItemsVisibility: {},
    allowUnfinishedFeatures: false,
    allowCreateInsightRequest: false,
    initialRenderMode: "view",
    hideSaveAsNewButton: false,
    hideShareButton: false,
    disableCrossFiltering: false,
    disableUserFilterReset: false,
    widgetsOverlay: {},
    externalRecipient: undefined,
    hideAddTabButton: false,
};
function applyConfigDefaults(config) {
    return {
        ...CONFIG_DEFAULTS,
        ...config,
    };
}
/**
 * Resolves dashboard config
 */
export function* resolveDashboardConfigAndFeatureFlagDependentCalls(ctx, cmd) {
    const resolvedConfig = yield call(resolveDashboardConfig, ctx, cmd);
    const additionalData = yield call(loadAutomationsData, ctx, resolvedConfig.settings);
    return {
        resolvedConfig,
        additionalData,
    };
}
//# sourceMappingURL=resolveDashboardConfig.js.map