// (C) 2024-2025 GoodData Corporation
import { cloneDeep, set } from "lodash-es";
import { areObjRefsEqual } from "@gooddata/sdk-model";
import { BucketNames, VisualizationTypes } from "@gooddata/sdk-ui";
import { ATTRIBUTE, BUCKETS, DATE } from "../../constants/bucket.js";
import { DEFAULT_REPEATER_UI_CONFIG, UICONFIG } from "../../constants/uiConfig.js";
import { getBucketItems, getMainRowAttribute, setBucketTitles } from "../bucketHelper.js";
import { hasNoAttribute, hasNoColumns } from "../bucketRules.js";
export const getDefaultRepeaterUiConfig = () => cloneDeep(DEFAULT_REPEATER_UI_CONFIG);
// 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 repeaterRowsIcon = "local:repeater/bucket-title-rows.svg";
const repeaterColumnsIcon = "local:repeater/bucket-title-columns.svg";
const repeaterViewIcon = "local:repeater/bucket-title-view.svg";
export function setRepeaterUiConfig(referencePoint, intl) {
    const referencePointConfigured = cloneDeep(referencePoint);
    const buckets = referencePointConfigured?.buckets ?? [];
    const attributeCanAddItems = hasNoAttribute(buckets);
    const emptyColumns = hasNoColumns(buckets);
    const columnsCanAddItems = !attributeCanAddItems || !emptyColumns;
    const viewCanAddItems = true; // !attributeCanAddItems || !emptyColumns;
    set(referencePointConfigured, UICONFIG, setBucketTitles(referencePoint, VisualizationTypes.REPEATER, intl));
    set(referencePointConfigured, [UICONFIG, BUCKETS, BucketNames.ATTRIBUTE, "canAddItems"], attributeCanAddItems);
    set(referencePointConfigured, [UICONFIG, BUCKETS, BucketNames.COLUMNS, "canAddItems"], columnsCanAddItems);
    set(referencePointConfigured, [UICONFIG, BUCKETS, BucketNames.VIEW, "canAddItems"], viewCanAddItems);
    set(referencePointConfigured, [UICONFIG, BUCKETS, BucketNames.ATTRIBUTE, "icon"], repeaterRowsIcon);
    set(referencePointConfigured, [UICONFIG, BUCKETS, BucketNames.COLUMNS, "icon"], repeaterColumnsIcon);
    set(referencePointConfigured, [UICONFIG, BUCKETS, BucketNames.VIEW, "icon"], repeaterViewIcon);
    return referencePointConfigured;
}
export const configRepeaterBuckets = (extendedReferencePoint) => {
    const newExtendedReferencePoint = cloneDeep(extendedReferencePoint);
    const { attribute, columns, view } = getRepeaterBucketItems(newExtendedReferencePoint);
    set(newExtendedReferencePoint, BUCKETS, [
        {
            localIdentifier: BucketNames.ATTRIBUTE,
            items: attribute,
        },
        {
            localIdentifier: BucketNames.COLUMNS,
            items: columns,
        },
        {
            localIdentifier: BucketNames.VIEW,
            items: view,
        },
    ]);
    return newExtendedReferencePoint;
};
const getRepeaterBucketItems = (extendedReferencePoint) => {
    const newExtendedReferencePoint = cloneDeep(extendedReferencePoint);
    const buckets = newExtendedReferencePoint?.buckets ?? [];
    const rowAttribute = getMainRowAttribute(buckets);
    const columns = getBucketItems(buckets, BucketNames.COLUMNS);
    const [view] = getBucketItems(buckets, BucketNames.VIEW);
    const validColumns = columns
        .filter((column) => column.type !== DATE)
        .filter((column) => {
        if (rowAttribute && column.type === ATTRIBUTE) {
            const isSameLabel = areObjRefsEqual(column.dfRef, rowAttribute.dfRef);
            const isSameAttributeButDifferentLabel = column.displayForms?.some((df) => areObjRefsEqual(df.ref, rowAttribute.dfRef));
            return isSameLabel || isSameAttributeButDifferentLabel;
        }
        return true;
    });
    return {
        attribute: rowAttribute ? [rowAttribute] : [],
        columns: validColumns.length ? validColumns : [],
        view: view ? [view] : [],
    };
};
//# sourceMappingURL=repeaterUiConfigHelper.js.map