'use strict';

var tslib_es6 = require('./tslib.es6-cjs.js');
var React = require('react');
var classnames = require('classnames');
var buttonRenderAdapter = require('./buttonRenderAdapter-cjs.js');
var Button = require('./Button-cjs.js');
var ScrollAreaViewport = require('./ScrollAreaViewport-cjs.js');

var styles = {"backdrop":"Qf3IFhKJwQ0-","viewport":"U069n8h0urk-","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(buttonRenderAdapter.normalizeBaseUiButtonProps(props));
                return buttonRenderAdapter.isAtlantisButtonRenderTarget(renderedElement)
                    ? buttonRenderAdapter.stripNativeTypeFromButtonRenderTarget(renderedElement)
                    : renderedElement;
            };
        }
        return buttonRenderAdapter.isAtlantisButtonRenderTarget(render)
            ? buttonRenderAdapter.renderAtlantisButtonTarget(render)
            : render;
    }
    if (buttonRenderAdapter.isAtlantisButtonRenderTarget(children)) {
        return buttonRenderAdapter.renderAtlantisButtonTarget(children);
    }
    if (React.isValidElement(children)) {
        return children;
    }
    return buttonRenderAdapter.renderAtlantisButtonTarget(React.createElement(Button.Button, { type: "primary" },
        React.createElement(Button.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.createContext(true);
function BottomSheetTrigger(_a) {
    var { children, render, className } = _a, triggerProps = tslib_es6.__rest(_a, ["children", "render", "className"]);
    return (React.createElement(ScrollAreaViewport.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 = tslib_es6.__rest(_a, ["children"]);
    return React.createElement(ScrollAreaViewport.DrawerRoot, Object.assign({}, rootProps), children);
}
function BottomSheetPopup(_a) {
    var { children, className, initialFocus, finalFocus, ref } = _a, popupProps = tslib_es6.__rest(_a, ["children", "className", "initialFocus", "finalFocus", "ref"]);
    // Defaults to wrapping; a consumer managing its own scroll region provides
    // `BottomSheetScrollAreaContext` = false.
    const renderScrollArea = React.useContext(BottomSheetScrollAreaContext);
    return (React.createElement(ScrollAreaViewport.DrawerPortal, null,
        React.createElement(ScrollAreaViewport.DrawerBackdrop, { forceRender: true, className: styles.backdrop, "data-testid": "ATL-BottomSheet-Backdrop" }),
        React.createElement(ScrollAreaViewport.DrawerViewport, { className: styles.viewport, "data-testid": "ATL-BottomSheet-Viewport" },
            React.createElement(ScrollAreaViewport.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.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.createElement(ScrollAreaViewport.ScrollAreaRoot, { className: classnames(styles.scrollRoot, className), style: style, "data-testid": "ATL-BottomSheet-ScrollRegion" },
        React.createElement(ScrollAreaViewport.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.createElement("div", { "aria-hidden": "true", className: styles.bottomFade, "data-testid": "ATL-BottomSheet-Fade" })));
}
function BottomSheetContent(_a) {
    var { children, className, ref } = _a, contentProps = tslib_es6.__rest(_a, ["children", "className", "ref"]);
    return (React.createElement(ScrollAreaViewport.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 = tslib_es6.__rest(_a, ["children", "className", "initialFocus", "finalFocus", "open", "defaultOpen", "onOpenChange", "ref"]);
    return (React.createElement(BottomSheetRoot, { open: open, defaultOpen: defaultOpen, onOpenChange: onOpenChange },
        React.createElement(BottomSheetPopup, { initialFocus: initialFocus, finalFocus: finalFocus },
            React.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,
});

exports.BottomSheet = BottomSheet;
exports.BottomSheetScrollAreaContext = BottomSheetScrollAreaContext;
