"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    RouterManager: ()=>RouterManager,
    createRouterManager: ()=>createRouterManager
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
var external_react_default = /*#__PURE__*/ __webpack_require__.n(external_react_namespaceObject);
const external_lodash_namespaceObject = require("lodash");
const external_react_router_dom_namespaceObject = require("react-router-dom");
const VariablesProvider_js_namespaceObject = require("../variables/VariablesProvider.js");
var VariablesProvider_js_default = /*#__PURE__*/ __webpack_require__.n(VariablesProvider_js_namespaceObject);
const index_js_namespaceObject = require("./components/index.js");
const external_CustomRouterContextProvider_js_namespaceObject = require("./CustomRouterContextProvider.js");
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, external_lodash_namespaceObject.set)(routes, name.split('.').join('.children.'), {
            ...(0, external_lodash_namespaceObject.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__*/ external_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: external_react_router_dom_namespaceObject.createHashRouter,
            browser: external_react_router_dom_namespaceObject.createBrowserRouter,
            memory: external_react_router_dom_namespaceObject.createMemoryRouter
        };
        const routes = this.getRoutesTree();
        const BaseLayoutContext = /*#__PURE__*/ (0, external_react_namespaceObject.createContext)(null);
        const Provider = ()=>{
            const BaseLayout = (0, external_react_namespaceObject.useContext)(BaseLayoutContext);
            return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_CustomRouterContextProvider_js_namespaceObject.CustomRouterContextProvider, {
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(BaseLayout, {
                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(VariablesProvider_js_default(), {
                        children: [
                            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_react_router_dom_namespaceObject.Outlet, {}),
                            children
                        ]
                    })
                })
            });
        };
        const ErrorElement = ()=>{
            const error = (0, external_react_router_dom_namespaceObject.useRouteError)();
            throw error;
        };
        this.router = routerCreators[type]([
            {
                element: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Provider, {}),
                errorElement: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(ErrorElement, {}),
                children: routes
            }
        ], opts);
        const RenderRouter = (param)=>{
            let { BaseLayout = index_js_namespaceObject.BlankComponent } = param;
            return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(BaseLayoutContext.Provider, {
                value: BaseLayout,
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_js_namespaceObject.RouterContextCleaner, {
                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_react_router_dom_namespaceObject.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);
}
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
