var $16c7f9b22cce3838$exports = require("./Button.cjs");
var $048d76b84370f141$exports = require("./utils.cjs");
var $862aa7df04d8fa76$exports = require("./FieldError.cjs");
var $5adc12e2ce73be8f$exports = require("./Form.cjs");
var $f3068c15cd7dcac2$exports = require("./Group.cjs");
var $81dc1c05bf045ce0$exports = require("./Input.cjs");
var $d5d46822336ca1e1$exports = require("./Label.cjs");
var $537333b300f7e667$exports = require("./ListBox.cjs");
var $88595bf043e542ec$exports = require("./Dialog.cjs");
var $74e35a768d38d46b$exports = require("./Popover.cjs");
var $cab7d9a238d19c33$exports = require("./Text.cjs");
var $3UK7x$reactariauseComboBox = require("react-aria/useComboBox");
var $3UK7x$reactariaprivatecollectionsCollectionBuilder = require("react-aria/private/collections/CollectionBuilder");
var $3UK7x$reactstatelyuseComboBoxState = require("react-stately/useComboBoxState");
var $3UK7x$reactariaprivatecollectionsHidden = require("react-aria/private/collections/Hidden");
var $3UK7x$reactariaprivateutilsfilterDOMProps = require("react-aria/private/utils/filterDOMProps");
var $3UK7x$react = require("react");
var $3UK7x$reactariauseFilter = require("react-aria/useFilter");
var $3UK7x$reactariauseListFormatter = require("react-aria/useListFormatter");
var $3UK7x$reactariaprivateutilsuseResizeObserver = require("react-aria/private/utils/useResizeObserver");


function $parcel$interopDefault(a) {
  return a && a.__esModule ? a.default : a;
}

function $parcel$export(e, n, v, s) {
  Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
}

$parcel$export(module.exports, "ComboBoxContext", function () { return $251f6141ad6e692a$export$d414ccceff7063c3; });
$parcel$export(module.exports, "ComboBoxStateContext", function () { return $251f6141ad6e692a$export$c02625b26074192c; });
$parcel$export(module.exports, "ComboBox", function () { return $251f6141ad6e692a$export$72b9695b8216309a; });
$parcel$export(module.exports, "ComboBoxValueContext", function () { return $251f6141ad6e692a$export$5c804022b41722df; });
$parcel$export(module.exports, "ComboBoxValue", function () { return $251f6141ad6e692a$export$3527949051a2d3a; });
/*
 * Copyright 2022 Adobe. All rights reserved.
 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy
 * of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
 * OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */ 



















