"use strict";
var __webpack_require__ = {};
(()=>{
    __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__, {
    ADMIN_SETTINGS_KEY: ()=>ADMIN_SETTINGS_KEY,
    ADMIN_SETTINGS_PATH: ()=>ADMIN_SETTINGS_PATH,
    SNIPPET_PREFIX: ()=>SNIPPET_PREFIX,
    SystemSettingsManager: ()=>SystemSettingsManager
});
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 ADMIN_SETTINGS_KEY = 'admin-settings.';
const ADMIN_SETTINGS_PATH = '/_admin/';
const SNIPPET_PREFIX = 'pm.';
class SystemSettingsManager {
    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(ADMIN_SETTINGS_KEY).concat(name);
    }
    getRoutePath(name) {
        return "".concat(ADMIN_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(ADMIN_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];
        const isAllow = this.hasAuth(name);
        const pluginSetting = this.getSetting(name);
        if (filterAuth && !isAllow || !pluginSetting) return null;
        const children = Object.keys(pluginSetting.children || {}).sort((a, b)=>a.localeCompare(b)).map((key)=>this.get(pluginSetting.children[key].name, filterAuth)).filter(Boolean).sort((a, b)=>(a.sort || 0) - (b.sort || 0));
        const { title, icon, aclSnippet, ...others } = pluginSetting;
        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];
        return 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(_pluginSettings, app){
        _define_property(this, "settings", {});
        _define_property(this, "aclSnippets", []);
        _define_property(this, "app", void 0);
        _define_property(this, "cachedList", {});
        this.app = app;
        Object.entries(_pluginSettings || {}).forEach((param)=>{
            let [name, pluginSettingOptions] = param;
            this.add(name, pluginSettingOptions);
        });
    }
}
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
});
