import {ButtonContext as $fc203795b9b363cd$export$24d547caef80ccd1} from "./Button.js";
import {DEFAULT_SLOT as $b7b7a92703138c9b$export$c62b8e45d58ddad9, dom as $b7b7a92703138c9b$export$df3a06d6289f983e, Provider as $b7b7a92703138c9b$export$2881499e37b75b9a, useContextProps as $b7b7a92703138c9b$export$29f1550f4b0d4415, useRenderProps as $b7b7a92703138c9b$export$4d86445c2cf5e3} from "./utils.js";
import {HeadingContext as $cd487658cd83358d$export$d688439359537581} from "./Heading.js";
import {PopoverContext as $03df2f2d3cffb62f$export$9b9a0cd73afb7ca4} from "./Popover.js";
import {RootMenuTriggerStateContext as $113aa0613e727c6c$export$795aec4671cbae19} from "./Menu.js";
import {useDialog as $bX1PO$useDialog} from "react-aria/useDialog";
import {filterDOMProps as $bX1PO$filterDOMProps} from "react-aria/private/utils/filterDOMProps";
import {mergeProps as $bX1PO$mergeProps} from "react-aria/mergeProps";
import {PressResponder as $bX1PO$PressResponder} from "react-aria/private/interactions/PressResponder";
import $bX1PO$react, {createContext as $bX1PO$createContext, useRef as $bX1PO$useRef, useState as $bX1PO$useState, useCallback as $bX1PO$useCallback, forwardRef as $bX1PO$forwardRef, useContext as $bX1PO$useContext} from "react";
import {useId as $bX1PO$useId} from "react-aria/useId";
import {useMenuTriggerState as $bX1PO$useMenuTriggerState} from "react-stately/useMenuTriggerState";
import {useOverlayTrigger as $bX1PO$useOverlayTrigger} from "react-aria/useOverlayTrigger";
import {useResizeObserver as $bX1PO$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 $acf8e70c2f419f18$export$8b93a07348a7730c = /*#__PURE__*/ (0, $bX1PO$createContext)(null);
const $acf8e70c2f419f18$export$d2f961adcb0afbe = /*#__PURE__*/ (0, $bX1PO$createContext)(null);
function $acf8e70c2f419f18$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, $bX1PO$useMenuTriggerState)(props);
    let buttonRef = (0, $bX1PO$useRef)(null);
    let { triggerProps: triggerProps, overlayProps: overlayProps } = (0, $bX1PO$useOverlayTrigger)({
        type: 'dialog'
    }, state, buttonRef);
    // Allows popover width to match trigger element
    let [buttonWidth, setButtonWidth] = (0, $bX1PO$useState)(null);
    let onResize = (0, $bX1PO$useCallback)(()=>{
        if (buttonRef.current) setButtonWidth(buttonRef.current.offsetWidth + 'px');
    }, [
        buttonRef
    ]);
    (0, $bX1PO$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, $bX1PO$useId)();
    overlayProps['aria-labelledby'] = triggerProps.id;
    return /*#__PURE__*/ (0, $bX1PO$react).createElement((0, $b7b7a92703138c9b$export$2881499e37b75b9a), {
        values: [
            [
                $acf8e70c2f419f18$export$d2f961adcb0afbe,
                state
            ],
            [
                (0, $113aa0613e727c6c$export$795aec4671cbae19),
                state
            ],
            [
                $acf8e70c2f419f18$export$8b93a07348a7730c,
                overlayProps
            ],
            [
                (0, $03df2f2d3cffb62f$export$9b9a0cd73afb7ca4),
                {
                    trigger: 'DialogTrigger',
                    triggerRef: buttonRef,
                    'aria-labelledby': overlayProps['aria-labelledby'],
                    style: {
                        '--trigger-width': buttonWidth
                    }
                }
            ]
        ]
    }, /*#__PURE__*/ (0, $bX1PO$react).createElement((0, $bX1PO$PressResponder), {
        ...triggerProps,
        ref: buttonRef,
        isPressed: state.isOpen
    }, props.children));
}
const $acf8e70c2f419f18$export$3ddf2d174ce01153 = /*#__PURE__*/ (0, $bX1PO$forwardRef)(function Dialog(props, ref) {
    let originalAriaLabelledby = props['aria-labelledby'];
    [props, ref] = (0, $b7b7a92703138c9b$export$29f1550f4b0d4415)(props, ref, $acf8e70c2f419f18$export$8b93a07348a7730c);
    let { dialogProps: dialogProps, titleProps: titleProps } = (0, $bX1PO$useDialog)({
        ...props,
        // Only pass aria-labelledby from props, not context.
        // Context is used as a fallback below.
        'aria-labelledby': originalAriaLabelledby
    }, ref);
    let state = (0, $bX1PO$useContext)($acf8e70c2f419f18$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, $b7b7a92703138c9b$export$4d86445c2cf5e3)({
        defaultClassName: 'react-aria-Dialog',
        className: props.className,
        style: props.style,
        children: props.children,
        values: {
            close: (state === null || state === void 0 ? void 0 : state.close) || (()=>{})
        }
    });
    let DOMProps = (0, $bX1PO$filterDOMProps)(props, {
        global: true
    });
    return /*#__PURE__*/ (0, $bX1PO$react).createElement((0, $b7b7a92703138c9b$export$df3a06d6289f983e).section, {
        ...(0, $bX1PO$mergeProps)(DOMProps, renderProps, dialogProps),
        render: props.render,
        ref: ref,
        slot: props.slot || undefined
    }, /*#__PURE__*/ (0, $bX1PO$react).createElement((0, $b7b7a92703138c9b$export$2881499e37b75b9a), {
        values: [
            [
                (0, $cd487658cd83358d$export$d688439359537581),
                {
                    slots: {
                        [(0, $b7b7a92703138c9b$export$c62b8e45d58ddad9)]: {},
                        title: {
                            ...titleProps,
                            level: 2
                        }
                    }
                }
            ],
            [
                (0, $fc203795b9b363cd$export$24d547caef80ccd1),
                {
                    slots: {
                        [(0, $b7b7a92703138c9b$export$c62b8e45d58ddad9)]: {},
                        close: {
                            onPress: ()=>state === null || state === void 0 ? void 0 : state.close()
                        }
                    }
                }
            ]
        ]
    }, renderProps.children));
});


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