"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get drawerConfig () {
        return drawerConfig;
    },
    get drawerRoot () {
        return drawerRoot;
    }
});
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _classnames = /*#__PURE__*/ _interop_require_default(require("classnames"));
const _hooks = require("../../hooks");
const _utils = require("../../utils");
const _Popup = require("../Popup");
const _Overlay = require("../Overlay");
const _utils1 = require("../Popup/utils");
const _Drawertokens = require("./Drawer.tokens");
const _base = require("./variations/_view/base");
const _base1 = require("./variations/_size/base");
const _base2 = require("./variations/_borderRadius/base");
const _Drawerstyles = require("./Drawer.styles");
const _DrawerContext = require("./DrawerContext");
const _hooks1 = require("./hooks");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
const drawerRoot = (Root)=>/*#__PURE__*/ (0, _react.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 = (0, _hooks.useFocusTrap)(true, initialFocusRef, focusAfterRef);
        const popupController = (0, _Popup.usePopupContext)();
        const innerRef = (0, _hooks.useForkRef)(trapRef, outerRef);
        const uniqId = (0, _utils.safeUseId)();
        const innerId = id || uniqId;
        const overlayBackgroundToken = withBlur ? `var(${_Drawertokens.tokens.drawerOverlayWithBlurColor})` : `var(${_Drawertokens.tokens.drawerOverlayColor})`;
        const innerWidth = width ? (0, _utils.getSizeValueFromProp)(width) : '100%';
        const innerHeight = height ? (0, _utils.getSizeValueFromProp)(height) : '100%';
        const placementClass = placement ? _Drawertokens.classes[`${placement}Placement`] : undefined;
        const { drawerInfo } = (0, _hooks1.useDrawer)({
            id: innerId,
            isOpen: innerIsOpen,
            closeOnEsc,
            onEscKeyDown,
            onClose,
            popupInfo,
            disableScroll: asModal
        });
        const transparent = (0, _react.useMemo)(()=>(0, _DrawerContext.getIdLastDrawer)(Array.from(popupController.items.values())) !== innerId, [
            innerId,
            popupController.items
        ]);
        const onDrawerOverlayKeyDown = (event)=>{
            if (onOverlayClick) {
                onOverlayClick(event);
                return;
            }
            if (onClose) {
                onClose();
            }
        };
        return /*#__PURE__*/ _react.default.createElement(_Drawerstyles.StyledPopup, {
            id: innerId,
            ref: asModal ? innerRef : outerRef,
            className: (0, _classnames.default)(placementClass, className, {
                [_Drawertokens.classes.enterCustomAnimation]: animationInfo?.enter,
                [_Drawertokens.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.default.createElement(Root, {
                view: view
            }, /*#__PURE__*/ _react.default.createElement(_Overlay.Overlay, {
                className: _Drawertokens.classes.overlay,
                zIndex: zIndex || _utils1.DEFAULT_Z_INDEX,
                backgroundColorProperty: overlayBackgroundToken,
                withBlur: withBlur,
                transparent: transparent,
                isClickable: closeOnOverlayClick,
                onOverlayClick: onDrawerOverlayKeyDown
            })),
            ...rest
        }, /*#__PURE__*/ _react.default.createElement(Root, {
            className: _Drawertokens.classes.panel,
            view: view,
            size: size,
            style: {
                width: innerWidth,
                height: innerHeight
            },
            borderRadius: borderRadius
        }, /*#__PURE__*/ _react.default.createElement(_Drawerstyles.StyledPanel, {
            width: innerWidth,
            height: innerHeight,
            customBackgroundColor: customBackgroundColor,
            customContentBackgroundColor: customContentBackgroundColor
        }, children)));
    });
const drawerConfig = {
    name: 'Drawer',
    tag: 'div',
    layout: drawerRoot,
    base: '',
    variations: {
        view: {
            css: _base.base
        },
        size: {
            css: _base1.base
        },
        borderRadius: {
            css: _base2.base
        }
    },
    defaults: {
        view: 'default',
        size: 'm',
        borderRadius: 'none'
    }
};
