// (C) 2019-2026 GoodData Corporation
// eslint-disable-next-line no-restricted-imports
import { cloneDeep, get, has, isEmpty, set } from "lodash-es";
import { bucketsIsEmpty, insightBuckets } from "@gooddata/sdk-model";
import { BucketNames } from "@gooddata/sdk-ui";
import { getAllMeasuresShowOnSecondaryAxis, getItemsCount, getItemsLocalIdentifiers, getMeasureItems, getStackItems, } from "./bucketHelper.js";
import { AXIS } from "../constants/axis.js";
import { PROPERTY_CONTROLS } from "../constants/properties.js";
import { OPTIONAL_STACKING_PROPERTIES } from "../constants/supportedProperties.js";
import { UICONFIG_AXIS } from "../constants/uiConfig.js";
export function getSupportedPropertiesControls(visualizationControlsProperties, supportedPropertiesList) {
    const clonedControls = cloneDeep(visualizationControlsProperties);
    if (supportedPropertiesList) {
        return supportedPropertiesList.reduce((props, current) => has(clonedControls, current) ? set(props, current, get(clonedControls, current)) : props, {});
    }
    return {};
}
export function hasColorMapping(properties) {
    return !!properties?.controls?.["colorMapping"];
}
export function setSecondaryMeasures(referencePoint, axisName) {
    if (!axisName) {
        return referencePoint;
    }
    const newReferencePoint = cloneDeep(referencePoint);
    const path = `${PROPERTY_CONTROLS}.${axisName}`;
    const secondaryAxisProperties = newReferencePoint?.properties?.controls?.[axisName];
    const buckets = newReferencePoint?.buckets ?? [];
    const allMeasures = getMeasureItems(buckets);
    const secondaryMeasures = getAllMeasuresShowOnSecondaryAxis(buckets);
    if (!secondaryAxisProperties && !secondaryMeasures.length) {
        return referencePoint;
    }
    const secondaryAxis = {
        ...secondaryAxisProperties,
        measures: getItemsLocalIdentifiers(secondaryMeasures),
    };
    const axis = {
        0: AXIS.PRIMARY,
        [allMeasures.length]: AXIS.SECONDARY,
    }[secondaryMeasures.length] || AXIS.DUAL;
    set(newReferencePoint, path, secondaryAxis);
    set(newReferencePoint, UICONFIG_AXIS, axis);
    return newReferencePoint;
}
export function isEmptyObject(obj) {
    return obj && Object.keys(obj).length === 0;
}
export function getSupportedProperties(visualizationProperties, supportedPropertiesList) {
    const controls = visualizationProperties?.controls ?? {};
    const supportedControls = getSupportedPropertiesControls(controls, supportedPropertiesList);
    return isEmpty(supportedControls)
        ? {}
        : {
            controls: supportedControls,
        };
}
export function getReferencePointWithSupportedProperties(referencePoint, supportedPropertiesList) {
    const supportedControlsProperties = referencePoint.properties
        ? getSupportedPropertiesControls(referencePoint.properties.controls, supportedPropertiesList)
        : {};
    if (isEmpty(supportedControlsProperties)) {
        const sortItems = referencePoint.properties?.sortItems;
        const sortItemsExpand = sortItems && !isEmpty(sortItems) ? { sortItems } : {};
        return {
            ...referencePoint,
            properties: {
                ...sortItemsExpand,
            },
        };
    }
    const buckets = referencePoint?.buckets ?? [];
    const stackCount = getItemsCount(buckets, BucketNames.STACK);
    const stackMeasuresToPercent = Boolean(supportedControlsProperties["stackMeasuresToPercent"]);
    if (!stackCount && stackMeasuresToPercent) {
        supportedControlsProperties["stackMeasures"] = true;
    }
    return {
        ...referencePoint,
        properties: {
            ...referencePoint.properties,
            controls: supportedControlsProperties,
        },
    };
}
export function getReferencePointWithTotalLabelsInitialized(referencePoint) {
    const properties = referencePoint.properties;
    const dataLabelVisibility = properties?.controls?.["dataLabels"]?.visible;
    const stacks = getStackItems(referencePoint.buckets);
    // Initialize total labels visibility with data labels visibility value.
    // Initialize if data labels visibility is defined and total labels visibility
    // is not defined and if current chart configuration allows configuring total labels.
    if (!(dataLabelVisibility === null || dataLabelVisibility === undefined) &&
        (properties?.controls?.["dataLabels"]?.totalsVisible === null ||
            properties?.controls?.["dataLabels"]?.totalsVisible === undefined) &&
        !isStackingToPercent(properties ?? {}) &&
        (stacks.length || isStackingMeasure(properties ?? {}))) {
        return {
            ...referencePoint,
            properties: {
                ...properties,
                controls: {
                    ...properties?.controls,
                    dataLabels: {
                        ...properties?.controls?.["dataLabels"],
                        totalsVisible: dataLabelVisibility,
                    },
                },
            },
        };
    }
    return referencePoint;
}
export function isStackingMeasure(properties) {
    return properties?.controls?.["stackMeasures"] ?? false;
}
export function isStackingToPercent(properties) {
    return properties?.controls?.["stackMeasuresToPercent"] ?? false;
}
export function isTotalSectionEnabled(properties) {
    return properties?.controls?.["total"]?.enabled ?? true;
}
export function isDualAxisOrSomeSecondaryAxisMeasure(extReferencePoint, secondaryMeasures) {
    return ((extReferencePoint?.properties?.controls?.["dualAxis"] ?? true) ||
        secondaryMeasures.some((item) => item?.showOnSecondaryAxis));
}
export function removeImmutableOptionalStackingProperties(referencePoint, supportedPropertiesList) {
    const buckets = referencePoint?.buckets ?? [];
    let immutableProperties = [];
    if (getItemsCount(buckets, BucketNames.MEASURES) <= 1) {
        immutableProperties = OPTIONAL_STACKING_PROPERTIES;
    }
    if (getItemsCount(buckets, BucketNames.STACK)) {
        immutableProperties = OPTIONAL_STACKING_PROPERTIES.slice(0, 1);
    }
    return supportedPropertiesList.filter((property) => !immutableProperties.some((immutableProperty) => immutableProperty === property));
}
// mapping between AD and SDK values
const AXIS_NAME_POSITION_MAPPING = {
    auto: "middle",
    bottom: "low",
    middle: "middle",
    top: "high",
    left: "low",
    center: "middle",
    right: "high",
};
const AXIS_TYPES = ["xaxis", "yaxis", "secondary_xaxis", "secondary_yaxis"];
export function getHighchartsAxisNameConfiguration(controlProperties) {
    const axisProperties = AXIS_TYPES.reduce((result, axis) => {
        const axisNameConfig = controlProperties?.[axis]?.name;
        if (isEmpty(axisNameConfig)) {
            return result;
        }
        if (axisNameConfig.position) {
            axisNameConfig.position = AXIS_NAME_POSITION_MAPPING[axisNameConfig.position];
        }
        result[axis] = {
            ...controlProperties[axis],
            name: axisNameConfig,
        };
        return result;
    }, {});
    return {
        ...controlProperties,
        ...axisProperties,
    };
}
export function getDataPointsConfiguration(controlProperties) {
    const dataPointsVisible = controlProperties["dataPoints"]?.visible;
    return {
        ...controlProperties,
        dataPoints: {
            visible: dataPointsVisible === undefined ? "auto" : dataPointsVisible,
        },
    };
}
export function getColumnWidthsFromProperties(visualizationProperties) {
    return visualizationProperties?.controls?.["columnWidths"];
}
export function getTextWrappingFromProperties(visualizationProperties) {
    return visualizationProperties?.controls?.["textWrapping"];
}
export function getGrandTotalsPositionFromProperties(visualizationProperties) {
    return visualizationProperties?.controls?.["grandTotalsPosition"];
}
export function getPaginationFromProperties(visualizationProperties) {
    return visualizationProperties?.controls?.["pagination"];
}
export function getPageSizeFromProperties(visualizationProperties) {
    const pageSize = visualizationProperties?.controls?.["pageSize"];
    return typeof pageSize === "number" ? pageSize : undefined;
}
export function getMeasureGroupDimensionFromProperties(visualizationProperties) {
    return visualizationProperties?.controls?.["measureGroupDimension"];
}
export function getColumnHeadersPositionFromProperties(visualizationProperties) {
    return visualizationProperties?.controls?.["columnHeadersPosition"];
}
export function getLegendConfiguration(controlProperties, insight) {
    const legendPosition = getLegendPosition(controlProperties, insight);
    set(controlProperties, "legend.position", legendPosition);
    return controlProperties;
}
export function getLegendConfigurationDashboardsEnv(controlProperties, options) {
    const legendPosition = getLegendPositionDashboardsEnv(controlProperties, options);
    set(controlProperties, "legend.position", legendPosition);
    set(controlProperties, "legend.responsive", "autoPositionWithPopup");
    return controlProperties;
}
export function getChartSupportedControls(controlProperties, insight) {
    return getDataPointsConfiguration(getHighchartsAxisNameConfiguration(getLegendConfiguration(cloneDeep(controlProperties ?? {}), insight)));
}
export function getChartSupportedControlsDashboardsEnv(controlProperties, options) {
    return getDataPointsConfiguration(getHighchartsAxisNameConfiguration(getLegendConfigurationDashboardsEnv(cloneDeep(controlProperties ?? {}), options)));
}
function getLegendPosition(controlProperties, insight) {
    const legendPosition = controlProperties?.["legend"]?.position ?? "auto";
    return legendPosition === "auto" && isStacked(insight) ? "right" : legendPosition;
}
function getLegendPositionDashboardsEnv(controlProperties, options) {
    const legendPosition = controlProperties?.["legend"]?.position ?? "auto";
    const width = options.dimensions?.width;
    return width !== undefined && width <= getMaxWidthForCollapsedLegend(legendPosition)
        ? "top"
        : legendPosition;
}
function isStacked(insight) {
    return !bucketsIsEmpty(insightBuckets(insight, BucketNames.STACK, BucketNames.SEGMENT));
}
const MAX_WIDTH_FOR_COLLAPSED_LEGEND = 440;
const MAX_WIDTH_FOR_COLLAPSED_AUTO_LEGEND = 610;
function getMaxWidthForCollapsedLegend(legendPosition) {
    return legendPosition === "auto" ? MAX_WIDTH_FOR_COLLAPSED_AUTO_LEGEND : MAX_WIDTH_FOR_COLLAPSED_LEGEND;
}
export function getPivotTableProperties(settings, properties) {
    const enableNewPivotTable = settings.enableNewPivotTable ?? true;
    const textWrapping = enableNewPivotTable ? getTextWrappingFromProperties(properties) : undefined;
    const grandTotalsPosition = enableNewPivotTable
        ? getGrandTotalsPositionFromProperties(properties)
        : undefined;
    return {
        measureGroupDimension: getMeasureGroupDimensionFromProperties(properties),
        columnHeadersPosition: getColumnHeadersPositionFromProperties(properties),
        ...(textWrapping ? { textWrapping } : {}),
        ...(grandTotalsPosition ? { grandTotalsPosition } : {}),
    };
}
//# sourceMappingURL=propertiesHelper.js.map