import React, { useRef, useState } from "react";
import { cl } from "../../utils/helpers/index.js";
import { useInputContext } from "./Input/Input.context.js";
const ComboboxWrapper = ({ children, className, hasError, inputProps, inputSize, }) => {
    const { toggleOpenButtonRef, clearInput, readOnly } = useInputContext();
    const wrapperRef = useRef(null);
    const [hasFocusWithin, setHasFocusWithin] = useState(false);
    function onFocusInsideWrapper(event) {
        var _a;
        if (!((_a = wrapperRef.current) === null || _a === void 0 ? void 0 : _a.contains(event.relatedTarget)) &&
            (toggleOpenButtonRef === null || toggleOpenButtonRef === void 0 ? void 0 : toggleOpenButtonRef.current) !== event.target) {
            setHasFocusWithin(true);
        }
    }
    function onBlurWrapper(event) {
        var _a;
        if (!((_a = wrapperRef.current) === null || _a === void 0 ? void 0 : _a.contains(event.relatedTarget))) {
            setHasFocusWithin(false);
            clearInput(event);
        }
    }
    return (React.createElement("div", { ref: wrapperRef, className: cl(className, "aksel-form-field", `aksel-form-field--${inputSize}`, {
            "aksel-combobox--error": hasError,
            "aksel-combobox--disabled": !!inputProps.disabled,
            "aksel-combobox--focused": hasFocusWithin,
            "aksel-combobox--readonly": readOnly,
            "aksel-form-field--readonly": readOnly,
        }), onFocus: onFocusInsideWrapper, onBlur: onBlurWrapper }, children));
};
export default ComboboxWrapper;
//# sourceMappingURL=ComboboxWrapper.js.map