import { _ as __rest } from './tslib.es6-es.js';
import React__default from 'react';
import classnames from 'classnames';
import { D as DrawerPortal, a as DrawerBackdrop, b as DrawerPopup, c as DrawerContent, d as DrawerRoot } from './DrawerRoot-es.js';

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

const BOTTOM_THRESHOLD = 1;
const BottomSheetContext = React__default.createContext(null);
function BottomSheetRoot(_a) {
    var { children, defaultOpen, onOpenChange, open } = _a, rootProps = __rest(_a, ["children", "defaultOpen", "onOpenChange", "open"]);
    const isControlled = open !== undefined;
    const [internalOpen, setInternalOpen] = React__default.useState(defaultOpen !== null && defaultOpen !== void 0 ? defaultOpen : false);
    const isOpen = isControlled ? open : internalOpen;
    const handleOpenChange = React__default.useCallback((nextOpen, eventDetails) => {
        if (!isControlled) {
            setInternalOpen(nextOpen);
        }
        onOpenChange === null || onOpenChange === void 0 ? void 0 : onOpenChange(nextOpen, eventDetails);
    }, [isControlled, onOpenChange]);
    return (React__default.createElement(BottomSheetContext.Provider, { value: { isOpen } },
        React__default.createElement(DrawerRoot, Object.assign({}, rootProps, { defaultOpen: defaultOpen, onOpenChange: handleOpenChange, open: open }), children)));
}
const BottomSheetPopup = React__default.forwardRef(function BottomSheetPopup(_a, ref) {
    var { children, className, initialFocus, finalFocus } = _a, popupProps = __rest(_a, ["children", "className", "initialFocus", "finalFocus"]);
    const scrollAreaRef = React__default.useRef(null);
    const [showBottomFade, setShowBottomFade] = React__default.useState(false);
    const bottomSheetContext = React__default.useContext(BottomSheetContext);
    const isOpen = bottomSheetContext === null || bottomSheetContext === void 0 ? void 0 : bottomSheetContext.isOpen;
    const syncBottomFade = React__default.useCallback(() => {
        const node = scrollAreaRef.current;
        if (!node)
            return;
        const hasOverflow = node.scrollHeight > node.clientHeight;
        const isAtBottom = node.scrollTop + node.clientHeight >=
            node.scrollHeight - BOTTOM_THRESHOLD;
        const nextShowBottomFade = hasOverflow && !isAtBottom;
        setShowBottomFade(nextShowBottomFade);
    }, []);
    const scheduleFadeUpdate = React__default.useCallback(() => {
        syncBottomFade();
        let firstAnimationFrame = 0;
        let secondAnimationFrame = 0;
        firstAnimationFrame = window.requestAnimationFrame(() => {
            syncBottomFade();
            secondAnimationFrame = window.requestAnimationFrame(() => {
                syncBottomFade();
            });
        });
        return () => {
            window.cancelAnimationFrame(firstAnimationFrame);
            window.cancelAnimationFrame(secondAnimationFrame);
        };
    }, [syncBottomFade]);
    React__default.useLayoutEffect(() => {
        if (isOpen === false) {
            setShowBottomFade(false);
            return;
        }
        return scheduleFadeUpdate();
    }, [isOpen, scheduleFadeUpdate]);
    React__default.useEffect(() => {
        const node = scrollAreaRef.current;
        if (!node || typeof ResizeObserver === "undefined")
            return;
        const resizeObserver = new ResizeObserver(syncBottomFade);
        resizeObserver.observe(node);
        return () => {
            resizeObserver.disconnect();
        };
    }, [syncBottomFade]);
    return (React__default.createElement(DrawerPortal, null,
        React__default.createElement(DrawerBackdrop, { className: styles.backdrop }),
        React__default.createElement(DrawerPopup, Object.assign({}, popupProps, { ref: ref, className: classnames(styles.popup, className), initialFocus: initialFocus, finalFocus: finalFocus }),
            React__default.createElement("div", { className: styles.scrollArea, onScroll: syncBottomFade, ref: scrollAreaRef }, children),
            showBottomFade ? (React__default.createElement("div", { "aria-hidden": "true", className: styles.bottomFade })) : null)));
});
const BottomSheetContent = React__default.forwardRef(function BottomSheetContent(_a, ref) {
    var { children, className } = _a, contentProps = __rest(_a, ["children", "className"]);
    return (React__default.createElement(DrawerContent, Object.assign({}, contentProps, { ref: ref, className: classnames(styles.content, className) }), children));
});
const BottomSheetComponent = React__default.forwardRef(function BottomSheet(_a, ref) {
    var { children, className, initialFocus, finalFocus, open, defaultOpen, onOpenChange } = _a, contentProps = __rest(_a, ["children", "className", "initialFocus", "finalFocus", "open", "defaultOpen", "onOpenChange"]);
    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,
    Popup: BottomSheetPopup,
    Content: BottomSheetContent,
});

export { BottomSheet as B };