const $251f6141ad6e692a$export$d414ccceff7063c3 = /*#__PURE__*/ (0, $3UK7x$react.createContext)(null);
const $251f6141ad6e692a$export$c02625b26074192c = /*#__PURE__*/ (0, $3UK7x$react.createContext)(null);
const $251f6141ad6e692a$export$72b9695b8216309a = /*#__PURE__*/ (0, $3UK7x$react.forwardRef)(function ComboBox(props, ref) {
    [props, ref] = (0, $048d76b84370f141$exports.useContextProps)(props, ref, $251f6141ad6e692a$export$d414ccceff7063c3);
    let { children: children, isDisabled: isDisabled = false, isInvalid: isInvalid = false, isRequired: isRequired = false } = props;
    let content = (0, $3UK7x$react.useMemo)(()=>/*#__PURE__*/ (0, ($parcel$interopDefault($3UK7x$react))).createElement((0, $537333b300f7e667$exports.ListBoxContext).Provider, {
            value: {
                items: props.items ?? props.defaultItems
            }
        }, typeof children === 'function' ? children({
            isOpen: false,
            isDisabled: isDisabled,
            isInvalid: isInvalid,
            isRequired: isRequired,
            defaultChildren: null
        }) : children), [
        children,
        isDisabled,
        isInvalid,
        isRequired,
        props.items,
        props.defaultItems
    ]);
    return /*#__PURE__*/ (0, ($parcel$interopDefault($3UK7x$react))).createElement((0, $3UK7x$reactariaprivatecollectionsCollectionBuilder.CollectionBuilder), {
        content: content
    }, (collection)=>/*#__PURE__*/ (0, ($parcel$interopDefault($3UK7x$react))).createElement($251f6141ad6e692a$var$ComboBoxInner, {
            props: props,
            collection: collection,
            comboBoxRef: ref
        }));
});
// Contexts to clear inside the popover.
const $251f6141ad6e692a$var$CLEAR_CONTEXTS = [
    (0, $d5d46822336ca1e1$exports.LabelContext),
    (0, $16c7f9b22cce3838$exports.ButtonContext),
    (0, $81dc1c05bf045ce0$exports.InputContext),
    (0, $f3068c15cd7dcac2$exports.GroupContext),
    (0, $cab7d9a238d19c33$exports.TextContext)
];
function $251f6141ad6e692a$var$ComboBoxInner({ props: props, collection: collection, comboBoxRef: ref }) {
    let { name: name, formValue: formValue = 'key', allowsCustomValue: allowsCustomValue } = props;
    if (allowsCustomValue) formValue = 'text';
    let { validationBehavior: formValidationBehavior } = (0, $048d76b84370f141$exports.useSlottedContext)((0, $5adc12e2ce73be8f$exports.FormContext)) || {};
    let validationBehavior = props.validationBehavior ?? formValidationBehavior ?? 'native';
    let { contains: contains } = (0, $3UK7x$reactariauseFilter.useFilter)({
        sensitivity: 'base'
    });
    let state = (0, $3UK7x$reactstatelyuseComboBoxState.useComboBoxState)({
        ...props,
        defaultFilter: props.defaultFilter || contains,
        // If props.items isn't provided, rely on collection filtering (aka listbox.items is provided or defaultItems provided to Combobox)
        items: props.items,
        children: undefined,
        collection: collection,
        validationBehavior: validationBehavior
    });
    let buttonRef = (0, $3UK7x$react.useRef)(null);
    let inputRef = (0, $3UK7x$react.useRef)(null);
    let listBoxRef = (0, $3UK7x$react.useRef)(null);
    let popoverRef = (0, $3UK7x$react.useRef)(null);
    let [labelRef, label] = (0, $048d76b84370f141$exports.useSlot)(!props['aria-label'] && !props['aria-labelledby']);
    let { buttonProps: buttonProps, inputProps: inputProps, listBoxProps: listBoxProps, labelProps: labelProps, descriptionProps: descriptionProps, errorMessageProps: errorMessageProps, valueProps: valueProps, ...validation } = (0, $3UK7x$reactariauseComboBox.useComboBox)({
        ...(0, $048d76b84370f141$exports.removeDataAttributes)(props),
        label: label,
        inputRef: inputRef,
        buttonRef: buttonRef,
        listBoxRef: listBoxRef,
        popoverRef: popoverRef,
        name: formValue === 'text' ? name : undefined,
        validationBehavior: validationBehavior
    }, state);
    // Make menu width match input + button
    let [menuWidth, setMenuWidth] = (0, $3UK7x$react.useState)(null);
    let onResize = (0, $3UK7x$react.useCallback)(()=>{
        if (inputRef.current) {
            let buttonRect = buttonRef.current?.getBoundingClientRect();
            let inputRect = inputRef.current.getBoundingClientRect();
            let minX = buttonRect ? Math.min(buttonRect.left, inputRect.left) : inputRect.left;
            let maxX = buttonRect ? Math.max(buttonRect.right, inputRect.right) : inputRect.right;
            setMenuWidth(maxX - minX + 'px');
        }
    }, [
        buttonRef,
        inputRef,
        setMenuWidth
    ]);
    (0, $3UK7x$reactariaprivateutilsuseResizeObserver.useResizeObserver)({
        ref: inputRef,
        onResize: onResize
    });
    // Only expose a subset of state to renderProps function to avoid infinite render loop
    let renderPropsState = (0, $3UK7x$react.useMemo)(()=>({
            isOpen: state.isOpen,
            isDisabled: props.isDisabled || false,
            isInvalid: validation.isInvalid || false,
            isRequired: props.isRequired || false
        }), [
        state.isOpen,
        props.isDisabled,
        validation.isInvalid,
        props.isRequired
    ]);
    let renderProps = (0, $048d76b84370f141$exports.useRenderProps)({
        ...props,
        values: renderPropsState,
        defaultClassName: 'react-aria-ComboBox'
    });
    let DOMProps = (0, $3UK7x$reactariaprivateutilsfilterDOMProps.filterDOMProps)(props, {
        global: true
    });
    delete DOMProps.id;
    let inputs = [];
    if (name && formValue === 'key') {
        let values = Array.isArray(state.value) ? state.value : [
            state.value
        ];
        if (values.length === 0) values = [
            null
        ];
        inputs = values.map((value, i)=>/*#__PURE__*/ (0, ($parcel$interopDefault($3UK7x$react))).createElement("input", {
                key: i,
                type: "hidden",
                name: name,
                form: props.form,
                value: value ?? ''
            }));
    }
    return /*#__PURE__*/ (0, ($parcel$interopDefault($3UK7x$react))).createElement((0, $048d76b84370f141$exports.Provider), {
        values: [
            [
                $251f6141ad6e692a$export$c02625b26074192c,
                state
            ],
            [
                (0, $d5d46822336ca1e1$exports.LabelContext),
                {
                    ...labelProps,
                    ref: labelRef
                }
            ],
            [
                (0, $16c7f9b22cce3838$exports.ButtonContext),
                {
                    ...buttonProps,
                    ref: buttonRef,
                    isPressed: state.isOpen
                }
            ],
            [
                (0, $81dc1c05bf045ce0$exports.InputContext),
                {
                    ...inputProps,
                    ref: inputRef
                }
            ],
            [
                (0, $88595bf043e542ec$exports.OverlayTriggerStateContext),
                state
            ],
            [
                (0, $74e35a768d38d46b$exports.PopoverContext),
                {
                    ref: popoverRef,
                    triggerRef: inputRef,
                    scrollRef: listBoxRef,
                    placement: 'bottom start',
                    isNonModal: true,
                    trigger: 'ComboBox',
                    style: {
                        '--trigger-width': menuWidth
                    },
                    clearContexts: $251f6141ad6e692a$var$CLEAR_CONTEXTS
                }
            ],
            [
                (0, $537333b300f7e667$exports.ListBoxContext),
                {
                    ...listBoxProps,
                    ref: listBoxRef
                }
            ],
            [
                (0, $537333b300f7e667$exports.ListStateContext),
                state
            ],
            [
                (0, $cab7d9a238d19c33$exports.TextContext),
                {
                    slots: {
                        description: descriptionProps,
                        errorMessage: errorMessageProps
                    }
                }
            ],
            [
                (0, $f3068c15cd7dcac2$exports.GroupContext),
                {
                    isInvalid: validation.isInvalid,
                    isDisabled: props.isDisabled || false
                }
            ],
            [
                (0, $862aa7df04d8fa76$exports.FieldErrorContext),
                validation
            ],
            [
                $251f6141ad6e692a$export$5c804022b41722df,
                valueProps
            ]
        ]
    }, /*#__PURE__*/ (0, ($parcel$interopDefault($3UK7x$react))).createElement((0, $048d76b84370f141$exports.dom).div, {
        ...DOMProps,
        ...renderProps,
        ref: ref,
        slot: props.slot || undefined,
        "data-focused": state.isFocused || undefined,
        "data-open": state.isOpen || undefined,
        "data-disabled": props.isDisabled || undefined,
        "data-invalid": validation.isInvalid || undefined,
        "data-required": props.isRequired || undefined
    }, renderProps.children, inputs));
}
const $251f6141ad6e692a$export$5c804022b41722df = /*#__PURE__*/ (0, $3UK7x$react.createContext)(null);
const $251f6141ad6e692a$export$3527949051a2d3a = /*#__PURE__*/ (0, $3UK7x$reactariaprivatecollectionsHidden.createHideableComponent)(function ComboBoxValue(props, ref) {
    [props, ref] = (0, $048d76b84370f141$exports.useContextProps)(props, ref, $251f6141ad6e692a$export$5c804022b41722df);
    let state = (0, $3UK7x$react.useContext)($251f6141ad6e692a$export$c02625b26074192c);
    let formatter = (0, $3UK7x$reactariauseListFormatter.useListFormatter)();
    let selectedText = (0, $3UK7x$react.useMemo)(()=>formatter.format(state.selectedItems.map((item)=>item?.textValue || '').filter((v)=>v !== '')), [
        formatter,
        state.selectedItems
    ]);
    let renderProps = (0, $048d76b84370f141$exports.useRenderProps)({
        ...props,
        defaultChildren: selectedText || props.placeholder,
        defaultClassName: 'react-aria-ComboBoxValue',
        values: {
            selectedItems: (0, $3UK7x$react.useMemo)(()=>state.selectedItems.map((item)=>item.value ?? null), [
                state.selectedItems
            ]),
            selectedText: selectedText,
            isPlaceholder: state.selectedItems.length === 0,
            state: state
        }
    });
    let DOMProps = (0, $3UK7x$reactariaprivateutilsfilterDOMProps.filterDOMProps)(props, {
        global: true
    });
    return /*#__PURE__*/ (0, ($parcel$interopDefault($3UK7x$react))).createElement((0, $048d76b84370f141$exports.dom).div, {
        ref: ref,
        ...DOMProps,
        ...renderProps,
        "data-placeholder": state.selectedItems.length === 0 || undefined
    });
});


//# sourceMappingURL=ComboBox.cjs.map
