import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2019-2025 GoodData Corporation
import { memo } from "react";
import { cloneDeep, set } from "lodash-es";
import { injectIntl } from "react-intl";
import { Dropdown, DropdownButton, DropdownList, SingleSelectListItem, } from "@gooddata/sdk-ui-kit";
import { getTranslation } from "../../utils/translations.js";
import { DisabledBubbleMessage } from "../DisabledBubbleMessage.js";
const alignPoints = ["bl tl", "tl bl", "br tr", "tr br"];
const DROPDOWN_ALIGNMENTS = alignPoints.map((align) => ({ align, offset: { x: 1, y: 0 } }));
export const DropdownControl = injectIntl(memo(function DropdownControl({ valuePath, properties, labelText, value = "", items = [], disabled = false, width = 117, showDisabledMessage = false, disabledMessageAlignPoints, customListItem: ListItem = SingleSelectListItem, pushData, intl, }) {
    const getSelectedItem = (value) => {
        if (items) {
            return items.find((item) => item.value === value);
        }
        return undefined;
    };
    const onSelect = (selectedItem) => {
        // we must not change the properties at any cost, so deep clone for now.
        // ideally we should use st. like immer with copy on write to not clone everything all the time
        const clonedProperties = cloneDeep(properties);
        set(clonedProperties, `controls.${valuePath}`, selectedItem.value);
        pushData?.({ properties: clonedProperties });
    };
    const getDropdownButton = (selectedItem, disabled, isOpen, toggleDropdown) => {
        const { icon, title } = selectedItem;
        return (_jsx(DropdownButton, { value: title, iconLeft: icon, isOpen: isOpen, onClick: toggleDropdown, disabled: disabled }));
    };
    const selectedItem = getSelectedItem(value) || {};
    return (_jsx(DisabledBubbleMessage, { showDisabledMessage: showDisabledMessage, alignPoints: disabledMessageAlignPoints, children: _jsxs("div", { className: "adi-properties-dropdown-container", children: [_jsx("span", { className: "input-label-text", children: getTranslation(labelText, intl) }), _jsx("label", { className: "adi-bucket-inputfield gd-input gd-input-small", children: _jsx(Dropdown, { renderButton: ({ isOpen, toggleDropdown }) => {
                            return getDropdownButton(selectedItem, disabled, isOpen, toggleDropdown);
                        }, closeOnParentScroll: true, closeOnMouseDrag: true, alignPoints: DROPDOWN_ALIGNMENTS, renderBody: ({ closeDropdown, isMobile }) => {
                            return (_jsx(DropdownList, { width: width, isMobile: isMobile, items: items, renderItem: ({ item }) => (_jsx(ListItem, { title: item.title, isSelected: item.value === selectedItem.value, onClick: () => {
                                        onSelect(item);
                                        closeDropdown();
                                    }, type: item.type, icon: item.icon, info: item.info })) }));
                        }, className: "adi-bucket-dropdown" }) })] }) }));
}));
//# sourceMappingURL=DropdownControl.js.map