import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2019-2026 GoodData Corporation
import { cloneDeep, isEmpty, set } from "lodash-es";
import { bucketsItems, insightBucket, insightBuckets, newAttributeSort, } from "@gooddata/sdk-model";
import { BucketNames, VisualizationTypes } from "@gooddata/sdk-ui";
import { ATTRIBUTE, BUCKETS, DATE } from "../../../constants/bucket.js";
import { AREA_CHART_SUPPORTED_PROPERTIES, OPTIONAL_STACKING_PROPERTIES, } from "../../../constants/supportedProperties.js";
import { AREA_UICONFIG_WITH_MULTIPLE_DATES, MAX_CATEGORIES_COUNT, MAX_METRICS_COUNT, } from "../../../constants/uiConfig.js";
import { newAvailableSortsGroup } from "../../../interfaces/SortConfig.js";
import { configureOverTimeComparison, configurePercent } from "../../../utils/bucketConfig.js";
import { getAllAttributeItemsWithPreference, getBucketItems, getFilteredMeasuresForStackedCharts, getStackItems, limitNumberOfMeasuresInBuckets, removeAllArithmeticMeasuresFromDerived, removeAllDerivedMeasures, sanitizeFilters, } from "../../../utils/bucketHelper.js";
import { getReferencePointWithSupportedProperties, removeImmutableOptionalStackingProperties, } from "../../../utils/propertiesHelper.js";
import { getCustomSortDisabledExplanation } from "../../../utils/sort.js";
import { setAreaChartUiConfig } from "../../../utils/uiConfigHelpers/areaChartUiConfigHelper.js";
import { LineChartBasedConfigurationPanel } from "../../configurationPanels/LineChartBasedConfigurationPanel.js";
import { PluggableBaseChart } from "../baseChart/PluggableBaseChart.js";
import { addIntersectionFiltersToInsight, modifyBucketsAttributesForDrillDown, reverseAndTrimIntersection, } from "../drillDownUtil.js";
/**
 * PluggableAreaChart
 *
 * ## Buckets
 *
 * | Name     | Id       | Accepts             |
 * |----------|----------|---------------------|
 * | Measures | measures | measures only       |
 * | ViewBy   | view     | attributes or dates |
 * | StackBy  | stack    | attributes only     |
 *
 * ### Bucket axioms
 *
 * - |Measures| ≥ 1
 * - |ViewBy| ≤ 2
 * - |StackBy| ≤ 1
 * - |ViewBy| + |StackBy| ≤ 2
 * - |ViewBy| + |StackBy| = 2 ⇒ |Measures| ≤ 1
 * - |ViewBy| + |StackBy| \< 2 ⇒ |Measures| ≤ 20
 *
 * ## Dimensions
 *
 * The PluggableAreaChart always creates two dimensional execution.
 *
 * - |StackBy| = 1 ∧ |ViewBy| ≥ 1 ⇒ [[StackBy[0]], [ViewBy[0], MeasureGroupIdentifier]]
 * - |StackBy| = 1 ∧ |ViewBy| = 0 ⇒ [[StackBy[0]], [MeasureGroupIdentifier]]
 * - |StackBy| = 0 ∧ |ViewBy| = 2 ⇒ [[ViewBy[1]], [ViewBy[0], MeasureGroupIdentifier]]
 * - |StackBy| = 0 ∧ |ViewBy| = 1 ⇒ [[MeasureGroupIdentifier], [ViewBy[0]]]
 * - |StackBy| = 0 ∧ |ViewBy| = 0 ⇒ [[MeasureGroupIdentifier], []]]
 *
 * ## Sorts
 *
 * Unless the user specifies otherwise, PluggableAreaChart does not use any sorts.
 *
 */
