import { useMemo, useState } from "@lynx-js/react";
import { Button } from "@lynx-js/lynx-ui-button";
import { renderContentWithExtraProps, useMemoizedFn } from "@lynx-js/lynx-ui-common";
import { clsx } from "clsx";
import { CheckboxContext } from "./CheckboxContext.jsx";
const Checkbox = (props)=>{
    const { style, className, children, onChange, checked, defaultChecked = false, disabled = false, indeterminate = false, checkboxProps } = props;
    const isControlled = void 0 !== checked;
    const [uncontrolledChecked, setUncontrolledChecked] = useState(defaultChecked);
    const actualChecked = isControlled ? checked : uncontrolledChecked;
    const handleValueChange = useMemoizedFn((newChecked)=>{
        if (!isControlled) setUncontrolledChecked(newChecked);
        onChange?.(newChecked);
    });
    const handleClick = useMemoizedFn(()=>{
        if (indeterminate) return void handleValueChange(true);
        handleValueChange(!actualChecked);
    });
    const checkboxContextValue = useMemo(()=>({
            checked: actualChecked,
            indeterminate
        }), [
        actualChecked,
        indeterminate
    ]);
    return <CheckboxContext.Provider value={checkboxContextValue}>
      <Button disabled={disabled} onClick={handleClick} style={style} className={clsx(className, {
        'ui-indeterminate': indeterminate,
        'ui-checked': actualChecked
    })} buttonProps={checkboxProps}>
        {renderContentWithExtraProps({
        checked: actualChecked,
        indeterminate
    }, children)}
      </Button>
    </CheckboxContext.Provider>;
};
export { Checkbox };

//# sourceMappingURL=Checkbox.jsx.map