import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
import { createPortal } from 'react-dom';
import Button from '../Button/Button.js';
import classNames from '../../_virtual/index.js';
import { useRef, useState, useEffect, useContext } from 'react';
import Icon from '../Icon/Icon.js';
import useEscapeKey from '../../hooks/keyboardevents/useEscKeyEvent.js';
import { ThemeContext } from '../ThemeProvider/ThemeProvider.js';
import Tooltip from '../Tooltip/Tooltip.js';
import useClickOutside from '../../hooks/useClickOutside.js';
import { cleanAllTooltips } from '../../utils/cleanAllTooltips/cleanAllTooltips.js';

const Drawer = ({ isOpen = true, children = 'Drawer content area', onClose = () => { }, onBackButtonClick = () => { }, title = 'drawer', primaryButtonProps = {}, secondaryButtonProps = {}, leftPrimaryButtonProps = {}, leftSecondaryButtonProps = {}, showEditButton = false, showTransition = true, onEdit = () => { }, onCollapse = () => { }, overlay = false, isFooterRequired = true, footerContent = null, size = 'medium', _isExpanded = false, isBackButtonVisible = false, _isCloseModalButtonVisible = true, showHeader = true, isScrollBar = true, backButtonIcon, onCloseIconClick, customHeader, customFooter, leftTertiaryButtonProps = {}, rightTertiaryButtonProps = {}, zIndex, top = '89px', height, width, right = '5px', overflow, isClickOutside, ignoreRefs, noHeaderBorder = false, direction = 'right', }) => {
    const containerRef = useRef(null);
    useClickOutside(containerRef, () => {
        cleanAllTooltips();
        const openDrawers = document.querySelectorAll('.ff-drawer');
        const lastDrawer = openDrawers[openDrawers.length - 1];
        if (lastDrawer &&
            lastDrawer.contains(containerRef.current) &&
            isClickOutside) {
            onClose();
        }
    }, ignoreRefs, { ignoreSelectors: ['[data-tippy-root]'] });
    const [isExpanded, setIsExpanded] = useState(false);
    const [delayedOpen, setDelayedOpen] = useState(false);
    // useKeyboardActions([{ key: 'Escape', action: onClose }]);
    const handelClose = useEscapeKey('Escape');
    handelClose(() => {
        cleanAllTooltips();
        onClose();
    });
    const toggleExpand = () => {
        setIsExpanded((prev) => {
            onCollapse(!prev);
            return !prev;
        });
    };
    const onCancel = () => {
        if (secondaryButtonProps.onClick) {
            secondaryButtonProps.onClick();
        }
        else {
            onClose();
        }
    };
    useEffect(() => {
        closeModal();
    }, [isOpen]);
    const closeModal = () => {
        if (!isOpen) {
            setTimeout(() => {
                return null;
            }, 1000);
        }
    };
    useEffect(() => {
        if (isOpen) {
            cleanAllTooltips();
            setTimeout(() => setDelayedOpen(true), 200);
        }
        else {
            setDelayedOpen(false);
        }
    }, [isOpen]);
    useEffect(() => {
        const handleKeyDown = (e) => {
            if (e.key === 'Enter' &&
                isOpen &&
                primaryButtonProps?.onClick &&
                !primaryButtonProps.disabled) {
                const activeElement = document.activeElement;
                const isSpecialInput = ['ff-add-module-input', 'ff-search-input'].some((className) => activeElement?.classList?.contains(className) ||
                    activeElement?.closest(`.${className}`));
                if (isSpecialInput) {
                    return;
                }
                primaryButtonProps.onClick();
            }
        };
        document.addEventListener('keydown', handleKeyDown);
        return () => {
            document.removeEventListener('keydown', handleKeyDown);
        };
    }, [isOpen, primaryButtonProps]);
    const drawerSize = isExpanded ? 'x-large' : size;
    const themeContext = useContext(ThemeContext);
    const currentTheme = themeContext?.currentTheme;
    const transitionDuration = !showTransition ? '0s' : '0.4s';
    const getDefaultZIndex = (size) => {
        const zIndexMap = {
            small: 1100,
            medium: 1050,
            large: 1000,
            'x-large': 950,
        };
        return zIndexMap[size] ?? 1050;
    };
    const computedZIndex = zIndex ?? getDefaultZIndex(size);
    return createPortal(jsxs("div", { className: classNames('ff-drawer-container', currentTheme), children: [overlay && (jsx("div", { className: 'ff-overlay', style: { zIndex: computedZIndex } })), jsxs("div", { ref: containerRef, className: classNames('ff-drawer', `ff-drawer--${drawerSize}`, {
                    'ff-drawer--open': delayedOpen,
                    'ff-drawer--left': direction === 'left',
                }), style: {
                    zIndex: computedZIndex,
                    top: top,
                    height,
                    width,
                    ...(direction === 'left'
                        ? { left: '5px', right: 'auto' }
                        : { right: right ?? '5px' }),
                    overflow,
                    transition: `all ${transitionDuration} ease-in-out`,
                }, children: [showHeader && (jsx("div", { className: classNames('ff-drawer-header', {
                            'ff-custom-header': customHeader,
                            'no-header-border': noHeaderBorder,
                        }), style: { zIndex: computedZIndex }, children: customHeader ? (jsx("div", { className: classNames('ff-custom-header', {
                                'no-header-border': noHeaderBorder,
                            }), children: customHeader })) : (jsxs("div", { className: "ff-drawer-action-section", children: [jsxs("div", { className: "ff-action-button", children: [showEditButton && (jsxs("button", { className: "ff-expand-collapse-button", onClick: onEdit, children: [jsx(Icon, { name: "logo", height: 16, width: 16 }), "Edit"] })), _isExpanded && (jsx(Tooltip, { title: isExpanded ? 'Minimize' : 'Maximize', placement: "bottom", children: jsx("div", { className: "ff-expand-collapse-button", onClick: toggleExpand, children: jsx(Icon, { name: isExpanded ? 'maximize_tree' : 'minimize_tree', height: 16, width: 16, hoverEffect: true, cursorType: "pointer" }) }) })), isBackButtonVisible &&
                                            (backButtonIcon || (jsx(Tooltip, { title: "Back", children: jsx(Icon, { name: "back_icon", height: 16, width: 16, hoverEffect: true, onClick: onBackButtonClick }) }))), title && jsx("div", { className: "ff-drawer-title", children: title })] }), _isCloseModalButtonVisible && (jsx("div", { className: "ff-close-icon", children: jsx(Tooltip, { title: "Close", children: jsx(Icon, { name: "drawer_icon_close", onClick: onCloseIconClick || onClose, height: 16, width: 16, color: "var(--tabs-label-active-color)", cursorType: "pointer" }) }) }))] })) })), jsxs("div", { className: classNames({
                            'ff-drawer-body-with-scroll': isScrollBar,
                            'ff-drawer-body': !isScrollBar,
                            'no-footer': !isFooterRequired,
                        }), children: [' ', children] }), isFooterRequired && (jsx("div", { className: classNames('ff-drawer-footer', {
                            'ff-custom-footer': customFooter,
                        }), style: {
                            zIndex: computedZIndex,
                        }, children: customFooter || footerContent || (jsxs(Fragment, { children: [jsxs("div", { className: "button-container", children: [leftSecondaryButtonProps.label && (jsx(Button, { onClick: leftPrimaryButtonProps.onClick, variant: "secondary", transparentBackground: true, ...leftSecondaryButtonProps })), leftPrimaryButtonProps.label && (jsx(Button, { onClick: leftPrimaryButtonProps.onClick, variant: "primary", transparentBackground: true, ...leftPrimaryButtonProps })), leftTertiaryButtonProps?.label && (jsx(Button, { onClick: leftTertiaryButtonProps.onClick, variant: "tertiary", transparentBackground: true, ...leftTertiaryButtonProps }))] }), jsxs("div", { className: "button-container", children: [rightTertiaryButtonProps?.label && (jsx(Button, { onClick: rightTertiaryButtonProps.onClick, variant: "tertiary", transparentBackground: true, ...rightTertiaryButtonProps })), secondaryButtonProps.label && (jsx(Button, { onClick: onCancel, variant: "secondary", transparentBackground: true, ...secondaryButtonProps })), primaryButtonProps.label && (jsx(Button, { onClick: primaryButtonProps.onClick, variant: "primary", transparentBackground: true, ...primaryButtonProps }))] })] })) }))] })] }), document.body);
};

export { Drawer as default };
//# sourceMappingURL=Drawer.js.map
