import {ButtonContext as $7705c033048f6da7$export$24d547caef80ccd1} from "./Button.mjs";
import {dom as $7230ffa83bc0c2cf$export$df3a06d6289f983e, Provider as $7230ffa83bc0c2cf$export$2881499e37b75b9a, removeDataAttributes as $7230ffa83bc0c2cf$export$ef03459518577ad4, useContextProps as $7230ffa83bc0c2cf$export$29f1550f4b0d4415, useRenderProps as $7230ffa83bc0c2cf$export$4d86445c2cf5e3, useSlot as $7230ffa83bc0c2cf$export$9d4c57ee4c6ffdd8, useSlottedContext as $7230ffa83bc0c2cf$export$fabf2dc03a41866e} from "./utils.mjs";
import {FieldErrorContext as $1f3c3b1a70cec653$export$ff05c3ac10437e03} from "./FieldError.mjs";
import {FormContext as $cdaed739b1139372$export$c24727297075ec6a} from "./Form.mjs";
import {GroupContext as $3a442827418ebe87$export$f9c6924e160136d1} from "./Group.mjs";
import {InputContext as $41fb335299a4a39e$export$37fb8590cf2c088c} from "./Input.mjs";
import {LabelContext as $43a3b93638fe5db9$export$75b6ee27786ba447} from "./Label.mjs";
import {ListBoxContext as $928221da08ecbc62$export$7ff8f37d2d81a48d, ListStateContext as $928221da08ecbc62$export$7c5906fe4f1f2af2} from "./ListBox.mjs";
import {OverlayTriggerStateContext as $f2ff30fde7b014be$export$d2f961adcb0afbe} from "./Dialog.mjs";
import {PopoverContext as $542a13ca2fa5b484$export$9b9a0cd73afb7ca4} from "./Popover.mjs";
import {TextContext as $efe09c6d1c304b50$export$9afb8bc826b033ea} from "./Text.mjs";
import {useComboBox as $er33p$useComboBox} from "react-aria/useComboBox";
import {CollectionBuilder as $er33p$CollectionBuilder} from "react-aria/private/collections/CollectionBuilder";
import {useComboBoxState as $er33p$useComboBoxState} from "react-stately/useComboBoxState";
import {createHideableComponent as $er33p$createHideableComponent} from "react-aria/private/collections/Hidden";
import {filterDOMProps as $er33p$filterDOMProps} from "react-aria/private/utils/filterDOMProps";
import $er33p$react, {createContext as $er33p$createContext, forwardRef as $er33p$forwardRef, useMemo as $er33p$useMemo, useRef as $er33p$useRef, useState as $er33p$useState, useCallback as $er33p$useCallback, useContext as $er33p$useContext} from "react";
import {useFilter as $er33p$useFilter} from "react-aria/useFilter";
import {useListFormatter as $er33p$useListFormatter} from "react-aria/useListFormatter";
import {useResizeObserver as $er33p$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 $dbdc5e6e7ce01b4b$export$d414ccceff7063c3 = /*#__PURE__*/ (0, $er33p$createContext)(null);
const $dbdc5e6e7ce01b4b$export$c02625b26074192c = /*#__PURE__*/ (0, $er33p$createContext)(null);
const $dbdc5e6e7ce01b4b$export$72b9695b8216309a = /*#__PURE__*/ (0, $er33p$forwardRef)(function ComboBox(props, ref) {
    [props, ref] = (0, $7230ffa83bc0c2cf$export$29f1550f4b0d4415)(props, ref, $dbdc5e6e7ce01b4b$export$d414ccceff7063c3);
    let { children: children, isDisabled: isDisabled = false, isInvalid: isInvalid = false, isRequired: isRequired = false } = props;
    let content = (0, $er33p$useMemo)(()=>/*#__PURE__*/ (0, $er33p$react).createElement((0, $928221da08ecbc62$export$7ff8f37d2d81a48d).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, $er33p$react).createElement((0, $er33p$CollectionBuilder), {
        content: content
    }, (collection)=>/*#__PURE__*/ (0, $er33p$react).createElement($dbdc5e6e7ce01b4b$var$ComboBoxInner, {
            props: props,
            collection: collection,
            comboBoxRef: ref
        }));
});
// Contexts to clear inside the popover.
const $dbdc5e6e7ce01b4b$var$CLEAR_CONTEXTS = [
    (0, $43a3b93638fe5db9$export$75b6ee27786ba447),
    (0, $7705c033048f6da7$export$24d547caef80ccd1),
    (0, $41fb335299a4a39e$export$37fb8590cf2c088c),
    (0, $3a442827418ebe87$export$f9c6924e160136d1),
    (0, $efe09c6d1c304b50$export$9afb8bc826b033ea)
];
function $dbdc5e6e7ce01b4b$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, $7230ffa83bc0c2cf$export$fabf2dc03a41866e)((0, $cdaed739b1139372$export$c24727297075ec6a)) || {};
    let validationBehavior = props.validationBehavior ?? formValidationBehavior ?? 'native';
    let { contains: contains } = (0, $er33p$useFilter)({
        sensitivity: 'base'
    });
    let state = (0, $er33p$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, $er33p$useRef)(null);
    let inputRef = (0, $er33p$useRef)(null);
    let listBoxRef = (0, $er33p$useRef)(null);
    let popoverRef = (0, $er33p$useRef)(null);
    let [labelRef, label] = (0, $7230ffa83bc0c2cf$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, $er33p$useComboBox)({
        ...(0, $7230ffa83bc0c2cf$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, $er33p$useState)(null);
    let onResize = (0, $er33p$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, $er33p$useResizeObserver)({
        ref: inputRef,
        onResize: onResize
    });
    // Only expose a subset of state to renderProps function to avoid infinite render loop
    let renderPropsState = (0, $er33p$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, $7230ffa83bc0c2cf$export$4d86445c2cf5e3)({
        ...props,
        values: renderPropsState,
        defaultClassName: 'react-aria-ComboBox'
    });
    let DOMProps = (0, $er33p$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, $er33p$react).createElement("input", {
                key: i,
                type: "hidden",
                name: name,
                form: props.form,
                value: value ?? ''
            }));
    }
    return /*#__PURE__*/ (0, $er33p$react).createElement((0, $7230ffa83bc0c2cf$export$2881499e37b75b9a), {
        values: [
            [
                $dbdc5e6e7ce01b4b$export$c02625b26074192c,
                state
            ],
            [
                (0, $43a3b93638fe5db9$export$75b6ee27786ba447),
                {
                    ...labelProps,
                    ref: labelRef
                }
            ],
            [
                (0, $7705c033048f6da7$export$24d547caef80ccd1),
                {
                    ...buttonProps,
                    ref: buttonRef,
                    isPressed: state.isOpen
                }
            ],
            [
                (0, $41fb335299a4a39e$export$37fb8590cf2c088c),
                {
                    ...inputProps,
                    ref: inputRef
                }
            ],
            [
                (0, $f2ff30fde7b014be$export$d2f961adcb0afbe),
                state
            ],
            [
                (0, $542a13ca2fa5b484$export$9b9a0cd73afb7ca4),
                {
                    ref: popoverRef,
                    triggerRef: inputRef,
                    scrollRef: listBoxRef,
                    placement: 'bottom start',
                    isNonModal: true,
                    trigger: 'ComboBox',
                    style: {
                        '--trigger-width': menuWidth
                    },
                    clearContexts: $dbdc5e6e7ce01b4b$var$CLEAR_CONTEXTS
                }
            ],
            [
                (0, $928221da08ecbc62$export$7ff8f37d2d81a48d),
                {
                    ...listBoxProps,
                    ref: listBoxRef
                }
            ],
            [
                (0, $928221da08ecbc62$export$7c5906fe4f1f2af2),
                state
            ],
            [
                (0, $efe09c6d1c304b50$export$9afb8bc826b033ea),
                {
                    slots: {
                        description: descriptionProps,
                        errorMessage: errorMessageProps
                    }
                }
            ],
            [
                (0, $3a442827418ebe87$export$f9c6924e160136d1),
                {
                    isInvalid: validation.isInvalid,
                    isDisabled: props.isDisabled || false
                }
            ],
            [
                (0, $1f3c3b1a70cec653$export$ff05c3ac10437e03),
                validation
            ],
            [
                $dbdc5e6e7ce01b4b$export$5c804022b41722df,
                valueProps
            ]
        ]
    }, /*#__PURE__*/ (0, $er33p$react).createElement((0, $7230ffa83bc0c2cf$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 $dbdc5e6e7ce01b4b$export$5c804022b41722df = /*#__PURE__*/ (0, $er33p$createContext)(null);
const $dbdc5e6e7ce01b4b$export$3527949051a2d3a = /*#__PURE__*/ (0, $er33p$createHideableComponent)(function ComboBoxValue(props, ref) {
    [props, ref] = (0, $7230ffa83bc0c2cf$export$29f1550f4b0d4415)(props, ref, $dbdc5e6e7ce01b4b$export$5c804022b41722df);
    let state = (0, $er33p$useContext)($dbdc5e6e7ce01b4b$export$c02625b26074192c);
    let formatter = (0, $er33p$useListFormatter)();
    let selectedText = (0, $er33p$useMemo)(()=>formatter.format(state.selectedItems.map((item)=>item?.textValue || '').filter((v)=>v !== '')), [
        formatter,
        state.selectedItems
    ]);
    let renderProps = (0, $7230ffa83bc0c2cf$export$4d86445c2cf5e3)({
        ...props,
        defaultChildren: selectedText || props.placeholder,
        defaultClassName: 'react-aria-ComboBoxValue',
        values: {
            selectedItems: (0, $er33p$useMemo)(()=>state.selectedItems.map((item)=>item.value ?? null), [
                state.selectedItems
            ]),
            selectedText: selectedText,
            isPlaceholder: state.selectedItems.length === 0,
            state: state
        }
    });
    let DOMProps = (0, $er33p$filterDOMProps)(props, {
        global: true
    });
    return /*#__PURE__*/ (0, $er33p$react).createElement((0, $7230ffa83bc0c2cf$export$df3a06d6289f983e).div, {
        ref: ref,
        ...DOMProps,
        ...renderProps,
        "data-placeholder": state.selectedItems.length === 0 || undefined
    });
});


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