export class PluggableAreaChart extends PluggableBaseChart {
    constructor(props) {
        super(props);
        this.type = VisualizationTypes.AREA;
        this.defaultControlsProperties = {
            stackMeasures: true,
        };
        this.initializeProperties(props.visualizationProperties);
    }
    getUiConfig() {
        const config = cloneDeep(AREA_UICONFIG_WITH_MULTIPLE_DATES);
        this.addMetricToFiltersIfEnabled(config);
        return config;
    }
    getExtendedReferencePoint(referencePoint) {
        const clonedReferencePoint = cloneDeep(referencePoint);
        let newReferencePoint = {
            ...clonedReferencePoint,
            uiConfig: this.getUiConfig(),
        };
        newReferencePoint = removeAllArithmeticMeasuresFromDerived(newReferencePoint);
        newReferencePoint = removeAllDerivedMeasures(newReferencePoint);
        this.configureBuckets(newReferencePoint);
        newReferencePoint = setAreaChartUiConfig(newReferencePoint, this.intl, this.type);
        newReferencePoint = configurePercent(newReferencePoint, false);
        newReferencePoint = configureOverTimeComparison(newReferencePoint);
        this.supportedPropertiesList = removeImmutableOptionalStackingProperties(newReferencePoint, this.getSupportedPropertiesList());
        newReferencePoint = getReferencePointWithSupportedProperties(newReferencePoint, this.supportedPropertiesList);
        return Promise.resolve(sanitizeFilters(newReferencePoint, this.featureFlags?.enableImprovedAdFilters, referencePoint));
    }
    getInsightWithDrillDownApplied(source, drillDownContext, backendSupportsElementUris) {
        const withFilters = this.addFilters(source, drillDownContext.drillDefinition, drillDownContext.event, backendSupportsElementUris);
        return modifyBucketsAttributesForDrillDown(withFilters, drillDownContext.drillDefinition);
    }
    getSortConfig(referencePoint) {
        const { defaultSort, availableSorts } = this.getDefaultAndAvailableSort(referencePoint);
        const { properties, availableSorts: previousAvailableSorts } = referencePoint;
        const { disabled, disabledExplanation } = this.isSortDisabled(referencePoint, availableSorts);
        return Promise.resolve({
            supported: true,
            disabled,
            appliedSort: super.reuseCurrentSort(previousAvailableSorts, properties, availableSorts, defaultSort),
            defaultSort,
            availableSorts,
            ...(disabledExplanation && { disabledExplanation }),
        });
    }
    updateInstanceProperties(options, insight, insightPropertiesMeta) {
        super.updateInstanceProperties(options, insight, insightPropertiesMeta);
        this.updateCustomSupportedProperties(insight);
    }
    configureBuckets(extendedReferencePoint) {
        const { measures, views, stacks } = this.getBucketItemsWithMultipleDates(extendedReferencePoint);
        set(extendedReferencePoint, BUCKETS, [
            {
                localIdentifier: BucketNames.MEASURES,
                items: measures,
            },
            {
                localIdentifier: BucketNames.VIEW,
                items: views,
            },
            {
                localIdentifier: BucketNames.STACK,
                items: stacks,
            },
        ]);
    }
    getSupportedPropertiesList() {
        return AREA_CHART_SUPPORTED_PROPERTIES;
    }
    renderConfigurationPanel(insight, options) {
        const configPanelElement = this.getConfigPanelElement();
        if (configPanelElement) {
            const panelConfig = {
                isContinuousLineControlDisabled: this.isContinuousLineControlDisabled(insight),
                isDistinctPointShapesDisabled: this.isDistinctPointShapesDisabled(),
                supportsAttributeHierarchies: this.backendCapabilities.supportsAttributeHierarchies,
                supportsChartFill: options.supportsChartFill,
            };
            this.renderFun(_jsx(LineChartBasedConfigurationPanel, { locale: this.locale, colors: this.colors, properties: this.visualizationProperties, propertiesMeta: this.propertiesMeta, insight: insight, references: this.references, pushData: this.handlePushData, type: this.type, isError: this.getIsError(), isLoading: this.isLoading, featureFlags: this.featureFlags, permissions: this.permissions, panelConfig: panelConfig, configurationPanelRenderers: options.custom?.configurationPanelRenderers }), configPanelElement);
        }
    }
    addFilters(source, drillConfig, event, backendSupportsElementUris) {
        const cutIntersection = reverseAndTrimIntersection(drillConfig, event.drillContext.intersection);
        return addIntersectionFiltersToInsight(source, cutIntersection, backendSupportsElementUris);
    }
    updateCustomSupportedProperties(insight) {
        if (bucketsItems(insightBuckets(insight, BucketNames.VIEW)).length > 1 ||
            !this.isContinuousLineControlDisabled(insight)) {
            this.addSupportedProperties(OPTIONAL_STACKING_PROPERTIES);
            this.setCustomControlsProperties({
                stackMeasures: false,
                stackMeasuresToPercent: false,
            });
        }
        else {
            this.setCustomControlsProperties({});
        }
    }
    addSupportedProperties(properties) {
        const supportedPropertiesList = this.supportedPropertiesList;
        (properties || []).forEach((property) => {
            if (!supportedPropertiesList.some((supportedProperty) => supportedProperty === property)) {
                supportedPropertiesList.push(property);
            }
        });
    }
    getViewByMaxItemCount(referencePoint) {
        return referencePoint.uiConfig?.buckets?.[BucketNames.VIEW]?.itemsLimit ?? MAX_CATEGORIES_COUNT;
    }
    getBucketMeasures(buckets = []) {
        const limitedBuckets = limitNumberOfMeasuresInBuckets(buckets, MAX_METRICS_COUNT, true);
        return getFilteredMeasuresForStackedCharts(limitedBuckets);
    }
    getBucketItemsWithMultipleDates(referencePoint) {
        const buckets = referencePoint?.buckets ?? [];
        const measures = this.getBucketMeasures(buckets);
        const viewByMaxItemCount = this.getViewByMaxItemCount(referencePoint);
        const stacks = getStackItems(buckets, [ATTRIBUTE, DATE]);
        const allAttributesWithoutStacks = getAllAttributeItemsWithPreference(buckets, [
            BucketNames.LOCATION,
            BucketNames.TREND,
            BucketNames.VIEW,
            BucketNames.ATTRIBUTES,
            BucketNames.SEGMENT,
            BucketNames.STACK,
        ]).filter((attribute) => !stacks.includes(attribute));
        const maxViews = stacks.length || measures.length > 1 ? 1 : viewByMaxItemCount;
        const views = allAttributesWithoutStacks.slice(0, maxViews);
        return {
            measures,
            views,
            stacks,
        };
    }
    getDefaultAndAvailableSort(referencePoint) {
        const { buckets, properties } = referencePoint;
        const measures = getBucketItems(buckets, BucketNames.MEASURES);
        const viewBy = getBucketItems(buckets, BucketNames.VIEW);
        const stackBy = getBucketItems(buckets, BucketNames.STACK);
        const canSortStackTotal = properties?.controls?.["stackMeasures"] ?? this.getUiConfig().optionalStacking?.stackMeasures;
        const defaultSort = viewBy.length > 0 ? [newAttributeSort(viewBy[0].localIdentifier, "asc")] : [];
        if (measures.length >= 2 && viewBy.length === 1 && !canSortStackTotal) {
            return {
                defaultSort,
                availableSorts: [
                    newAvailableSortsGroup(viewBy[0].localIdentifier, measures.map((m) => m.localIdentifier)),
                ],
            };
        }
        if (measures.length === 1 && isEmpty(stackBy)) {
            if (viewBy.length >= 2) {
                return {
                    defaultSort,
                    availableSorts: [newAvailableSortsGroup(viewBy[0].localIdentifier)],
                };
            }
            if (viewBy.length === 1) {
                return {
                    defaultSort,
                    availableSorts: [
                        newAvailableSortsGroup(viewBy[0].localIdentifier, measures.map((m) => m.localIdentifier), true, false),
                    ],
                };
            }
        }
        if (measures.length > 0 && viewBy.length === 1 && (stackBy.length === 1 || canSortStackTotal)) {
            return {
                defaultSort,
                availableSorts: [
                    newAvailableSortsGroup(viewBy[0].localIdentifier, isEmpty(stackBy) ? measures.map((m) => m.localIdentifier) : []),
                ],
            };
        }
        return {
            defaultSort: [],
            availableSorts: [],
        };
    }
    isSortDisabled(referencePoint, availableSorts) {
        const { buckets } = referencePoint;
        const measures = getBucketItems(buckets, BucketNames.MEASURES);
        const viewBy = getBucketItems(buckets, BucketNames.VIEW);
        const disabledExplanation = getCustomSortDisabledExplanation(measures, viewBy, this.intl);
        return {
            disabled: viewBy.length < 1 || measures.length < 1 || availableSorts.length === 0,
            disabledExplanation,
        };
    }
    isContinuousLineControlDisabled(insight) {
        const isStackingMeasures = this.visualizationProperties?.controls?.["stackMeasures"];
        if (typeof isStackingMeasures === "undefined") {
            const measuresBuckets = insightBucket(insight, BucketNames.MEASURES);
            const stackBuckets = insightBucket(insight, BucketNames.STACK);
            return (stackBuckets?.items?.length ?? 0) > 0 || (measuresBuckets?.items?.length ?? 0) > 1;
        }
        return isStackingMeasures;
    }
    isDistinctPointShapesDisabled() {
        const dataPointsVisible = this.visualizationProperties?.controls?.["dataPoints"]?.visible;
        if (typeof dataPointsVisible === "undefined") {
            return true;
        }
        return !!dataPointsVisible;
    }
}
//# sourceMappingURL=PluggableAreaChart.js.map