import {ButtonContext as $fc203795b9b363cd$export$24d547caef80ccd1} from "./Button.js";
import {dom as $b7b7a92703138c9b$export$df3a06d6289f983e, Provider as $b7b7a92703138c9b$export$2881499e37b75b9a, removeDataAttributes as $b7b7a92703138c9b$export$ef03459518577ad4, useContextProps as $b7b7a92703138c9b$export$29f1550f4b0d4415, useRenderProps as $b7b7a92703138c9b$export$4d86445c2cf5e3, useSlot as $b7b7a92703138c9b$export$9d4c57ee4c6ffdd8, useSlottedContext as $b7b7a92703138c9b$export$fabf2dc03a41866e} from "./utils.js";
import {FieldErrorContext as $6567560e1d9cc847$export$ff05c3ac10437e03} from "./FieldError.js";
import {FormContext as $c7332d4a2d191cd2$export$c24727297075ec6a} from "./Form.js";
import {GroupContext as $2e357e4f16c05be6$export$f9c6924e160136d1} from "./Group.js";
import {InputContext as $d8e7992b5f7739ce$export$37fb8590cf2c088c} from "./Input.js";
import {LabelContext as $3e4839e5b30e7b17$export$75b6ee27786ba447} from "./Label.js";
import {ListBoxContext as $ba3142315b3e1149$export$7ff8f37d2d81a48d, ListStateContext as $ba3142315b3e1149$export$7c5906fe4f1f2af2} from "./ListBox.js";
import {OverlayTriggerStateContext as $acf8e70c2f419f18$export$d2f961adcb0afbe} from "./Dialog.js";
import {PopoverContext as $03df2f2d3cffb62f$export$9b9a0cd73afb7ca4} from "./Popover.js";
import {TextContext as $20d769b1e2b13352$export$9afb8bc826b033ea} from "./Text.js";
import {useComboBox as $feVgu$useComboBox} from "react-aria/useComboBox";
import {CollectionBuilder as $feVgu$CollectionBuilder} from "react-aria/CollectionBuilder";
import {useComboBoxState as $feVgu$useComboBoxState} from "react-stately/useComboBoxState";
import {createHideableComponent as $feVgu$createHideableComponent} from "react-aria/private/collections/Hidden";
import {filterDOMProps as $feVgu$filterDOMProps} from "react-aria/filterDOMProps";
import $feVgu$react, {createContext as $feVgu$createContext, forwardRef as $feVgu$forwardRef, useMemo as $feVgu$useMemo, useRef as $feVgu$useRef, useState as $feVgu$useState, useCallback as $feVgu$useCallback, useContext as $feVgu$useContext} from "react";
import {useFilter as $feVgu$useFilter} from "react-aria/useFilter";
import {useListFormatter as $feVgu$useListFormatter} from "react-aria/useListFormatter";
import {useResizeObserver as $feVgu$useResizeObserver} from "react-aria/private/utils/useResizeObserver";

/*
 * 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 $ddc2347ad81bd857$export$d414ccceff7063c3 = /*#__PURE__*/ (0, $feVgu$createContext)(null);
