import { jsx as _jsx } from "react/jsx-runtime";
import { Router } from "react-router-dom";
import { createBrowserHistory } from "history";
import { useLayoutEffect, useRef, } from "react";
/**
 * A router component that manages navigation and route changes.
 */
export default function CustomBrowserRouter({ children, beforeRouteChange = (s) => s, }) {
    const { current: historyCustom } = useRef(createBrowserHistory());
    useLayoutEffect(() => {
        function overrideFunction(name) {
            const orig = historyCustom[name];
            historyCustom[name] = (route, state, ...args) => {
                const isReplacingAnInternalNavigation = name === "replace" && history.state?.state?.internalNavigation;
                const isARouteChange = name !== "replace";
                if ((isARouteChange || isReplacingAnInternalNavigation) && !state)
                    state = {};
                if (state)
                    state.internalNavigation =
                        name === "replace"
                            ? history.state?.state?.internalNavigation
                            : true;
                const modifiedRoute = beforeRouteChange(route, historyCustom);
                return orig(modifiedRoute, state, ...args);
            };
        }
        overrideFunction("push");
        overrideFunction("replace");
    }, [historyCustom]);
    return _jsx(Router, { history: historyCustom, children: children });
}
//# sourceMappingURL=CustomBrowserRouter.js.map