import { jsx as _jsx } from "react/jsx-runtime";
import { DashboardParameterModeValues, isNumberParameterDefinition, objRefToString, } from "@gooddata/sdk-model";
import { Dropdown, ParameterControlButton, ParameterControlDropdown } from "@gooddata/sdk-ui-kit";
import { useDashboardDispatch, useDashboardSelector } from "../../../model/react/DashboardStoreProvider.js";
import { selectCatalogParameterByRef } from "../../../model/store/catalog/catalogSelectors.js";
import { parametersActions } from "../../../model/store/parameters/index.js";
import { selectParameterRuntimeOverrideByRef } from "../../../model/store/parameters/parametersSelectors.js";
import { selectIsInEditMode } from "../../../model/store/renderMode/renderModeSelectors.js";
import { DraggableChipSource } from "../../dragAndDrop/DraggableChipSource.js";
/**
 * Renders a chip for a single dashboard parameter.
 *
 * @internal
 */
export function DashboardParameterFilter({ parameter }) {
    const dispatch = useDashboardDispatch();
    const workspaceParameter = useDashboardSelector(selectCatalogParameterByRef(parameter.ref));
    const runtimeOverride = useDashboardSelector(selectParameterRuntimeOverrideByRef(parameter.ref));
    const isInEditMode = useDashboardSelector(selectIsInEditMode);
    if (parameter.mode === DashboardParameterModeValues.HIDDEN || runtimeOverride === undefined) {
        return null;
    }
    if (!workspaceParameter || !isNumberParameterDefinition(workspaceParameter.definition)) {
        return null;
    }
    const name = parameter.label ?? workspaceParameter.title;
    const { constraints } = workspaceParameter.definition;
    const dragItem = { type: "parameter", ref: parameter.ref };
    if (parameter.mode === DashboardParameterModeValues.READONLY) {
        return (_jsx(DraggableChipSource, { dragItem: dragItem, canDrag: isInEditMode, children: _jsx(ParameterControlButton, { name: name, value: runtimeOverride, isActive: false, isDraggable: isInEditMode, "data-testid": `dashboard-parameter-${objRefToString(parameter.ref)}` }) }));
    }
    return (_jsx(DraggableChipSource, { dragItem: dragItem, canDrag: isInEditMode, children: _jsx(Dropdown, { renderButton: ({ isOpen, toggleDropdown }) => (_jsx(ParameterControlButton, { name: name, value: runtimeOverride, isActive: isOpen, isDraggable: isInEditMode, onClick: () => toggleDropdown(), "data-testid": `dashboard-parameter-${objRefToString(parameter.ref)}` })), renderBody: ({ closeDropdown }) => (_jsx(ParameterControlDropdown, { name: name, value: runtimeOverride, constraints: constraints, onApply: (value) => {
                    dispatch(parametersActions.setParameterRuntimeValue({ ref: parameter.ref, value }));
                    closeDropdown();
                }, onCancel: closeDropdown })) }) }));
}
//# sourceMappingURL=DashboardParameterFilter.js.map