import React, { useRef, useCallback, useEffect, useState } from "react";
import { TransitionGroup } from "react-transition-group";
import Drawer from "../Drawer";
import TransitionSlide from "../../transitions/TransitionSlide";
import { useSide } from "./Provider";
export const SideWrapper = (props) => {
    // Nb Note that it's not a real queue and we need to handle where we go from each _slide_
    const { state, setSide } = useSide();
    const [queue, setQueue] = useState([]);
    const [direction, setDirection] = useState("left");
    const [transitionsEnabled, setTransitionsEnabled] = useState(false);
    const nonce = useRef(0);
    const onClose = useCallback(() => setSide(), [setSide]);
    useEffect(() => {
        setQueue(q => {
            if (!state.open || !state.Component)
                return [];
            return q.concat([{ ...state, key: nonce.current++ }]);
        });
    }, [state]);
    useEffect(() => {
        let timeout;
        if (queue.length > 1) {
            const [, ...rest] = queue;
            // eslint-disable-next-line @typescript-eslint/ban-ts-comment
            // @ts-ignore mismatch between nodejs and dom types
            timeout = setTimeout(() => {
                setQueue(rest);
                setDirection("left");
            }, 0);
        }
        return () => {
            if (timeout)
                clearTimeout(timeout);
        };
    }, [queue]);
    const wrappedOnBack = useCallback(() => {
        setDirection("right");
        const onBack = state?.props?.onBack;
        onBack && onBack();
    }, [state?.props]);
    return (React.createElement(Drawer, { ...props, isOpen: !!state.open, onClose: onClose, onBack: state?.props?.onBack ? wrappedOnBack : undefined, setTransitionsEnabled: setTransitionsEnabled, hideNavigation: false },
        React.createElement(TransitionGroup, { enter: transitionsEnabled, exit: transitionsEnabled, component: null }, queue.map(({ Component, props, key }) => (React.createElement(TransitionSlide, { key: key, direction: direction }, Component && React.createElement(Component, { ...props })))))));
};
export default SideWrapper;
//# sourceMappingURL=index.js.map