// (C) 2025-2026 GoodData Corporation
import { insightProperties, insightSanitize, } from "@gooddata/sdk-model";
import { BucketNames } from "@gooddata/sdk-ui";
import { pivotTableNextAdditionalFactories } from "./pivotTableNextAdditionalFactories.js";
import { pivotTableNextConfigFromInsight } from "./pivotTableNextConfigFromInsight.js";
import { PluggablePivotTableNext } from "./PluggablePivotTableNext.js";
import { getReactEmbeddingCodeGenerator } from "../../../utils/embeddingCodeGenerator/getReactEmbeddingCodeGenerator.js";
import { executionConfigInsightConversion, filtersInsightConversion, localeInsightConversion, multipleAttributesBucketConversion, multipleAttributesOrMeasuresBucketConversion, sortsInsightConversion, totalsInsightConversion, } from "../../../utils/embeddingCodeGenerator/insightToPropsConverter/convenience.js";
import { getInsightToPropsConverter, insightConversion, } from "../../../utils/embeddingCodeGenerator/insightToPropsConverter/convertor.js";
import { getPageSizeFromProperties, getPaginationFromProperties } from "../../../utils/propertiesHelper.js";
import { BaseChartDescriptor } from "../baseChart/BaseChartDescriptor.js";
import { MIN_VISUALIZATION_HEIGHT_TABLE_REPEATER_FLEXIBLE_LAYOUT } from "../constants.js";
import { addIntersectionFiltersToInsight, modifyBucketsAttributesForDrillDown, sanitizeTableProperties, } from "../drillDownUtil.js";
export class PivotTableNextDescriptor extends BaseChartDescriptor {
    getFactory() {
        return (params) => new PluggablePivotTableNext(params);
    }
    getSizeInfo(_insight, layoutDescriptor, settings) {
        return {
            width: {
                default: 4,
                min: 2,
                max: layoutDescriptor.gridColumnsCount,
            },
            height: {
                default: this.getDefaultHeight(settings),
                min: this.getMinHeight(settings),
                max: this.getMaxHeight(settings),
            },
        };
    }
    getMinHeight(_settings) {
        // Flexible layout is always enabled now
        return MIN_VISUALIZATION_HEIGHT_TABLE_REPEATER_FLEXIBLE_LAYOUT;
    }
    applyDrillDown(insight, drillDownContext, backendSupportsElementUris) {
        const drillDownInsight = modifyBucketsAttributesForDrillDown(insight, drillDownContext.drillDefinition);
        const drillDownInsightWithFilters = addIntersectionFiltersToInsight(drillDownInsight, drillDownContext.event.drillContext.intersection, backendSupportsElementUris);
        return sanitizeTableProperties(insightSanitize(drillDownInsightWithFilters));
    }
    getEmbeddingCode = getReactEmbeddingCodeGenerator({
        component: {
            importType: "named",
            name: "PivotTableNext",
            package: "@gooddata/sdk-ui-pivot/next",
        },
        insightToProps: getInsightToPropsConverter({
            measures: multipleAttributesOrMeasuresBucketConversion("measures", BucketNames.MEASURES),
            rows: multipleAttributesBucketConversion("rows", BucketNames.ATTRIBUTE),
            columns: multipleAttributesBucketConversion("columns", BucketNames.COLUMNS),
            filters: filtersInsightConversion("filters"),
            sortBy: sortsInsightConversion("sortBy"),
            totals: totalsInsightConversion("totals"),
            config: insightConversion("config", {
                typeImport: {
                    importType: "named",
                    name: "PivotTableNextConfig",
                    package: "@gooddata/sdk-ui-pivot/next",
                },
                cardinality: "scalar",
            }, pivotTableNextConfigFromInsight),
            pageSize: insightConversion("pageSize", { cardinality: "scalar" }, (insight) => {
                const pagination = getPaginationFromProperties(insightProperties(insight));
                const pageSize = getPageSizeFromProperties(insightProperties(insight));
                // Only include pageSize when pagination is enabled and pageSize is set
                return pagination?.enabled && pageSize !== undefined ? pageSize : undefined;
            }),
            locale: localeInsightConversion("locale"),
            execConfig: executionConfigInsightConversion("execConfig"),
        }),
        additionalFactories: pivotTableNextAdditionalFactories,
    });
    getMeta() {
        return {
            documentationUrl: "https://www.gooddata.com/docs/gooddata-ui/latest/references/visual_components/pivot_table_next",
            supportsExport: true,
            supportsZooming: false,
        };
    }
}
//# sourceMappingURL=PivotTableNextDescriptor.js.map