import { _ as __rest } from './tslib.es6-es.js';
import React__default from 'react';
import classnames from 'classnames';
import { n as normalizeBaseUiButtonProps, i as isAtlantisButtonRenderTarget, s as stripNativeTypeFromButtonRenderTarget, r as renderAtlantisButtonTarget } from './buttonRenderAdapter-es.js';
import { B as Button } from './Button-es.js';
import { D as DrawerTrigger, a as DrawerRoot, b as DrawerPortal, c as DrawerBackdrop, d as DrawerPopup, S as ScrollAreaRoot, e as ScrollAreaViewport, f as DrawerContent } from './ScrollAreaViewport-es.js';

var styles = {"backdrop":"Qf3IFhKJwQ0-","popup":"_868PDAA3U2M-","content":"eKuxReHyju8-","scrollRoot":"V--tRnN-P0I-","scrollArea":"_1I0eaFciaWo-","bottomFade":"BYEzqcZUGls-","spinning":"IJxq5fm5qdw-"};

// `resolveBottomSheetTriggerRender` so an Atlantis `<Button>` works as a trigger
// via `children` or `render`. Raw text children are wrapped in a default Button.
function resolveBottomSheetTriggerRender({ children, render, }) {
    if (render) {
        if (typeof render === "function") {
            return (props) => {
                const renderedElement = render(normalizeBaseUiButtonProps(props));
                return isAtlantisButtonRenderTarget(renderedElement)
                    ? stripNativeTypeFromButtonRenderTarget(renderedElement)
                    : renderedElement;
            };
        }
        return isAtlantisButtonRenderTarget(render)
            ? renderAtlantisButtonTarget(render)
            : render;
    }
    if (isAtlantisButtonRenderTarget(children)) {
        return renderAtlantisButtonTarget(children);
    }
    if (React__default.isValidElement(children)) {
        return children;
    }
    return renderAtlantisButtonTarget(React__default.createElement(Button, { type: "primary" },
        React__default.createElement(Button.Label, null, children)));
}

/**
 * Whether `BottomSheet.Popup` renders its own built-in scroll area (with the
 * bottom fade). Defaults to `true` — the popup wraps its `children` in a
 * scrollable viewport. A consumer that manages its own scroll region (e.g.
 * `Dialog`, which pins a header/footer outside a single scroll area) provides
 * `false` to opt out. Mirrors how `Dialog.Body` decides whether to wrap itself
 * via context rather than a prop.
 */
const BottomSheetScrollAreaContext = React__default.createContext(true);
function BottomSheetTrigger(_a) {
    var { children, render, className } = _a, triggerProps = __rest(_a, ["children", "render", "className"]);
    return (React__default.createElement(DrawerTrigger, Object.assign({}, triggerProps, { className: className, render: resolveBottomSheetTriggerRender({ children, render }) })));
}
/**
 * Thin pass-through to the Drawer root: a single owner manages open state, and
 * all root props — `handle`, `modal`, `disablePointerDismissal`, the payload
 * render-function `children`, etc. — reach BaseUI unchanged.
 */
function BottomSheetRoot(_a) {
    var { children } = _a, rootProps = __rest(_a, ["children"]);
    return React__default.createElement(DrawerRoot, Object.assign({}, rootProps), children);
}
function BottomSheetPopup(_a) {
    var { children, className, initialFocus, finalFocus, ref } = _a, popupProps = __rest(_a, ["children", "className", "initialFocus", "finalFocus", "ref"]);
    // Defaults to wrapping; a consumer managing its own scroll region provides
    // `BottomSheetScrollAreaContext` = false.
    const renderScrollArea = React__default.useContext(BottomSheetScrollAreaContext);
    return (React__default.createElement(DrawerPortal, null,
        React__default.createElement(DrawerBackdrop, { forceRender: true, className: styles.backdrop, "data-testid": "ATL-BottomSheet-Backdrop" }),
        React__default.createElement(DrawerPopup, Object.assign({}, popupProps, { ref: ref, className: classnames(styles.popup, className), initialFocus: initialFocus, finalFocus: finalFocus }), !renderScrollArea ? (
        // The consumer manages its own scroll region (e.g. Dialog pins a
        // header/footer outside a single scroll area) — often by composing
        // `BottomSheet.ScrollArea` itself.
        children) : (React__default.createElement(BottomSheetScrollArea, null, children)))));
}
/**
 * The sheet's scroll region: a styled BaseUI `ScrollArea` (native scrollbar) with
 * a bottom fade that hints at more content while the viewport can scroll further.
 * `BottomSheet.Popup` renders this automatically unless a consumer opts out via
 * `BottomSheetScrollAreaContext` and composes it (or its own scroll region)
 * directly.
 */
function BottomSheetScrollArea({ children, className, style, }) {
    return (React__default.createElement(ScrollAreaRoot, { className: classnames(styles.scrollRoot, className), style: style },
        React__default.createElement(ScrollAreaViewport, { className: styles.scrollArea, "data-testid": "ATL-BottomSheet-ScrollArea", 
            // BaseUI sets overflow: scroll in the style prop by default. This is to override that because adding it to the className doesn't work.
            style: { overflow: "auto" } }, children),
        React__default.createElement("div", { "aria-hidden": "true", className: styles.bottomFade, "data-testid": "ATL-BottomSheet-Fade" })));
}
function BottomSheetContent(_a) {
    var { children, className, ref } = _a, contentProps = __rest(_a, ["children", "className", "ref"]);
    return (React__default.createElement(DrawerContent, Object.assign({}, contentProps, { ref: ref, className: classnames(styles.content, className) }), children));
}
function BottomSheetComponent(_a) {
    var { children, className, initialFocus, finalFocus, open, defaultOpen, onOpenChange, ref } = _a, contentProps = __rest(_a, ["children", "className", "initialFocus", "finalFocus", "open", "defaultOpen", "onOpenChange", "ref"]);
    return (React__default.createElement(BottomSheetRoot, { open: open, defaultOpen: defaultOpen, onOpenChange: onOpenChange },
        React__default.createElement(BottomSheetPopup, { initialFocus: initialFocus, finalFocus: finalFocus },
            React__default.createElement(BottomSheetContent, Object.assign({}, contentProps, { ref: ref, className: className }), children))));
}
const BottomSheet = Object.assign(BottomSheetComponent, {
    Root: BottomSheetRoot,
    Trigger: BottomSheetTrigger,
    Popup: BottomSheetPopup,
    Content: BottomSheetContent,
    ScrollArea: BottomSheetScrollArea,
});

export { BottomSheetScrollAreaContext as B, BottomSheet as a };
