import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE_react_router_dom__ from "react-router-dom";
import * as __WEBPACK_EXTERNAL_MODULE__variables_VariablesProvider_mjs__ from "../variables/VariablesProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__components_index_mjs__ from "./components/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__CustomRouterContextProvider_mjs__ from "./CustomRouterContextProvider.mjs";
function _define_property(obj, key, value) {
    if (key in obj) Object.defineProperty(obj, key, {
        value: value,
        enumerable: true,
        configurable: true,
        writable: true
    });
    else obj[key] = value;
    return obj;
}
class RouterManager {
    get basename() {
        return this.router.basename;
    }
    get state() {
        return this.router.state;
    }
    get navigate() {
        return this.router.navigate;
    }
    getRoutesTree() {
        const routes = {};
        for (const [name, route] of Object.entries(this.routes))(0, __WEBPACK_EXTERNAL_MODULE_lodash__.set)(routes, name.split('.').join('.children.'), {
            ...(0, __WEBPACK_EXTERNAL_MODULE_lodash__.get)(routes, name.split('.').join('.children.')),
            ...route
        });
        const buildRoutesTree = (routes)=>Object.values(routes).reduce((acc, item)=>{
                if (1 === Object.keys(item).length && item.children) acc.push(...buildRoutesTree(item.children));
                else {
                    const { Component, element, children, ...reset } = item;
                    let ele = element;
                    if (Component) ele = 'string' == typeof Component ? this.app.renderComponent(Component) : /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__["default"].createElement(Component);
                    const res = {
                        ...reset,
                        element: ele,
                        children: children ? buildRoutesTree(children) : void 0
                    };
                    acc.push(res);
                }
                return acc;
            }, []);
        return buildRoutesTree(routes);
    }
    getRoutes() {
        return this.routes;
    }
    setType(type) {
        this.options.type = type;
    }
    getBasename() {
        return this.options.basename;
    }
    setBasename(basename) {
        this.options.basename = basename;
    }
    getRouterComponent(children) {
        const { type = 'browser', ...opts } = this.options;
        const routerCreators = {
            hash: __WEBPACK_EXTERNAL_MODULE_react_router_dom__.createHashRouter,
            browser: __WEBPACK_EXTERNAL_MODULE_react_router_dom__.createBrowserRouter,
            memory: __WEBPACK_EXTERNAL_MODULE_react_router_dom__.createMemoryRouter
        };
        const routes = this.getRoutesTree();
        const BaseLayoutContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)(null);
        const Provider = ()=>{
            const BaseLayout = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(BaseLayoutContext);
            return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__CustomRouterContextProvider_mjs__.CustomRouterContextProvider, {
                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(BaseLayout, {
                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE__variables_VariablesProvider_mjs__["default"], {
                        children: [
                            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_react_router_dom__.Outlet, {}),
                            children
                        ]
                    })
                })
            });
        };
        const ErrorElement = ()=>{
            const error = (0, __WEBPACK_EXTERNAL_MODULE_react_router_dom__.useRouteError)();
            throw error;
        };
        this.router = routerCreators[type]([
            {
                element: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Provider, {}),
                errorElement: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(ErrorElement, {}),
                children: routes
            }
        ], opts);
        const RenderRouter = (param)=>{
            let { BaseLayout = __WEBPACK_EXTERNAL_MODULE__components_index_mjs__.BlankComponent } = param;
            return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(BaseLayoutContext.Provider, {
                value: BaseLayout,
                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__components_index_mjs__.RouterContextCleaner, {
                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_react_router_dom__.RouterProvider, {
                        router: this.router
                    })
                })
            });
        };
        return RenderRouter;
    }
    add(name, route) {
        this.routes[name] = route;
    }
    get(name) {
        return this.routes[name];
    }
    has(name) {
        return !!this.get(name);
    }
    remove(name) {
        delete this.routes[name];
    }
    constructor(options = {}, app){
        _define_property(this, "routes", {});
        _define_property(this, "options", void 0);
        _define_property(this, "app", void 0);
        _define_property(this, "router", void 0);
        this.options = options;
        this.app = app;
        this.routes = options.routes || {};
    }
}
function createRouterManager(options, app) {
    return new RouterManager(options, app);
}
export { RouterManager, createRouterManager };
