// (C) 2021-2026 GoodData Corporation
import { call, select } from "redux-saga/effects";
import { newBucket, newInsightDefinition, newMeasure, objRefToString, serializeObjRef, } from "@gooddata/sdk-model";
import { sanitizeDateDatasetTitle, sortByRelevanceAndTitle, } from "../../_staging/catalog/dateDatasetOrdering.js";
import { invalidQueryArguments } from "../events/general.js";
import { createCachedQueryService } from "../store/_infra/queryService.js";
import { selectAllCatalogMeasuresMap } from "../store/catalog/catalogSelectors.js";
import { loadDateDatasetsForInsight } from "./loadAvailableDateDatasets.js";
export const QueryDateDatasetsForMeasureService = createCachedQueryService("GDC.DASH/QUERY.MEASURE.DATE.DATASETS", queryService, (query) => serializeObjRef(query.payload.measureRef));
/**
 * Selector that will return date datasets for a measure. The input to the selector is the dashboard query that is used
 * to obtain and cache the data.
 *
 * This selector will return undefined if the query to obtain the data for a particular measure was not yet fired or
 * processed. Otherwise, will return object containing `status` of the data retrieval; if the `status` is
 * `'success'` then the `result` prop will contain the data.
 *
 * @remarks see {@link IQueryMeasureDateDatasets}
 * @internal
 */
export const selectDateDatasetsForMeasure = QueryDateDatasetsForMeasureService.cache.selectQueryResult;
//
// Query implementation
//
function* queryService(ctx, query) {
    const { payload: { measureRef }, } = query;
    const measures = yield select(selectAllCatalogMeasuresMap);
    const measure = measures.get(measureRef);
    if (!measure) {
        throw invalidQueryArguments(ctx, `Measure ${objRefToString(measureRef)} does not exist.`, query.correlationId);
    }
    const intermediateInsight = newInsightDefinition("local:headline", (i) => i.buckets([
        newBucket("measures", newMeasure(measure.measure.ref, (m) => m.localId("measure_date_datasets_availability"))),
    ]));
    const dateDatasets = yield call(loadDateDatasetsForInsight, ctx, intermediateInsight);
    const dateDatasetDisplayNames = Object.fromEntries(dateDatasets.map((d) => [d.dataSet.title, sanitizeDateDatasetTitle(d)]));
    return {
        dateDatasets,
        dateDatasetsOrdered: sortByRelevanceAndTitle(dateDatasets, dateDatasetDisplayNames),
        dateDatasetDisplayNames,
    };
}
