import React, { forwardRef, useMemo } from "react";
import cls from "classnames";
import { useForkRef, useFocusTrap } from "../../hooks";
import { getSizeValueFromProp, safeUseId } from "../../utils";
import { usePopupContext } from "../Popup";
import { Overlay } from "../Overlay";
import { DEFAULT_Z_INDEX } from "../Popup/utils";
import { classes, tokens } from "./Drawer.tokens";
import { base as viewCSS } from "./variations/_view/base";
import { base as sizeCSS } from "./variations/_size/base";
import { base as borderRadiusCSS } from "./variations/_borderRadius/base";
import { StyledPopup, StyledPanel } from "./Drawer.styles";
import { getIdLastDrawer } from "./DrawerContext";
import { useDrawer } from "./hooks";
// issue #823
export const drawerRoot = (Root)=>/*#__PURE__*/ forwardRef(({ id, zIndex, popupInfo, withBlur, children, view, size, width, height, isOpen, opened, initialFocusRef, focusAfterRef, animationInfo, className, customBackgroundColor, customContentBackgroundColor, onClose, onOverlayClick, onEscKeyDown, offset, frame, positionFixed = false, borderRadius = 'none', placement = 'right', closeOnEsc = true, closeOnOverlayClick = true, asModal = true, draggable, ...rest }, outerRef)=>{
        const innerIsOpen = Boolean(isOpen || opened);
        const trapRef = useFocusTrap(true, initialFocusRef, focusAfterRef);
        const popupController = usePopupContext();
        const innerRef = useForkRef(trapRef, outerRef);
        const uniqId = safeUseId();
        const innerId = id || uniqId;
        const overlayBackgroundToken = withBlur ? `var(${tokens.drawerOverlayWithBlurColor})` : `var(${tokens.drawerOverlayColor})`;
        const innerWidth = width ? getSizeValueFromProp(width) : '100%';
        const innerHeight = height ? getSizeValueFromProp(height) : '100%';
        const placementClass = placement ? classes[`${placement}Placement`] : undefined;
        const { drawerInfo } = useDrawer({
            id: innerId,
            isOpen: innerIsOpen,
            closeOnEsc,
            onEscKeyDown,
            onClose,
            popupInfo,
            disableScroll: asModal
        });
        const transparent = useMemo(()=>getIdLastDrawer(Array.from(popupController.items.values())) !== innerId, [
            innerId,
            popupController.items
        ]);
        const onDrawerOverlayKeyDown = (event)=>{
            if (onOverlayClick) {
                onOverlayClick(event);
                return;
            }
            if (onClose) {
                onClose();
            }
        };
        return /*#__PURE__*/ React.createElement(StyledPopup, {
            id: innerId,
            ref: asModal ? innerRef : outerRef,
            className: cls(placementClass, className, {
                [classes.enterCustomAnimation]: animationInfo?.enter,
                [classes.exitCustomAnimation]: animationInfo?.exit
            }),
            opened: innerIsOpen,
            zIndex: zIndex,
            placement: placement,
            popupInfo: drawerInfo,
            frame: frame,
            positionFixed: positionFixed,
            width: innerWidth,
            height: innerHeight,
            offset: offset,
            withAnimation: true,
            drawerAnimationInfo: animationInfo,
            overlay: asModal && /*#__PURE__*/ React.createElement(Root, {
                view: view
            }, /*#__PURE__*/ React.createElement(Overlay, {
                className: classes.overlay,
                zIndex: zIndex || DEFAULT_Z_INDEX,
                backgroundColorProperty: overlayBackgroundToken,
                withBlur: withBlur,
                transparent: transparent,
                isClickable: closeOnOverlayClick,
                onOverlayClick: onDrawerOverlayKeyDown
            })),
            ...rest
        }, /*#__PURE__*/ React.createElement(Root, {
            className: classes.panel,
            view: view,
            size: size,
            style: {
                width: innerWidth,
                height: innerHeight
            },
            borderRadius: borderRadius
        }, /*#__PURE__*/ React.createElement(StyledPanel, {
            width: innerWidth,
            height: innerHeight,
            customBackgroundColor: customBackgroundColor,
            customContentBackgroundColor: customContentBackgroundColor
        }, children)));
    });
export const drawerConfig = {
    name: 'Drawer',
    tag: 'div',
    layout: drawerRoot,
    base: '',
    variations: {
        view: {
            css: viewCSS
        },
        size: {
            css: sizeCSS
        },
        borderRadius: {
            css: borderRadiusCSS
        }
    },
    defaults: {
        view: 'default',
        size: 'm',
        borderRadius: 'none'
    }
};
