import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2025-2026 GoodData Corporation
import { useEffect, useId, useRef } from "react";
import { accessibilityConfigToAttributes } from "../../typings/utilities.js";
import { bem } from "../@utils/bem.js";
const { b, e } = bem("gd-ui-kit-checkbox");
/**
 * Checkbox control — renders a native `<input type="checkbox">` for
 * accessibility and a custom 14px box for the visual. The input is overlaid on
 * the box, so clicking the box toggles it. Renders no wrapping `<label>`: an
 * optional inline label is a sibling associated via `htmlFor`, which lets an
 * ancestor row supply its own label without nesting.
 *
 * @internal
 */
export function UiCheckbox({ checked, onChange = () => { }, preventDefault = false, stopPropagation = false, indeterminate = false, disabled = false, accessibilityConfig, tabIndex, label, id, }) {
    const inputRef = useRef(null);
    const generatedId = useId();
    const inputId = id ?? generatedId;
    useEffect(() => {
        if (inputRef.current) {
            inputRef.current.indeterminate = indeterminate;
        }
    }, [indeterminate]);
    return (_jsxs("span", { className: b(), children: [
            _jsxs("span", { className: e("control"), children: [
                    _jsx("input", { ref: inputRef, id: inputId, type: "checkbox", className: e("input"), checked: checked, onChange: onChange, disabled: disabled, onClick: (e) => {
                            if (preventDefault) {
                                e.preventDefault();
                            }
                            if (stopPropagation) {
                                e.stopPropagation();
                            }
                        }, tabIndex: tabIndex, ...accessibilityConfigToAttributes(accessibilityConfig) }), _jsx("span", { className: e("box", { checked, indeterminate, disabled }), "aria-hidden": "true" })
                ] }), label ? (_jsx("label", { className: e("label") + " gd-label", htmlFor: inputId, children: label })) : null] }));
}
