import React, { useMemo, useState } from 'react';
import { Route, Router, Routes as ReactRouters, useNavigate, } from 'react-router-dom';
import { useWixPatternsContainer } from '@wix/bex-core/react';
import { WixPatternsRouterContext } from '../../providers/WixPatternsRouterProvider';
import { PatternsReactRoute } from './PatternsReactRoute';
import { createPlatformizedNavigator } from './createPlatformizedNavigator';
import { WixPatternsRouterState } from '../../state/WixPatternsRouterState';
const convertToRoute = (child) => {
    if (React.isValidElement(child) && child.type === PatternsReactRoute) {
        const routeProps = child.props;
        return React.createElement(Route, { ...routeProps });
    }
    return child;
};
const Routers = ({ children }) => {
    const navigate = useNavigate();
    const container = useWixPatternsContainer();
    const [routerState] = useState(() => new WixPatternsRouterState({ container, navigate }));
    return (React.createElement(WixPatternsRouterContext.Provider, { value: routerState },
        React.createElement(ReactRouters, null, React.Children.map(children, convertToRoute))));
};
export const PatternsReactRouter = ({ children, }) => {
    const container = useWixPatternsContainer();
    const pageLocation = container.usePageLocation?.();
    if (!pageLocation) {
        throw new Error('It appears that `PatternsReactRouter` is being used incorrectly. Please refer to the documentation to ensure proper implementation.');
    }
    const navigator = useMemo(() => createPlatformizedNavigator({ pageLocation, container }), [pageLocation]);
    return (React.createElement(Router, { navigator: navigator, location: pageLocation },
        React.createElement(Routers, null, children)));
};
//# sourceMappingURL=PatternsReactRouter.js.map