// (C) 2022-2025 GoodData Corporation
import { bucketAttribute, bucketAttributes, bucketItems, bucketMeasure, bucketMeasures, insightFilters, insightProperties, insightTotals, insightVisualizationType, isRelativeDateFilter, relativeDateFilterValues, } from "@gooddata/sdk-model";
import { DefaultLocale } from "@gooddata/sdk-ui";
import { bucketConversion, insightConversion } from "./convertor.js";
import { removeUseless } from "../../removeUseless.js";
import { createSorts } from "../../sort.js";
function namedSdkModelPropMetaFor(name, propType) {
    return {
        cardinality: propType,
        typeImport: {
            name,
            importType: "named",
            package: "@gooddata/sdk-model",
        },
    };
}
function namedSdkModelPropGroupMetaFor(name) {
    return {
        Single: namedSdkModelPropMetaFor(name, "scalar"),
        Multiple: namedSdkModelPropMetaFor(name, "array"),
    };
}
export const sdkModelPropMetas = {
    Measure: namedSdkModelPropGroupMetaFor("IMeasure"),
    Attribute: namedSdkModelPropGroupMetaFor("IAttribute"),
    AttributeOrMeasure: namedSdkModelPropGroupMetaFor("IAttributeOrMeasure"),
    Filter: namedSdkModelPropGroupMetaFor("IFilter"),
    SortItem: namedSdkModelPropGroupMetaFor("ISortItem"),
    Total: namedSdkModelPropGroupMetaFor("ITotal"),
};
function firstBucketItem(bucket) {
    return bucketItems(bucket)?.[0];
}
/**
 * Utility function for creating bucket conversion for a single {@link @gooddata/sdk-model#IAttribute} item.
 */
export function singleAttributeBucketConversion(propName, bucketName) {
    return bucketConversion(propName, sdkModelPropMetas.Attribute.Single, bucketName, bucketAttribute);
}
/**
 * Utility function for creating bucket conversion for multiple {@link @gooddata/sdk-model#IAttribute} items.
 */
export function multipleAttributesBucketConversion(propName, bucketName) {
    return bucketConversion(propName, sdkModelPropMetas.Attribute.Multiple, bucketName, bucketAttributes);
}
/**
 * Utility function for creating bucket conversion for a single {@link @gooddata/sdk-model#IMeasure} item.
 */
export function singleMeasureBucketConversion(propName, bucketName) {
    return bucketConversion(propName, sdkModelPropMetas.Measure.Single, bucketName, bucketMeasure);
}
/**
 * Utility function for creating bucket conversion for multiple {@link @gooddata/sdk-model#IMeasure} items.
 */
export function multipleMeasuresBucketConversion(propName, bucketName) {
    return bucketConversion(propName, sdkModelPropMetas.Measure.Multiple, bucketName, bucketMeasures);
}
/**
 * Utility function for creating bucket conversion for a single {@link @gooddata/sdk-model#IAttributeOrMeasure} item.
 */
export function singleAttributeOrMeasureBucketConversion(propName, bucketName) {
    return bucketConversion(propName, sdkModelPropMetas.AttributeOrMeasure.Single, bucketName, firstBucketItem);
}
/**
 * Utility function for creating bucket conversion for multiple {@link @gooddata/sdk-model#IAttributeOrMeasure} items.
 */
export function multipleAttributesOrMeasuresBucketConversion(propName, bucketName) {
    return bucketConversion(propName, sdkModelPropMetas.AttributeOrMeasure.Multiple, bucketName, bucketItems);
}
/**
 * Utility function for creating insight conversion for multiple {@link @gooddata/sdk-model#IFilter} items.
 */
export function filtersInsightConversion(propName) {
    return insightConversion(propName, sdkModelPropMetas.Filter.Multiple, (insight) => {
        const filters = insightFilters(insight) ?? [];
        return filters.filter((f) => {
            // get rid of the filters AD creates as All time: relative filter without boundaries
            if (isRelativeDateFilter(f)) {
                const { from, to } = relativeDateFilterValues(f);
                return !(from === null || from === undefined || to === null || to === undefined);
            }
            return true;
        });
    });
}
/**
 * Utility function for creating insight conversion for multiple {@link @gooddata/sdk-model#ISortItem} items.
 */
export function sortsInsightConversion(propName) {
    return insightConversion(propName, sdkModelPropMetas.SortItem.Multiple, (insight) => {
        const type = insightVisualizationType(insight);
        return createSorts(type, insight, insightProperties(insight)?.["controls"] ?? {});
    });
}
/**
 * Utility function for creating insight conversion for multiple {@link @gooddata/sdk-model#ITotal} items.
 */
export function totalsInsightConversion(propName) {
    return insightConversion(propName, sdkModelPropMetas.Total.Multiple, insightTotals);
}
/**
 * Utility function for creating insight conversion for single {@link @gooddata/sdk-ui#ILocale} item.
 */
export function localeInsightConversion(propName) {
    return insightConversion(propName, { cardinality: "scalar" }, (_, ctx) => {
        const val = ctx?.settings?.locale;
        return val && val !== DefaultLocale ? val : undefined;
    });
}
/**
 * Utility function for creating insight conversion for single {@link @gooddata/sdk-ui#IExecutionConfig} item.
 */
export function executionConfigInsightConversion(propName) {
    return insightConversion(propName, {
        cardinality: "scalar",
        typeImport: {
            importType: "named",
            name: "IExecutionConfig",
            package: "@gooddata/sdk-model",
        },
    }, (_, ctx) => {
        return ctx?.executionConfig && removeUseless(ctx.executionConfig);
    });
}
//# sourceMappingURL=convenience.js.map