import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2019-2026 GoodData Corporation
import { cloneDeep, set } from "lodash-es";
import { BucketNames, VisualizationTypes } from "@gooddata/sdk-ui";
import { BUCKETS, METRIC } from "../../../constants/bucket.js";
import { BUBBLE_CHART_SUPPORTED_PROPERTIES } from "../../../constants/supportedProperties.js";
import { DEFAULT_BUBBLE_CHART_CONFIG } from "../../../constants/uiConfig.js";
import { configureOverTimeComparison, configurePercent } from "../../../utils/bucketConfig.js";
import { getAllAttributeItems, getAllMeasures, getPreferredBucketItems, limitNumberOfMeasuresInBuckets, removeAllArithmeticMeasuresFromDerived, removeAllDerivedMeasures, sanitizeFilters, } from "../../../utils/bucketHelper.js";
import { getReferencePointWithSupportedProperties } from "../../../utils/propertiesHelper.js";
import { removeSort } from "../../../utils/sort.js";
import { setBubbleChartUiConfig } from "../../../utils/uiConfigHelpers/bubbleChartUiConfigHelper.js";
import { BubbleChartConfigurationPanel } from "../../configurationPanels/BubbleChartConfigurationPanel.js";
import { PluggableBaseChart } from "../baseChart/PluggableBaseChart.js";
/**
 * PluggableBubbleChart
 *
 * ## Buckets
 *
 * | Name             | Id                 | Accepts             |
 * |------------------|--------------------|---------------------|
 * | Measure (X-axis) | measures           | measures only       |
 * | Measure (Y-axis) | secondary_measures | measures only       |
 * | Measure (Size)   | tertiary_measures  | measures only       |
 * | ViewBy           | view               | attributes or dates |
 *
 * ### Bucket axioms
 *
 * - |MeasureX| ≤ 1
 * - |MeasureY| ≤ 1
 * - |MeasureSize| ≤ 1
 * - |ViewBy| ≤ 1
 * - |MeasureX| + |MeasureY| + |MeasureSize| ≥ 1
 *
 * ## Dimensions
 *
 * The PluggableBubbleChart always creates the same two dimensional execution.
 *
 * - ⊤ ⇒ [[...ViewBy], [MeasureGroupIdentifier]]
 *
 * ## Sorts
 *
 * The PluggableBubbleChart does not use any sorts.
 */
export class PluggableBubbleChart extends PluggableBaseChart {
    constructor(props) {
        super(props);
        this.type = VisualizationTypes.BUBBLE;
        this.supportedPropertiesList = BUBBLE_CHART_SUPPORTED_PROPERTIES;
        this.initializeProperties(props.visualizationProperties);
    }
    getExtendedReferencePoint(referencePoint) {
        const clonedReferencePoint = cloneDeep(referencePoint);
        const uiConfig = cloneDeep(DEFAULT_BUBBLE_CHART_CONFIG);
        this.addMetricToFiltersIfEnabled(uiConfig);
        let newReferencePoint = {
            ...clonedReferencePoint,
            uiConfig,
        };
        newReferencePoint = removeAllArithmeticMeasuresFromDerived(newReferencePoint);
        newReferencePoint = removeAllDerivedMeasures(newReferencePoint);
        const buckets = limitNumberOfMeasuresInBuckets(clonedReferencePoint.buckets, 3);
        // limit number of measures in the ref point here?
        // Check if there are three measure buckets
        const measuresBucketItems = getPreferredBucketItems(buckets, [BucketNames.MEASURES], [METRIC]);
        const secondaryMeasuresBucketItems = getPreferredBucketItems(buckets, [BucketNames.SECONDARY_MEASURES], [METRIC]);
        const tertiaryMeasuresBucketItems = getPreferredBucketItems(buckets, [BucketNames.TERTIARY_MEASURES], [METRIC]);
        const allMeasures = getAllMeasures(buckets);
        // skip first to reserve first items to be picked later
        const secondaryAndTertiaryItems = [
            ...secondaryMeasuresBucketItems.slice(0, 1),
            ...tertiaryMeasuresBucketItems.slice(0, 1),
        ];
        const measures = measuresBucketItems.length > 0
            ? measuresBucketItems.slice(0, 1)
            : allMeasures.filter((measure) => !secondaryAndTertiaryItems.includes(measure)).slice(0, 1);
        const secondaryMeasures = secondaryMeasuresBucketItems.length > 0
            ? secondaryMeasuresBucketItems.slice(0, 1)
            : allMeasures
                .filter((measure) => ![...measures, ...tertiaryMeasuresBucketItems.slice(0, 1)].includes(measure))
                .slice(0, 1);
        const tertiaryMeasures = tertiaryMeasuresBucketItems.length > 0
            ? tertiaryMeasuresBucketItems.slice(0, 1)
            : allMeasures
                .filter((measure) => ![...measures, ...secondaryMeasures].includes(measure))
                .slice(0, 1);
        set(newReferencePoint, BUCKETS, [
            {
                localIdentifier: BucketNames.MEASURES,
                items: measures,
            },
            {
                localIdentifier: BucketNames.SECONDARY_MEASURES,
                items: secondaryMeasures,
            },
            {
                localIdentifier: BucketNames.TERTIARY_MEASURES,
                items: tertiaryMeasures,
            },
            {
                localIdentifier: BucketNames.VIEW,
                items: getAllAttributeItems(buckets).slice(0, 1),
            },
        ]);
        newReferencePoint = setBubbleChartUiConfig(newReferencePoint, this.intl, this.type);
        newReferencePoint = configurePercent(newReferencePoint, true);
        newReferencePoint = configureOverTimeComparison(newReferencePoint);
        newReferencePoint = getReferencePointWithSupportedProperties(newReferencePoint, this.supportedPropertiesList);
        newReferencePoint = removeSort(newReferencePoint);
        return Promise.resolve(sanitizeFilters(newReferencePoint, this.featureFlags?.enableImprovedAdFilters, referencePoint));
    }
    renderConfigurationPanel(insight, options) {
        const configPanelElement = this.getConfigPanelElement();
        if (configPanelElement) {
            const panelConfig = {
                supportsAttributeHierarchies: this.backendCapabilities.supportsAttributeHierarchies,
            };
            this.renderFun(_jsx(BubbleChartConfigurationPanel, { locale: this.locale, references: this.references, properties: this.visualizationProperties, propertiesMeta: this.propertiesMeta, insight: insight, colors: this.colors, pushData: this.handlePushData, type: this.type, isError: this.getIsError(), isLoading: this.isLoading, featureFlags: this.featureFlags, panelConfig: panelConfig, configurationPanelRenderers: options.custom?.configurationPanelRenderers }), configPanelElement);
        }
        return null;
    }
}
//# sourceMappingURL=PluggableBubbleChart.js.map