import { jsx as _jsx } from "react/jsx-runtime";
import { useIntl } from "react-intl";
import { DashboardParameterModeValues } from "@gooddata/sdk-model";
import { Dropdown, ParameterControlDropdown, UiChip, UiTooltip, isActionKey, useIdPrefixed, } from "@gooddata/sdk-ui-kit";
/**
 * Workspace parameter as an editable chip in automation dialogs.
 *
 * @internal
 */
export function AutomationParameter({ parameter, onChange, onDelete, overlayPositionType, isReadOnly, }) {
    const intl = useIntl();
    const { ref, title, value, mode, constraints } = parameter;
    const testId = `automation-parameter-${ref.identifier}`;
    const label = intl.formatMessage({ id: "dialogs.automation.parameters.chip" }, { title, value });
    const lockedTooltip = intl.formatMessage({ id: "dialogs.automation.filters.lockedTooltip" });
    const deleteAriaLabel = intl.formatMessage({ id: "dialogs.automation.filters.deleteAriaLabel" });
    const tooltipId = useIdPrefixed("automation-parameter-tooltip");
    if (isReadOnly) {
        // `isDisabled` renders a real disabled button — unfocusable, so keyboard users can't edit or
        // delete. No lock icon/tooltip: unlike author-`READONLY` this chip is only frozen because
        // persistence is off, so it must not read as author-locked.
        return (_jsx(UiChip, { label: label, iconBefore: "parameter", isDisabled: true, isExpandable: false, dataTestId: testId }));
    }
    if (mode === DashboardParameterModeValues.READONLY) {
        return (_jsx(UiChip, { label: label, iconBefore: "parameter", isLocked: true, isExpandable: false, dataTestId: testId, renderChipContent: (content) => (_jsx(UiTooltip, { id: tooltipId, arrowPlacement: "top-start", content: lockedTooltip, triggerBy: ["hover", "focus"], anchor: content, anchorWrapperStyles: { display: "flex", width: "100%", height: "100%", minWidth: 0 } })) }));
    }
    return (_jsx(Dropdown, { overlayPositionType: overlayPositionType, renderButton: ({ isOpen, toggleDropdown }) => (_jsx(UiChip, { label: label, iconBefore: "parameter", isActive: isOpen, isDeletable: true, onClick: () => toggleDropdown(), onDelete: () => onDelete?.(ref), dataTestId: testId, accessibilityConfig: {
                isExpanded: isOpen,
                deleteAriaLabel: `${deleteAriaLabel} ${title}`,
            }, onDeleteKeyDown: (event) => {
                if (isActionKey(event)) {
                    event.stopPropagation();
                }
            } })), renderBody: ({ closeDropdown }) => (_jsx(ParameterControlDropdown, { name: title, value: value, constraints: constraints, onApply: (next) => {
                onChange?.(ref, next);
                closeDropdown();
            }, onCancel: closeDropdown })) }));
}
