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__icon_index_mjs__ from "../icon/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;
}
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: __WEBPACK_EXTERNAL_MODULE_react_router_dom__.Outlet,
            ...options,
            name,
            topLevelName: options.topLevelName || topLevelName
        };
        if (nameArr.length > 1) (0, __WEBPACK_EXTERNAL_MODULE_lodash__.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, __WEBPACK_EXTERNAL_MODULE_react__.createElement)(__WEBPACK_EXTERNAL_MODULE__icon_index_mjs__.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);
        });
    }
}
export { ADMIN_SETTINGS_KEY, ADMIN_SETTINGS_PATH, SNIPPET_PREFIX, SystemSettingsManager };
