"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = (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 = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (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__, {
    SNIPPET_PREFIX: ()=>SNIPPET_PREFIX,
    UserSettingsManager: ()=>UserSettingsManager,
    USER_SETTINGS_PATH: ()=>USER_SETTINGS_PATH,
    USER_SETTINGS_KEY: ()=>USER_SETTINGS_KEY
});
const external_react_namespaceObject = require("react");
const external_lodash_namespaceObject = require("lodash");
const external_react_router_dom_namespaceObject = require("react-router-dom");
const index_js_namespaceObject = require("../icon/index.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;
}
const USER_SETTINGS_KEY = 'profilers.';
const USER_SETTINGS_PATH = '/profilers/';
const SNIPPET_PREFIX = 'pm.';
class UserSettingsManager {
    setAclSnippets(aclSnippets) {
        this.aclSnippets = aclSnippets;
    }
    getAclSnippet(name) {
        const setting = this.settings[name];
        return (null == setting ? void 0 : setting.aclSnippet) ? setting.aclSnippet : "".concat(SNIPPET_PREFIX).concat(name);
    }
    getRouteName(name) {
        return "".concat(USER_SETTINGS_KEY).concat(name);
    }
    getRoutePath(name) {
        return "".concat(USER_SETTINGS_PATH).concat(name.replaceAll('.', '/'));
    }
    add(name, options) {
        const nameArr = name.split('.');
        const topLevelName = nameArr[0];
        this.settings[name] = {
            ...this.settings[name],
            Component: external_react_router_dom_namespaceObject.Outlet,
            ...options,
            name,
            topLevelName: options.topLevelName || topLevelName
        };
        if (nameArr.length > 1) (0, external_lodash_namespaceObject.set)(this.settings, nameArr.join('.children.'), this.settings[name]);
        this.app.router.add(this.getRouteName(name), {
            path: this.getRoutePath(name),
            Component: this.settings[name].Component
        });
    }
    remove(name) {
        Object.keys(this.settings).forEach((key)=>{
            if (key.startsWith(name)) {
                delete this.settings[key];
                this.app.router.remove("".concat(USER_SETTINGS_KEY).concat(key));
            }
        });
    }
    hasAuth(name) {
        if (this.aclSnippets.includes("!".concat(this.getAclSnippet('*')))) return false;
        return false === this.aclSnippets.includes("!".concat(this.getAclSnippet(name)));
    }
    getSetting(name) {
        return this.settings[name];
    }
    has(name) {
        const hasAuth = this.hasAuth(name);
        if (!hasAuth) return false;
        return !!this.getSetting(name);
    }
    get(name) {
        let filterAuth = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : true;
        const isAllow = this.hasAuth(name);
        const userSetting = this.getSetting(name);
        if (filterAuth && !isAllow || !userSetting) return null;
        const children = Object.keys(userSetting.children || {}).sort((a, b)=>a.localeCompare(b)).map((key)=>this.get(userSetting.children[key].name, filterAuth)).filter(Boolean).sort((a, b)=>(a.sort || 0) - (b.sort || 0));
        const { title, icon, aclSnippet, ...others } = userSetting;
        return {
            ...others,
            aclSnippet: this.getAclSnippet(name),
            title,
            isAllow,
            label: title,
            icon: 'string' == typeof icon ? (0, external_react_namespaceObject.createElement)(index_js_namespaceObject.Icon, {
                type: icon
            }) : icon,
            path: this.getRoutePath(name),
            key: name,
            children: children.length ? children : void 0
        };
    }
    getList() {
        let filterAuth = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : true;
        const cacheKey = JSON.stringify(filterAuth);
        if (this.cachedList[cacheKey]) return this.cachedList[cacheKey];
        return this.cachedList[cacheKey] = Array.from(new Set(Object.values(this.settings).map((item)=>item.topLevelName))).sort((a, b)=>a.localeCompare(b)).map((name)=>this.get(name, filterAuth)).filter(Boolean).sort((a, b)=>(a.sort || 0) - (b.sort || 0));
    }
    getAclSnippets() {
        return Object.keys(this.settings).map((name)=>this.getAclSnippet(name));
    }
    constructor(_userSettings, app){
        _define_property(this, "settings", {});
        _define_property(this, "aclSnippets", []);
        _define_property(this, "app", void 0);
        _define_property(this, "cachedList", {});
        this.app = app;
        Object.entries(_userSettings || {}).forEach((param)=>{
            let [name, userSettingOptions] = param;
            this.add(name, userSettingOptions);
        });
    }
}
exports.SNIPPET_PREFIX = __webpack_exports__.SNIPPET_PREFIX;
exports.USER_SETTINGS_KEY = __webpack_exports__.USER_SETTINGS_KEY;
exports.USER_SETTINGS_PATH = __webpack_exports__.USER_SETTINGS_PATH;
exports.UserSettingsManager = __webpack_exports__.UserSettingsManager;
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
    "SNIPPET_PREFIX",
    "USER_SETTINGS_KEY",
    "USER_SETTINGS_PATH",
    "UserSettingsManager"
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
Object.defineProperty(exports, '__esModule', {
    value: true
});
