// (C) 2019-2026 GoodData Corporation
import { cloneDeep, set } from "lodash-es";
import { BucketNames } from "@gooddata/sdk-ui";
import { getBucketItemsWarningMessage } from "./baseChartUiConfigHelper.js";
import { messages } from "../../../locales.js";
import { ATTRIBUTE, BUCKETS, DATE } from "../../constants/bucket.js";
import { OPEN_AS_REPORT, SUPPORTED, UICONFIG } from "../../constants/uiConfig.js";
import { getStackItems, setBucketTitles } from "../bucketHelper.js";
import { hasNoMeasures, hasNoStacksWithDate, hasOneMeasureOrAlsoLineStyleControlMeasure, hasSomeSegmentByItems, } 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 lineMeasuresIcon = "local:line/bucket-title-measures.svg";
const lineTrendIcon = "local:line/bucket-title-trend.svg";
const lineSegmentIcon = "local:line/bucket-title-segment.svg";
function setLineChartBucketWarningMessages(referencePoint, intl) {
    const buckets = referencePoint?.buckets || [];
    const updatedUiConfig = cloneDeep(referencePoint?.uiConfig);
    const stackItems = getStackItems(buckets, [ATTRIBUTE, DATE]);
    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 warningMessage;
            if (bucket.localIdentifier === BucketNames.MEASURES && intl) {
                warningMessage = getBucketItemsWarningMessage(messages["metricSegment"].id, intl, stackItems);
            }
            else if (bucket.localIdentifier === BucketNames.SEGMENT && intl) {
                warningMessage = getTranslation(messages["categorySegment"].id, intl);
            }
            if (warningMessage) {
                set(updatedUiConfig, [BUCKETS, localIdentifier, "warningMessage"], warningMessage);
            }
        }
    });
    return updatedUiConfig;
}
export function setLineChartUiConfig(referencePoint, intl, visualizationType) {
    const referencePointConfigured = cloneDeep(referencePoint);
    const buckets = referencePointConfigured?.buckets ?? [];
    const measuresCanAddItems = hasNoMeasures(buckets) || hasNoStacksWithDate(buckets);
    const segmentCanAddItems = hasSomeSegmentByItems(buckets) ||
        hasNoMeasures(buckets) ||
        hasOneMeasureOrAlsoLineStyleControlMeasure(buckets);
    set(referencePointConfigured, UICONFIG, setBucketTitles(referencePoint, visualizationType, intl));
    set(referencePointConfigured, [UICONFIG, BUCKETS, BucketNames.MEASURES, "canAddItems"], measuresCanAddItems);
    set(referencePointConfigured, [UICONFIG, BUCKETS, BucketNames.TREND, "canAddItems"], true);
    set(referencePointConfigured, [UICONFIG, BUCKETS, BucketNames.SEGMENT, "canAddItems"], segmentCanAddItems);
    set(referencePointConfigured, UICONFIG, setLineChartBucketWarningMessages(referencePointConfigured, intl));
    set(referencePointConfigured, [UICONFIG, OPEN_AS_REPORT, SUPPORTED], !hasColorMapping(referencePoint.properties));
    set(referencePointConfigured, [UICONFIG, BUCKETS, BucketNames.MEASURES, "icon"], lineMeasuresIcon);
    set(referencePointConfigured, [UICONFIG, BUCKETS, BucketNames.TREND, "icon"], lineTrendIcon);
    set(referencePointConfigured, [UICONFIG, BUCKETS, BucketNames.SEGMENT, "icon"], lineSegmentIcon);
    return referencePointConfigured;
}
//# sourceMappingURL=lineChartUiConfigHelper.js.map