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__components_index_mjs__ from "./components/index.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 {
    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() {
        const { type = 'browser', ...opts } = this.options;
        const Routers = {
            hash: __WEBPACK_EXTERNAL_MODULE_react_router_dom__.HashRouter,
            browser: __WEBPACK_EXTERNAL_MODULE_react_router_dom__.BrowserRouter,
            memory: __WEBPACK_EXTERNAL_MODULE_react_router_dom__.MemoryRouter
        };
        const ReactRouter = Routers[type];
        const RenderRoutes = ()=>{
            const routes = this.getRoutesTree();
            const element = (0, __WEBPACK_EXTERNAL_MODULE_react_router_dom__.useRoutes)(routes);
            return element;
        };
        const RenderRouter = (param)=>{
            let { BaseLayout = __WEBPACK_EXTERNAL_MODULE__components_index_mjs__.BlankComponent } = param;
            return /*#__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)(ReactRouter, {
                    ...opts,
                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(BaseLayout, {
                        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(RenderRoutes, {})
                    })
                })
            });
        };
        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);
        this.options = options;
        this.app = app;
    }
}
function createRouterManager(options, app) {
    return new RouterManager(options, app);
}
export { RouterManager, createRouterManager };