const $ddc2347ad81bd857$export$c02625b26074192c = /*#__PURE__*/ (0, $feVgu$createContext)(null);
const $ddc2347ad81bd857$export$72b9695b8216309a = /*#__PURE__*/ (0, $feVgu$forwardRef)(function ComboBox(props, ref) {
    [props, ref] = (0, $b7b7a92703138c9b$export$29f1550f4b0d4415)(props, ref, $ddc2347ad81bd857$export$d414ccceff7063c3);
    let { children: children, isDisabled: isDisabled = false, isInvalid: isInvalid = false, isRequired: isRequired = false } = props;
    let content = (0, $feVgu$useMemo)(()=>{
        var _props_items;
        return /*#__PURE__*/ (0, $feVgu$react).createElement((0, $ba3142315b3e1149$export$7ff8f37d2d81a48d).Provider, {
            value: {
                items: (_props_items = props.items) !== null && _props_items !== void 0 ? _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, $feVgu$react).createElement((0, $feVgu$CollectionBuilder), {
        content: content
    }, (collection)=>/*#__PURE__*/ (0, $feVgu$react).createElement($ddc2347ad81bd857$var$ComboBoxInner, {
            props: props,
            collection: collection,
            comboBoxRef: ref
        }));
});
// Contexts to clear inside the popover.
const $ddc2347ad81bd857$var$CLEAR_CONTEXTS = [
    (0, $3e4839e5b30e7b17$export$75b6ee27786ba447),
    (0, $fc203795b9b363cd$export$24d547caef80ccd1),
    (0, $d8e7992b5f7739ce$export$37fb8590cf2c088c),
    (0, $2e357e4f16c05be6$export$f9c6924e160136d1),
    (0, $20d769b1e2b13352$export$9afb8bc826b033ea)
];
function $ddc2347ad81bd857$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, $b7b7a92703138c9b$export$fabf2dc03a41866e)((0, $c7332d4a2d191cd2$export$c24727297075ec6a)) || {};
    var _props_validationBehavior, _ref;
    let validationBehavior = (_ref = (_props_validationBehavior = props.validationBehavior) !== null && _props_validationBehavior !== void 0 ? _props_validationBehavior : formValidationBehavior) !== null && _ref !== void 0 ? _ref : 'native';
    let { contains: contains } = (0, $feVgu$useFilter)({
        sensitivity: 'base'
    });
    let state = (0, $feVgu$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, $feVgu$useRef)(null);
    let inputRef = (0, $feVgu$useRef)(null);
    let listBoxRef = (0, $feVgu$useRef)(null);
    let popoverRef = (0, $feVgu$useRef)(null);
    let [labelRef, label] = (0, $b7b7a92703138c9b$export$9d4c57ee4c6ffdd8)(!props['aria-label'] && !props['aria-labelledby']);
    let { buttonProps: buttonProps, inputProps: inputProps, listBoxProps: listBoxProps, labelProps: labelProps, descriptionProps: descriptionProps, errorMessageProps: errorMessageProps, valueProps: valueProps, ...validation } = (0, $feVgu$useComboBox)({
        ...(0, $b7b7a92703138c9b$export$ef03459518577ad4)(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, $feVgu$useState)(null);
    let onResize = (0, $feVgu$useCallback)(()=>{
        if (inputRef.current) {
            var _buttonRef_current;
            let buttonRect = (_buttonRef_current = buttonRef.current) === null || _buttonRef_current === void 0 ? void 0 : _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, $feVgu$useResizeObserver)({
        ref: inputRef,
        onResize: onResize
    });
    // Only expose a subset of state to renderProps function to avoid infinite render loop
    let renderPropsState = (0, $feVgu$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, $b7b7a92703138c9b$export$4d86445c2cf5e3)({
        ...props,
        values: renderPropsState,
        defaultClassName: 'react-aria-ComboBox'
    });
    let DOMProps = (0, $feVgu$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, $feVgu$react).createElement("input", {
                key: i,
                type: "hidden",
                name: name,
                form: props.form,
                value: value !== null && value !== void 0 ? value : ''
            }));
    }
    return /*#__PURE__*/ (0, $feVgu$react).createElement((0, $b7b7a92703138c9b$export$2881499e37b75b9a), {
        values: [
            [
                $ddc2347ad81bd857$export$c02625b26074192c,
                state
            ],
            [
                (0, $3e4839e5b30e7b17$export$75b6ee27786ba447),
                {
                    ...labelProps,
                    ref: labelRef
                }
            ],
            [
                (0, $fc203795b9b363cd$export$24d547caef80ccd1),
                {
                    ...buttonProps,
                    ref: buttonRef,
                    isPressed: state.isOpen
                }
            ],
            [
                (0, $d8e7992b5f7739ce$export$37fb8590cf2c088c),
                {
                    ...inputProps,
                    ref: inputRef
                }
            ],
            [
                (0, $acf8e70c2f419f18$export$d2f961adcb0afbe),
                state
            ],
            [
                (0, $03df2f2d3cffb62f$export$9b9a0cd73afb7ca4),
                {
                    ref: popoverRef,
                    triggerRef: inputRef,
                    scrollRef: listBoxRef,
                    placement: 'bottom start',
                    isNonModal: true,
                    trigger: 'ComboBox',
                    style: {
                        '--trigger-width': menuWidth
                    },
                    clearContexts: $ddc2347ad81bd857$var$CLEAR_CONTEXTS
                }
            ],
            [
                (0, $ba3142315b3e1149$export$7ff8f37d2d81a48d),
                {
                    ...listBoxProps,
                    ref: listBoxRef
                }
            ],
            [
                (0, $ba3142315b3e1149$export$7c5906fe4f1f2af2),
                state
            ],
            [
                (0, $20d769b1e2b13352$export$9afb8bc826b033ea),
                {
                    slots: {
                        description: descriptionProps,
                        errorMessage: errorMessageProps
                    }
                }
            ],
            [
                (0, $2e357e4f16c05be6$export$f9c6924e160136d1),
                {
                    isInvalid: validation.isInvalid,
                    isDisabled: props.isDisabled || false
                }
            ],
            [
                (0, $6567560e1d9cc847$export$ff05c3ac10437e03),
                validation
            ],
            [
                $ddc2347ad81bd857$export$5c804022b41722df,
                valueProps
            ]
        ]
    }, /*#__PURE__*/ (0, $feVgu$react).createElement((0, $b7b7a92703138c9b$export$df3a06d6289f983e).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 $ddc2347ad81bd857$export$5c804022b41722df = /*#__PURE__*/ (0, $feVgu$createContext)(null);
const $ddc2347ad81bd857$export$3527949051a2d3a = /*#__PURE__*/ (0, $feVgu$createHideableComponent)(function ComboBoxValue(props, ref) {
    [props, ref] = (0, $b7b7a92703138c9b$export$29f1550f4b0d4415)(props, ref, $ddc2347ad81bd857$export$5c804022b41722df);
    let state = (0, $feVgu$useContext)($ddc2347ad81bd857$export$c02625b26074192c);
    let formatter = (0, $feVgu$useListFormatter)();
    let selectedText = (0, $feVgu$useMemo)(()=>formatter.format(state.selectedItems.map((item)=>(item === null || item === void 0 ? void 0 : item.textValue) || '').filter((v)=>v !== '')), [
        formatter,
        state.selectedItems
    ]);
    let renderProps = (0, $b7b7a92703138c9b$export$4d86445c2cf5e3)({
        ...props,
        defaultChildren: selectedText || props.placeholder,
        defaultClassName: 'react-aria-ComboBoxValue',
        values: {
            selectedItems: (0, $feVgu$useMemo)(()=>state.selectedItems.map((item)=>{
                    var _item_value;
                    return (_item_value = item.value) !== null && _item_value !== void 0 ? _item_value : null;
                }), [
                state.selectedItems
            ]),
            selectedText: selectedText,
            isPlaceholder: state.selectedItems.length === 0,
            state: state
        }
    });
    let DOMProps = (0, $feVgu$filterDOMProps)(props, {
        global: true
    });
    return /*#__PURE__*/ (0, $feVgu$react).createElement((0, $b7b7a92703138c9b$export$df3a06d6289f983e).div, {
        ref: ref,
        ...DOMProps,
        ...renderProps,
        "data-placeholder": state.selectedItems.length === 0 || undefined
    });
});


export {$ddc2347ad81bd857$export$d414ccceff7063c3 as ComboBoxContext, $ddc2347ad81bd857$export$c02625b26074192c as ComboBoxStateContext, $ddc2347ad81bd857$export$72b9695b8216309a as ComboBox, $ddc2347ad81bd857$export$5c804022b41722df as ComboBoxValueContext, $ddc2347ad81bd857$export$3527949051a2d3a as ComboBoxValue};
//# sourceMappingURL=ComboBox.js.map
