import {ButtonContext as $7705c033048f6da7$export$24d547caef80ccd1} from "./Button.mjs";
import {DEFAULT_SLOT as $7230ffa83bc0c2cf$export$c62b8e45d58ddad9, dom as $7230ffa83bc0c2cf$export$df3a06d6289f983e, Provider as $7230ffa83bc0c2cf$export$2881499e37b75b9a, useContextProps as $7230ffa83bc0c2cf$export$29f1550f4b0d4415, useRenderProps as $7230ffa83bc0c2cf$export$4d86445c2cf5e3} from "./utils.mjs";
import {HeadingContext as $2ec61d1d4f780267$export$d688439359537581} from "./Heading.mjs";
import {PopoverContext as $542a13ca2fa5b484$export$9b9a0cd73afb7ca4} from "./Popover.mjs";
import {RootMenuTriggerStateContext as $49319ee1285aa241$export$795aec4671cbae19} from "./Menu.mjs";
import {useDialog as $gV8PY$useDialog} from "react-aria/useDialog";
import {filterDOMProps as $gV8PY$filterDOMProps} from "react-aria/private/utils/filterDOMProps";
import {mergeProps as $gV8PY$mergeProps} from "react-aria/mergeProps";
import {PressResponder as $gV8PY$PressResponder} from "react-aria/private/interactions/PressResponder";
import $gV8PY$react, {createContext as $gV8PY$createContext, useRef as $gV8PY$useRef, useState as $gV8PY$useState, useCallback as $gV8PY$useCallback, forwardRef as $gV8PY$forwardRef, useContext as $gV8PY$useContext} from "react";
import {useId as $gV8PY$useId} from "react-aria/useId";
import {useMenuTriggerState as $gV8PY$useMenuTriggerState} from "react-stately/useMenuTriggerState";
import {useOverlayTrigger as $gV8PY$useOverlayTrigger} from "react-aria/useOverlayTrigger";
import {useResizeObserver as $gV8PY$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 $f2ff30fde7b014be$export$8b93a07348a7730c = /*#__PURE__*/ (0, $gV8PY$createContext)(null);
const $f2ff30fde7b014be$export$d2f961adcb0afbe = /*#__PURE__*/ (0, $gV8PY$createContext)(null);
function $f2ff30fde7b014be$export$2e1e1122cf0cba88(props) {
    // Use useMenuTriggerState instead of useOverlayTriggerState in case a menu is embedded in the dialog.
    // This is needed to handle submenus.
    let state = (0, $gV8PY$useMenuTriggerState)(props);
    let buttonRef = (0, $gV8PY$useRef)(null);
    let { triggerProps: triggerProps, overlayProps: overlayProps } = (0, $gV8PY$useOverlayTrigger)({
        type: 'dialog'
    }, state, buttonRef);
    // Allows popover width to match trigger element
    let [buttonWidth, setButtonWidth] = (0, $gV8PY$useState)(null);
    let onResize = (0, $gV8PY$useCallback)(()=>{
        if (buttonRef.current) setButtonWidth(buttonRef.current.offsetWidth + 'px');
    }, [
        buttonRef
    ]);
    (0, $gV8PY$useResizeObserver)({
        ref: buttonRef,
        onResize: onResize
    });
    // Label dialog by the trigger as a fallback if there is no title slot.
    // This is done in RAC instead of hooks because otherwise we cannot distinguish
    // between context and props. Normally aria-labelledby overrides the title
    // but when sent by context we want the title to win.
    triggerProps.id = (0, $gV8PY$useId)();
    overlayProps['aria-labelledby'] = triggerProps.id;
    return /*#__PURE__*/ (0, $gV8PY$react).createElement((0, $7230ffa83bc0c2cf$export$2881499e37b75b9a), {
        values: [
            [
                $f2ff30fde7b014be$export$d2f961adcb0afbe,
                state
            ],
            [
                (0, $49319ee1285aa241$export$795aec4671cbae19),
                state
            ],
            [
                $f2ff30fde7b014be$export$8b93a07348a7730c,
                overlayProps
            ],
            [
                (0, $542a13ca2fa5b484$export$9b9a0cd73afb7ca4),
                {
                    trigger: 'DialogTrigger',
                    triggerRef: buttonRef,
                    'aria-labelledby': overlayProps['aria-labelledby'],
                    style: {
                        '--trigger-width': buttonWidth
                    }
                }
            ]
        ]
    }, /*#__PURE__*/ (0, $gV8PY$react).createElement((0, $gV8PY$PressResponder), {
        ...triggerProps,
        ref: buttonRef,
        isPressed: state.isOpen
    }, props.children));
}
const $f2ff30fde7b014be$export$3ddf2d174ce01153 = /*#__PURE__*/ (0, $gV8PY$forwardRef)(function Dialog(props, ref) {
    let originalAriaLabelledby = props['aria-labelledby'];
    [props, ref] = (0, $7230ffa83bc0c2cf$export$29f1550f4b0d4415)(props, ref, $f2ff30fde7b014be$export$8b93a07348a7730c);
    let { dialogProps: dialogProps, titleProps: titleProps } = (0, $gV8PY$useDialog)({
        ...props,
        // Only pass aria-labelledby from props, not context.
        // Context is used as a fallback below.
        'aria-labelledby': originalAriaLabelledby
    }, ref);
    let state = (0, $gV8PY$useContext)($f2ff30fde7b014be$export$d2f961adcb0afbe);
    if (!dialogProps['aria-label'] && !dialogProps['aria-labelledby']) {
        // If aria-labelledby exists on props, we know it came from context.
        // Use that as a fallback in case there is no title slot.
        if (props['aria-labelledby']) dialogProps['aria-labelledby'] = props['aria-labelledby'];
        else if (process.env.NODE_ENV !== 'production') console.warn('If a Dialog does not contain a <Heading slot="title">, it must have an aria-label or aria-labelledby attribute for accessibility.');
    }
    let renderProps = (0, $7230ffa83bc0c2cf$export$4d86445c2cf5e3)({
        defaultClassName: 'react-aria-Dialog',
        className: props.className,
        style: props.style,
        children: props.children,
        values: {
            close: state?.close || (()=>{})
        }
    });
    let DOMProps = (0, $gV8PY$filterDOMProps)(props, {
        global: true
    });
    return /*#__PURE__*/ (0, $gV8PY$react).createElement((0, $7230ffa83bc0c2cf$export$df3a06d6289f983e).section, {
        ...(0, $gV8PY$mergeProps)(DOMProps, renderProps, dialogProps),
        render: props.render,
        ref: ref,
        slot: props.slot || undefined
    }, /*#__PURE__*/ (0, $gV8PY$react).createElement((0, $7230ffa83bc0c2cf$export$2881499e37b75b9a), {
        values: [
            [
                (0, $2ec61d1d4f780267$export$d688439359537581),
                {
                    slots: {
                        [(0, $7230ffa83bc0c2cf$export$c62b8e45d58ddad9)]: {},
                        title: {
                            ...titleProps,
                            level: 2
                        }
                    }
                }
            ],
            [
                (0, $7705c033048f6da7$export$24d547caef80ccd1),
                {
                    slots: {
                        [(0, $7230ffa83bc0c2cf$export$c62b8e45d58ddad9)]: {},
                        close: {
                            onPress: ()=>state?.close()
                        }
                    }
                }
            ]
        ]
    }, renderProps.children));
});


export {$f2ff30fde7b014be$export$8b93a07348a7730c as DialogContext, $f2ff30fde7b014be$export$d2f961adcb0afbe as OverlayTriggerStateContext, $f2ff30fde7b014be$export$2e1e1122cf0cba88 as DialogTrigger, $f2ff30fde7b014be$export$3ddf2d174ce01153 as Dialog};
//# sourceMappingURL=Dialog.mjs.map
