import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2007-2026 GoodData Corporation
import { useRef } from "react";
import cx from "classnames";
import { useIntl } from "react-intl";
import { useScopedIdOptional } from "../../@ui/hooks/useScopedId.js";
import { isEnterKey, isSpaceKey } from "../../utils/events.js";
/**
 * @internal
 */
export function InvertableSelectAllCheckbox({ isVisible, checked, onToggle, onApplyButtonClick, isApplyDisabled, isFiltered, totalItemsCount, isPartialSelection, hideTotalItemsCount = false, }) {
    const intl = useIntl();
    const itemRef = useRef(null);
    const onClick = () => {
        onToggle();
    };
    const onKeyDown = (event) => {
        event.preventDefault(); // Prevent scrolling
        if (isSpaceKey(event)) {
            onToggle();
        }
        if (isEnterKey(event) && !isApplyDisabled) {
            onApplyButtonClick?.();
        }
    };
    const wrapperClasses = cx("gd-invertable-select-all-checkbox-wrapper", "input-checkbox-label");
    const containerId = useScopedIdOptional();
    const ariaLabel = isFiltered
        ? intl.formatMessage({ id: "gs.list.allSearchResultsAndCount" }, { count: totalItemsCount })
        : intl.formatMessage({ id: "gs.list.allAndCount" }, { count: totalItemsCount });
    if (!isVisible) {
        return null;
    }
    return (_jsx("div", { className: "gd-invertable-select-all-checkbox s-select-all-checkbox", ref: itemRef, tabIndex: 0, onClick: onClick, onKeyDown: onKeyDown, role: "checkbox", "aria-checked": isPartialSelection ? "mixed" : checked, "aria-controls": containerId, "aria-label": ariaLabel, children: _jsx("span", { className: wrapperClasses, children: _jsxs("span", { className: "input-label-text", children: [
                    _jsxs("span", { className: cx("gd-list-all-checkbox", {
                            "gd-list-all-checkbox-checked": checked,
                        }), children: [intl.formatMessage({ id: "gs.list.all" }), isFiltered
                                ? ` ${intl.formatMessage({
                                    id: "gs.list.searchResults",
                                })}`
                                : null] }), hideTotalItemsCount ? null : (_jsx("span", { className: "gd-list-actions-selection-size s-list-search-selection-size", children: `(${totalItemsCount})` }))] }) }) }));
}
