// (C) 2019-2025 GoodData Corporation
import { cloneDeep, set } from "lodash-es";
import { BucketNames } from "@gooddata/sdk-ui";
import { messages } from "../../../locales.js";
import { BUCKETS } from "../../constants/bucket.js";
import { OPEN_AS_REPORT, SUPPORTED, UICONFIG } from "../../constants/uiConfig.js";
import { setBucketTitles } from "../bucketHelper.js";
import { hasMoreThanOneCategory, hasMoreThanOneMasterMeasure } from "../bucketRules.js";
import { hasColorMapping } from "../propertiesHelper.js";
import { getTranslation } from "../translations.js";
// If you need to edit these icons
// reflect changes also in gdc-analytical-designer
// https://github.com/gooddata/gdc-analytical-designer/blob/develop/app/components/buckets/BucketIcon.tsx
const pieMeasuresIcon = "local:pie/bucket-title-measures.svg";
const pieViewIcon = "local:pie/bucket-title-view.svg";
function setPieChartBucketWarningMessages(referencePoint, intl) {
    const buckets = referencePoint?.buckets;
    const updatedUiConfig = cloneDeep(referencePoint?.uiConfig);
    if (!updatedUiConfig) {
        return updatedUiConfig;
    }
    buckets.forEach((bucket) => {
        const localIdentifier = bucket?.localIdentifier ?? "";
        const bucketUiConfig = updatedUiConfig?.buckets?.[localIdentifier];
        // skip disabled buckets
        if (!bucketUiConfig?.enabled) {
            return;
        }
        if (!bucketUiConfig?.canAddItems) {
            let warningMessageId;
            if (bucket.localIdentifier === BucketNames.VIEW) {
                warningMessageId = messages["category"].id;
            }
            if (warningMessageId) {
                const warningMessage = getTranslation(warningMessageId, intl);
                set(updatedUiConfig, [BUCKETS, localIdentifier, "warningMessage"], warningMessage);
            }
        }
    });
    return updatedUiConfig;
}
export function setPieChartUiConfig(referencePoint, intl, visualizationType) {
    const referencePointConfigured = cloneDeep(referencePoint);
    const buckets = referencePointConfigured?.buckets ?? [];
    const measuresCanAddItems = !hasMoreThanOneCategory(buckets);
    const viewCanAddItems = !hasMoreThanOneMasterMeasure(buckets, BucketNames.MEASURES);
    set(referencePointConfigured, UICONFIG, setBucketTitles(referencePoint, visualizationType, intl));
    set(referencePointConfigured, [UICONFIG, BUCKETS, BucketNames.MEASURES, "canAddItems"], measuresCanAddItems);
    set(referencePointConfigured, [UICONFIG, BUCKETS, BucketNames.VIEW, "canAddItems"], viewCanAddItems);
    set(referencePointConfigured, UICONFIG, setPieChartBucketWarningMessages(referencePointConfigured, intl));
    set(referencePointConfigured, [UICONFIG, OPEN_AS_REPORT, SUPPORTED], !hasColorMapping(referencePoint.properties));
    set(referencePointConfigured, [UICONFIG, BUCKETS, BucketNames.MEASURES, "icon"], pieMeasuresIcon);
    set(referencePointConfigured, [UICONFIG, BUCKETS, BucketNames.VIEW, "icon"], pieViewIcon);
    return referencePointConfigured;
}
//# sourceMappingURL=pieChartUiConfigHelper.js.map