import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import cx from "classnames";
import { FormattedMessage, defineMessage } from "react-intl";
import { insightBucket, insightHasAttributes } from "@gooddata/sdk-model";
import { BucketNames } from "@gooddata/sdk-ui";
import { Bubble, BubbleHoverTrigger, Message } from "@gooddata/sdk-ui-kit";
import { ConfigurationPanelContent } from "./ConfigurationPanelContent.js";
import { messages } from "../../../locales.js";
import { BUBBLE_ARROW_OFFSET_X, BUBBLE_ARROW_OFFSET_Y, HIDE_DELAY_DEFAULT, SHOW_DELAY_DEFAULT, } from "../../constants/bubble.js";
import { getMeasuresFromMdObject } from "../../utils/bucketHelper.js";
import { LabelSubsection } from "../configurationControls/axis/LabelSubsection.js";
import { NameSubsection } from "../configurationControls/axis/NameSubsection.js";
import { CheckboxControl } from "../configurationControls/CheckboxControl.js";
import { ClusteringThresholdControl } from "../configurationControls/clustering/ClusteringThresholdControl.js";
import { NumberOfClustersControl } from "../configurationControls/clustering/NumberOfClustersControl.js";
import { ConfigSection } from "../configurationControls/ConfigSection.js";
import { DataLabelsControl } from "../configurationControls/DataLabelsControl.js";
import { MinMaxControl } from "../configurationControls/MinMaxControl.js";
import { countItemsOnAxes } from "../pluggableVisualizations/baseChart/insightIntrospection.js";
export class ScatterPlotConfigurationPanel extends ConfigurationPanelContent {
    isControlDisabled() {
        const { insight, isError, isLoading } = this.props;
        const measures = getMeasuresFromMdObject(insight);
        return !!(!measures || measures.length < 1 || isError || isLoading);
    }
    renderConfigurationPanel() {
        const { xAxisVisible, gridEnabled, yAxisVisible, clusteringEnabled } = this.getControlProperties();
        const { featureFlags, propertiesMeta, properties, pushData, insight, type } = this.props;
        const controls = properties?.controls;
        const controlsDisabled = this.isControlDisabled();
        const { xaxis: itemsOnXAxis, yaxis: itemsOnYAxis } = countItemsOnAxes(type, controls, insight);
        const xAxisNameSectionDisabled = controlsDisabled || itemsOnXAxis !== 1;
        const yAxisNameSectionDisabled = controlsDisabled || itemsOnYAxis !== 1;
        const showClusteringSection = !!featureFlags?.enableScatterPlotClustering;
        const isScatterPlotClusteringDisabled = this.isClusteringDisabled();
        const showingPartialClusters = propertiesMeta?.showingPartialClusters;
        return (_jsxs(BubbleHoverTrigger, { showDelay: SHOW_DELAY_DEFAULT, hideDelay: HIDE_DELAY_DEFAULT, children: [
                _jsxs("div", { children: [this.renderColorSection(), this.renderLegendSection(), _jsxs(ConfigSection, { id: "xaxis_section", title: messages["xaxisTitle"].id, valuePath: "xaxis.visible", canBeToggled: true, toggledOn: xAxisVisible, toggleDisabled: controlsDisabled, propertiesMeta: propertiesMeta, properties: properties, pushData: pushData, children: [
                                _jsx(NameSubsection, { disabled: xAxisNameSectionDisabled, configPanelDisabled: controlsDisabled, axis: "xaxis", properties: properties, pushData: pushData }), _jsx(LabelSubsection, { disabled: controlsDisabled, configPanelDisabled: controlsDisabled, axis: "xaxis", properties: properties, pushData: pushData, showFormat: true }), this.renderMinMax("xaxis")] }), _jsxs(ConfigSection, { id: "yaxis_section", title: messages["yaxisTitle"].id, valuePath: "yaxis.visible", canBeToggled: true, toggledOn: yAxisVisible, toggleDisabled: controlsDisabled, propertiesMeta: propertiesMeta, properties: properties, pushData: pushData, children: [
                                _jsx(NameSubsection, { disabled: yAxisNameSectionDisabled, configPanelDisabled: controlsDisabled, axis: "yaxis", properties: properties, pushData: pushData }), _jsx(LabelSubsection, { disabled: controlsDisabled, configPanelDisabled: controlsDisabled, axis: "yaxis", properties: properties, pushData: pushData, showFormat: true }), this.renderMinMax("yaxis")] }), this.renderInteractionsSection(), _jsxs(ConfigSection, { id: "canvas_section", title: messages["canvasTitle"].id, propertiesMeta: propertiesMeta, properties: properties, pushData: pushData, children: [
                                _jsx(DataLabelsControl, { pushData: pushData, properties: properties, isDisabled: this.areDataLabelsDisabled(), defaultValue: false, showDisabledMessage: this.isDataLabelsWarningShown() }), _jsx(CheckboxControl, { valuePath: "grid.enabled", labelText: messages["canvasGridLine"].id, properties: properties, checked: gridEnabled, disabled: controlsDisabled, pushData: pushData })
                            ] }), showClusteringSection ? (_jsxs(ConfigSection, { id: "clustering_section", title: messages["clusteringTitle"].id, propertiesMeta: propertiesMeta, properties: properties, pushData: pushData, valuePath: "clustering.enabled", canBeToggled: true, toggledOn: clusteringEnabled, toggleDisabled: controlsDisabled || isScatterPlotClusteringDisabled, showDisabledMessage: isScatterPlotClusteringDisabled, toggleMessageId: defineMessage({ id: "properties.clustering.disabled" }).id, children: [
                                _jsx(NumberOfClustersControl, { valuePath: "clustering.numberOfClusters", disabled: controlsDisabled || isScatterPlotClusteringDisabled, properties: properties, pushData: pushData }), _jsx(ClusteringThresholdControl, { valuePath: "clustering.threshold", disabled: controlsDisabled || isScatterPlotClusteringDisabled, properties: properties, pushData: pushData }), showingPartialClusters ? (_jsxs(Message, { type: "progress", className: "adi-input-progress", children: [
                                        _jsx("h4", { children: _jsx(FormattedMessage, { id: "properties.clustering.amount.partial.title" }) }), _jsx(FormattedMessage, { id: "properties.clustering.amount.partial.description" })
                                    ] })) : null] })) : null, this.renderAdvancedSection()] }), _jsx(Bubble, { className: this.getBubbleClassNames(), arrowOffsets: { "tc bc": [BUBBLE_ARROW_OFFSET_X, BUBBLE_ARROW_OFFSET_Y] }, alignPoints: [{ align: "tc bc" }], children: _jsx(FormattedMessage, { id: "properties.config.not_applicable" }) })
            ] }));
    }
    renderMinMax(basePath) {
        const { pushData, properties, propertiesMeta } = this.props;
        return (_jsx(MinMaxControl, { isDisabled: this.isControlDisabled(), basePath: basePath, pushData: pushData, properties: properties, propertiesMeta: propertiesMeta }));
    }
    areDataLabelsDisabled() {
        const isDisabled = super.isControlDisabled();
        return isDisabled || !insightHasAttributes(this.props.insight);
    }
    isDataLabelsWarningShown() {
        const isDisabled = super.isControlDisabled();
        return !isDisabled && !insightHasAttributes(this.props.insight);
    }
    isClusteringDisabled() {
        const isDisabled = super.isControlDisabled();
        const segmentByBucket = this.props?.insight
            ? insightBucket(this.props.insight, BucketNames.SEGMENT)
            : undefined;
        const viewByBucket = this.props?.insight
            ? insightBucket(this.props.insight, BucketNames.ATTRIBUTE)
            : undefined;
        const measuresBucket = this.props?.insight
            ? insightBucket(this.props.insight, BucketNames.MEASURES)
            : undefined;
        const secondaryMeasuresBucket = this.props?.insight
            ? insightBucket(this.props.insight, BucketNames.SECONDARY_MEASURES)
            : undefined;
        const hasSegmentBucketItems = (segmentByBucket?.items?.length ?? 0) > 0;
        const hasViewBucketItems = (viewByBucket?.items?.length ?? 0) > 0;
        const hasXAxisMeasure = (measuresBucket?.items?.length ?? 0) === 1;
        const hasYAxisMeasure = (secondaryMeasuresBucket?.items?.length ?? 0) === 1;
        return (isDisabled || hasSegmentBucketItems || !hasViewBucketItems || !hasXAxisMeasure || !hasYAxisMeasure);
    }
    getBubbleClassNames() {
        return cx("bubble-primary", {
            invisible: !this.isControlDisabled(),
        });
    }
    getControlProperties() {
        const propertiesControls = this.props.properties?.controls;
        const xAxisVisible = propertiesControls?.["xaxis"]?.visible ?? true;
        const yAxisVisible = propertiesControls?.["yaxis"]?.visible ?? true;
        const gridEnabled = propertiesControls?.["grid"]?.enabled ?? true;
        const clusteringEnabled = propertiesControls?.["clustering"]?.enabled ?? false;
        return {
            xAxisVisible,
            yAxisVisible,
            gridEnabled,
            clusteringEnabled,
        };
    }
}
//# sourceMappingURL=ScatterPlotConfigurationPanel.js.map