/* @oracle/oraclejet-preact: undefined */
'use strict';

var jsxRuntime = require('preact/jsx-runtime');
var compat = require('preact/compat');
require('./logger-2b636482.js');
require('./TabbableModeContext-e99d527e.js');
require('preact/hooks');
var useId = require('./useId-6c0eeb27.js');
require('./LayerHost-8ec73638.js');


require('./UNSAFE_UserAssistance/themes/redwood/UserAssistanceVariants.css.js');
var InlineUserAssistance = require('./InlineUserAssistance-6c6d3250.js');
require('./Flex-da9f223a.js');


require('./UNSAFE_MessageBanner/themes/redwood/MessageBannerVariants.css.js');
require('preact');


require('./hooks/UNSAFE_useTooltip/themes/redwood/TooltipContentVariants.css.js');
require('./tooltipUtils-d461919d.js');

require('./IconButton-fad4e4b2.js');

require('./MessageFormattingUtils-42d84399.js');
require('./TransitionGroup-b239d98f.js');
require('./MessagesContext-4e939750.js');



require('./UNSAFE_ComponentMessage/themes/redwood/ComponentMessageVariants.css.js');
require('./FormFieldContext-8418dc68.js');
require('./Popup-17440baa.js');
require('./UNSAFE_Separator/themes/SeparatorStyles.css.js');


require('./UNSAFE_Label/themes/redwood/LabelVariants.css.js');
require('./InputGroupContext-05f2a46f.js');

require('./UNSAFE_Popup/themes/redwood/PopupVariants.css.js');
require('./FormContext-9452bd30.js');
var useFormContext = require('./useFormContext-4c3091dc.js');
var CheckboxRadioContext = require('./CheckboxRadioContext-3b134bd1.js');
var CheckboxControl = require('./CheckboxControl-697725cb.js');

/**
 * Checkbox is a single standalone checkbox control. It represents a boolean value.
 */
const Checkbox = compat.forwardRef(({ 'aria-describedby': ariaDescribedBy, assistiveText, columnSpan, helpSourceLink, helpSourceText, isDisabled: propIsDisabled, isReadonly: propIsReadonly, isRequired = false, messages, onCommit, userAssistanceDensity: propUserAssistanceDensity, value: propValue, testId, children }, ref = null) => {
    const { isDisabled: isFormDisabled, isReadonly: isFormReadonly, userAssistanceDensity: formUserAssistanceDensity } = useFormContext.useFormContext();
    const checkboxRef = compat.useRef(null);
    const id = useId.useId();
    const UAtestId = testId ? `${testId}-inline-user-assistance` : undefined;
    // default to FormContext values if component properties are not specified
    const isDisabled = propIsDisabled ?? isFormDisabled;
    const isReadonly = propIsReadonly ?? isFormReadonly;
    const userAssistanceDensity = propUserAssistanceDensity ?? formUserAssistanceDensity;
    // we still render <InlineUserAssistance/> if density is 'efficient' or 'compact' to save space,
    // even though we don't render user assistance for disabled or readonly fields
    const hasBottomUserAssistance = !isDisabled && !isReadonly;
    const bottomUaId = hasBottomUserAssistance ? `${id}-bottom-ua` : undefined;
    const onChangeHandler = compat.useCallback((e) => {
        // return early on keyup if key isn't spacebar
        const { key } = e;
        const hasKey = key !== undefined;
        if (hasKey && key !== ' ') {
            return;
        }
        const input = e.target;
        // While mouse clicking StyledCheckbox results in toggling the checked state on an input element, spacebar key events do not.
        // If it is a spacebar key event, we will return the toggled value of its current state.
        const value = hasKey ? !input.checked : input.checked;
        const previousValue = !!propValue;
        if (previousValue !== value) {
            onCommit({
                value,
                previousValue
            });
        }
    }, [onCommit, propValue]);
    compat.useImperativeHandle(ref, () => ({
        focus: () => {
            !isDisabled && checkboxRef.current?.focus();
        },
        blur: () => {
            const activeElement = document.activeElement;
            checkboxRef.current === activeElement && activeElement.blur();
        }
    }));
    // TODO fix issue with tooltip and link in label
    // 
    return (jsxRuntime.jsxs(CheckboxRadioContext.CheckboxRadioContext.Provider, { value: { isFocusRingShown: !isDisabled }, children: [jsxRuntime.jsx(CheckboxControl.CheckboxControl, { ...(hasBottomUserAssistance
                    ? {
                        onChange: onChangeHandler,
                        onKeyUp: onChangeHandler
                    }
                    : {}), "aria-describedby": ariaDescribedBy, assistiveText: assistiveText, columnSpan: columnSpan, helpSourceLink: helpSourceLink, helpSourceText: helpSourceText, isChecked: propValue, isDisabled: isDisabled, isReadonly: isReadonly, isRequired: isRequired, ref: checkboxRef, children: children }), hasBottomUserAssistance ? (jsxRuntime.jsx(InlineUserAssistance.InlineUserAssistance, { id: bottomUaId, isRequiredShown: !!isRequired && !propValue, requiredAlignment: "start", messages: messages, userAssistanceDensity: userAssistanceDensity === 'reflow' ? 'reflow' : 'efficient', testId: UAtestId })) : userAssistanceDensity === 'reflow' ? undefined : (
            /* save space for user assistance if density is 'efficient' or 'compact', even though we don't
               render user assistance for disabled or readonly fields */
            jsxRuntime.jsx(InlineUserAssistance.InlineUserAssistance, { userAssistanceDensity: "efficient", testId: UAtestId }))] }));
});

exports.Checkbox = Checkbox;
//# sourceMappingURL=Checkbox-07e4794b.js.map
