import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2007-2026 GoodData Corporation
import { forwardRef, useCallback, useMemo } from "react";
import cx from "classnames";
import { ValidationContextStore } from "@gooddata/sdk-ui";
import { simplifyText } from "@gooddata/util";
const getGeneratedTestId = (effectiveValue, title, ariaLabel) => {
    if (effectiveValue && typeof effectiveValue === "string") {
        return `${simplifyText(effectiveValue)}`;
    }
    return ariaLabel ? `${simplifyText(ariaLabel)}` : `${simplifyText(title)}`;
};
function Icon({ icon }) {
    if (!icon) {
        return null;
    }
    return _jsx("span", { className: cx("gd-button-icon", icon), "data-testid": "gd-button-icon", "aria-hidden": "true" });
}
/**
 * @internal
 */
export const Button = forwardRef(function Button({ className, describedByFromValidation: isDescribedByFromValidation = false, disabled = false, onClick = () => { }, tabIndex = 0, tagName = "button", type = "button", id, title, iconLeft, iconRight, accessibilityConfig, dataId, dataTestId, intent, size, variant, value, children, }, ref) {
    const { isExpanded, popupId, ariaLabel, ariaLabelledBy, ariaDescribedBy: ariaDescribedByFromConfig, popupType, ariaHaspopup, role = "button", ariaControls, ariaExpanded, ariaPressed, } = accessibilityConfig ?? {};
    const ariaDropdownProps = {
        ...(popupId && isExpanded ? { "aria-controls": popupId } : {}),
        ...(ariaHaspopup ? { "aria-haspopup": ariaHaspopup } : {}),
        ...(popupId ? { "aria-haspopup": popupType ?? !!popupId } : {}),
        ...(isExpanded === undefined ? {} : { "aria-expanded": isExpanded }),
    };
    const ariaDropdownAttributesFromConfig = {
        ...ariaDropdownProps,
        // override calculated dropdown props by explicit overrides from props
        "aria-controls": ariaControls ?? undefined,
        "aria-expanded": ariaExpanded ?? undefined,
    };
    const handleClick = useCallback((e) => {
        if (disabled) {
            return;
        }
        onClick(e);
    }, [disabled, onClick]);
    const describedByFromValidation = ValidationContextStore.useContextStoreOptional((ctx) => ctx
        .getInvalidDatapoints({ recursive: true })
        .map((datapoint) => datapoint.id)
        .join(" "));
    const ariaDescribedBy = isDescribedByFromValidation
        ? [describedByFromValidation, ariaDescribedByFromConfig].filter(Boolean).join(" ") || undefined
        : ariaDescribedByFromConfig;
    const effectiveValue = useMemo(() => value ?? children, [children, value]);
    const testId = dataTestId || getGeneratedTestId(effectiveValue, title ?? "", ariaLabel ?? "");
    const classNames = useMemo(() => {
        const generatedSeleniumClass = effectiveValue && typeof effectiveValue === "string" ? `s-${simplifyText(effectiveValue)}` : "";
        return cx([
            "gd-button",
            generatedSeleniumClass,
            className,
            {
                [`gd-button-${variant}`]: !!variant,
                [`gd-button-${size}`]: !!size,
                [`gd-button-${intent}`]: !!intent,
                disabled: disabled,
            },
        ]);
    }, [className, disabled, effectiveValue, intent, size, variant]);
    const TagName = tagName;
    return (_jsxs(TagName, { id: id, "data-id": dataId, "data-testid": testId, ref: ref, title: title, className: classNames, type: type, onClick: handleClick, "aria-disabled": disabled, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledBy, "aria-describedby": ariaDescribedBy, "aria-pressed": ariaPressed, ...ariaDropdownAttributesFromConfig, ...(tagName === "button" ? {} : { tabIndex }), ...(tagName !== "button" || role !== "button" ? { role } : {}), children: [
            _jsx(Icon, { icon: iconLeft }), effectiveValue ? _jsx("span", { className: "gd-button-text", children: effectiveValue }) : null, _jsx(Icon, { icon: iconRight })
        ] }));
});
