import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2007-2025 GoodData Corporation
import { useCallback, useRef } from "react";
import cx from "classnames";
import { useIntl } from "react-intl";
import { ListWithActionsFocusStore } from "../../@ui/hooks/useListWithActionsFocus.js";
import { isEnterKey, isSpaceKey } from "../../utils/events.js";
/**
 * @internal
 */
export function InvertableSelectAllCheckbox(props) {
    const { isVisible, checked, onToggle, onApplyButtonClick, isApplyDisabled, isFiltered, totalItemsCount, isPartialSelection, } = props;
    const intl = useIntl();
    const itemRef = useRef(null);
    const handleToggle = useCallback((_e) => {
        onToggle();
    }, [onToggle]);
    const onFocus = (event) => {
        // Prevent focus from moving from item inside to the checkbox
        if (event.target.tagName === "INPUT") {
            event.preventDefault();
            itemRef.current?.focus(); // Keep focus on the item
        }
    };
    const onKeyDown = (event) => {
        event.preventDefault(); // Prevent scrolling
        if (isSpaceKey(event)) {
            onToggle();
        }
        if (isEnterKey(event) && !isApplyDisabled) {
            onApplyButtonClick();
        }
    };
    const checkboxClasses = cx("input-checkbox", "gd-checkbox-selection", {
        "checkbox-indefinite": isPartialSelection,
    });
    const labelClasses = cx("input-checkbox-label", "s-select-all-checkbox");
    const controlsId = ListWithActionsFocusStore.useContextStoreOptional((ctx) => ctx.containerId);
    if (!isVisible) {
        return null;
    }
    return (_jsx("div", { className: "gd-invertable-select-all-checkbox", ref: itemRef, tabIndex: 0, onFocus: onFocus, onKeyDown: onKeyDown, role: "checkbox", "aria-checked": isPartialSelection ? "mixed" : checked, "aria-controls": controlsId, children: _jsxs("label", { className: labelClasses, children: [_jsx("input", { readOnly: true, type: "checkbox", className: checkboxClasses, checked: checked, onChange: handleToggle, "aria-hidden": true, role: "presentation", tabIndex: -1 }), _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] }), _jsx("span", { className: "gd-list-actions-selection-size s-list-search-selection-size", children: `(${totalItemsCount})` })] })] }) }));
}
//# sourceMappingURL=InvertableSelectAllCheckbox.js.map