// (C) 2019-2025 GoodData Corporation
import { cloneDeep, set } from "lodash-es";
import { BucketNames, VisualizationTypes } from "@gooddata/sdk-ui";
import { BUCKETS } from "../../constants/bucket.js";
import { DEFAULT_XIRR_UICONFIG } from "../../constants/uiConfig.js";
import { getItemsCount, setBucketTitles } from "../bucketHelper.js";
import { hasNoAttribute, hasNoMeasures } from "../bucketRules.js";
export const getDefaultXirrUiConfig = () => cloneDeep(DEFAULT_XIRR_UICONFIG);
function getCustomError({ buckets }, formatMessage) {
    const measuresCount = getItemsCount(buckets, BucketNames.MEASURES);
    const attributeCount = getItemsCount(buckets, BucketNames.ATTRIBUTE);
    if (measuresCount === 0 || attributeCount === 0) {
        return {
            heading: formatMessage({ id: "dashboard.xirr.error.invalid_buckets.heading" }),
            text: formatMessage({ id: "dashboard.xirr.error.invalid_buckets.text" }),
        };
    }
    return undefined;
}
export const getXirrUiConfig = (referencePoint, intl) => {
    const uiConfig = setBucketTitles({
        ...referencePoint,
        uiConfig: getDefaultXirrUiConfig(),
    }, VisualizationTypes.XIRR, intl);
    const buckets = referencePoint?.buckets ?? [];
    const canAddMeasures = hasNoMeasures(buckets);
    const canAddAttribute = hasNoAttribute(buckets);
    set(uiConfig, [BUCKETS, BucketNames.MEASURES, "canAddItems"], canAddMeasures);
    set(uiConfig, [BUCKETS, BucketNames.ATTRIBUTE, "canAddItems"], canAddAttribute);
    uiConfig.customError = getCustomError(referencePoint, intl.formatMessage);
    return uiConfig;
};
//# sourceMappingURL=xirrUiConfigHelper.js.map