"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SettingsRoute = void 0;
var tslib_1 = require("tslib");
var react_1 = tslib_1.__importStar(require("react"));
var v_eris_1 = require("v-eris");
var content_menu_1 = require("../../placeholders/ContentMenu/content.menu");
var content_container_1 = require("../../placeholders/ContentContainer/content.container");
var use_settings_1 = require("../../utility/use.settings");
var build_1 = require("../../components/Build/build");
var SettingsRoute = function () {
    var lang = (0, react_1.useContext)(v_eris_1.LangContext);
    var qsRef = (0, react_1.useRef)({});
    var isSelf = (0, react_1.useRef)(false);
    var _a = (0, react_1.useState)(false), altered = _a[0], setAltered = _a[1];
    var build = (0, react_1.useContext)(build_1.BuildContext);
    var route = build.routes["settings"];
    var qs = build.query;
    var alterListener = function (e) {
        if (e.key != "Control")
            return;
        e.preventDefault();
        setAltered(e.type == "keydown" ? true : false);
    };
    (0, react_1.useEffect)(function () {
        document.addEventListener("keydown", alterListener);
        document.addEventListener("keyup", alterListener);
        return function () {
            document.removeEventListener("keydown", alterListener);
            document.removeEventListener("keyup", alterListener);
        };
    }, []);
    qs.section = v_eris_1.Common.string(qs.section || "");
    qs.item = v_eris_1.Common.string(qs.item || "");
    qs.type = v_eris_1.Common.string(qs.type || "");
    qs.token = v_eris_1.Common.string(qs.token || "");
    if (!route.sections[v_eris_1.Common.string(qs.section)])
        qs.section = route.list[0] ? route.list[0].key : "";
    var section = qs.section;
    var sectionItem = route.sections[section] || {};
    var sectionTable = route.table[qs.item];
    if (sectionTable && isSelf.current) {
        var length_1 = 0;
        for (var _i = 0, sectionTable_1 = sectionTable; _i < sectionTable_1.length; _i++) {
            var item = sectionTable_1[_i];
            var keySection = (0, use_settings_1.SettingsParamToKey3)(qs.item, item.key);
            var visibleSection = build.settings.getExclusionValue(keySection);
            if (!visibleSection)
                continue;
            length_1++;
        }
        ;
        if (!length_1)
            sectionTable = null;
    }
    ;
    if (!sectionTable) {
        qs.item = sectionItem.list && sectionItem.list[0] ? sectionItem.list[0].key : "";
        sectionTable = route.table[qs.item] || [];
    }
    ;
    qsRef.current = qs;
    var settingsInheritance = (0, use_settings_1.useSettings)({
        token: qs.token || "users:self",
        table: build.settingsTable,
        connectionless: true,
    });
    var preparedTypes = (0, react_1.useMemo)(function () {
        var array = [];
        var icons = {
            "configurations": react_1.default.createElement(v_eris_1.Icons.key, null),
            "roles": react_1.default.createElement(v_eris_1.Icons.users, null),
            "users": react_1.default.createElement(v_eris_1.Icons.user, null),
        };
        for (var _i = 0, _a = settingsInheritance.inheritanceStack; _i < _a.length; _i++) {
            var stack = _a[_i];
            array.push({
                token: stack.type + ":" + stack.token,
                k: stack.token,
                name: stack.name,
                icon: icons[stack.type]
            });
        }
        ;
        if (settingsInheritance.inheritanceStack.length)
            isSelf.current = !qs.token || qs.token == "self" ? true : settingsInheritance.isSelf();
        if (isSelf.current)
            array = array.length > 0 ? [array[array.length - 1]] : [];
        return array;
    }, [settingsInheritance.iteration, build.settings.iteration]);
    var typeNeedle = preparedTypes.find(function (f) { return f.token == qs.type; });
    var type = typeNeedle ? typeNeedle.token : (preparedTypes.length ? preparedTypes[preparedTypes.length - 1].token : "");
    var settings = (0, use_settings_1.useSettings)({
        token: isSelf.current ? "users:self" : type,
        table: build.settingsTable,
        connectionless: true,
        replicate: isSelf.current ? [build.settings] : null
    });
    var contentList = (0, react_1.useMemo)(function () {
        return sectionTable.map(function (item) {
            var keySection = (0, use_settings_1.SettingsParamToKey3)(qs.item, item.key);
            var overrideSection = settings.getExclusionOverride(keySection);
            var visibleSection = settings.getExclusionValue(keySection);
            //console.log( keySection, visibleSection, settings );
            return (react_1.default.createElement(v_eris_1.Card, { className: "eris-line" + (isSelf.current && !visibleSection ? " hidden" : ""), key: item.key, header: react_1.default.createElement("div", null,
                    item.icon,
                    react_1.default.createElement(v_eris_1.Space, null),
                    lang.get(item.title),
                    react_1.default.createElement("span", { className: "eris-line-toggle" + (isSelf.current ? " hidden" : "") },
                        react_1.default.createElement("span", { className: v_eris_1.Props.className("eris-parameter-changed", { active: settings.isExclusionChanged(keySection) }) }),
                        react_1.default.createElement("span", { className: v_eris_1.Props.className("eris-parameter-revert", { active: settings.isExclusionChanged(keySection) }), onClick: function () {
                                settings.revertExclusion(keySection);
                            } }, lang.get("Settings::Revert")),
                        react_1.default.createElement(v_eris_1.Space, null),
                        react_1.default.createElement(v_eris_1.Icons.redo, { transition: true, className: v_eris_1.Props.className("eris-parameter-clear", {
                                active: !settings.isExclusionCleared(keySection) && settings.isExclusionOverride(keySection) && overrideSection.length > 1
                            }), onClick: function () { return settings.clearExclusionOverride(keySection); } }),
                        react_1.default.createElement(v_eris_1.Space, null),
                        react_1.default.createElement(v_eris_1.Tooltip, { content: overrideSection.map(function (item) {
                                return react_1.default.createElement("div", { key: item.token },
                                    react_1.default.createElement("span", null,
                                        lang.get(item.name),
                                        " "),
                                    item.token == "default" ? lang.get("Settings::HasValue") : lang.get("Settings::OverrideValueSimple"),
                                    react_1.default.createElement(v_eris_1.Toggle, { value: item.value }));
                            }) },
                            react_1.default.createElement(v_eris_1.Toggle, { value: visibleSection, onChange: function (e) {
                                    settings.changeExclusion(keySection, e.value);
                                } })))) }, item.list.map(function (parameter) {
                var key = (0, use_settings_1.SettingsParamToKey)(qs.item, item.key, parameter.key);
                var override = settings.getOverride(key);
                var stringify = "";
                try {
                    stringify = JSON.stringify(settings.getSecureValue(key), null, " ");
                }
                catch (e) {
                    stringify = "";
                }
                ;
                if (parameter.bindless)
                    return null;
                return react_1.default.createElement(v_eris_1.Card, { key: key, header: react_1.default.createElement("div", null,
                        lang.get(parameter.title),
                        react_1.default.createElement("span", { className: v_eris_1.Props.className("eris-parameter-changed", { active: settings.isChanged(key) }) }),
                        react_1.default.createElement("span", { className: v_eris_1.Props.className("eris-parameter-revert", { active: settings.isChanged(key) }), onClick: function () {
                                settings.revert(key);
                            } }, lang.get("Settings::Revert")),
                        react_1.default.createElement(v_eris_1.Tooltip, { content: override.map(function (item) {
                                return react_1.default.createElement("div", { key: key + ":" + item.name },
                                    react_1.default.createElement("span", null,
                                        lang.get(item.name),
                                        " "),
                                    item.token == "default" ? lang.get("Settings::HasValue") : lang.get("Settings::OverrideValueSimple"),
                                    react_1.default.createElement("div", null, react_1.default.cloneElement(build.types[item.type], {
                                        value: item.valueSecure,
                                        k: key,
                                        params: item.params,
                                        lang: lang,
                                        stack: item.stack,
                                        extra: parameter.extra,
                                        secure: parameter.secure,
                                        settings: settings
                                    })));
                            }) },
                            react_1.default.createElement("span", { className: v_eris_1.Props.className("eris-parameter-override", {}) }, settings.isInherit(key) ?
                                lang.get("Settings::InheritFrom", [(settings.isCleared(key) && override.length > 1 ? override[override.length - 2].name : override[override.length - 1].name)])
                                :
                                    (settings.isOverride(key) && override.length > 1 ? lang.get("Settings::IsOverriding", [override[override.length - 2].name]) : ""))),
                        react_1.default.createElement(v_eris_1.Space, null),
                        react_1.default.createElement(v_eris_1.Tooltip, { content: lang.get("Settings::ClearOverride") },
                            react_1.default.createElement(v_eris_1.Icons.redo, { transition: true, className: v_eris_1.Props.className("eris-parameter-clear", {
                                    active: !settings.isCleared(key) && settings.isOverride(key) && override.length > 1
                                }), onClick: function () { return settings.clearOverride(key); } })),
                        react_1.default.createElement(v_eris_1.Space, null),
                        react_1.default.createElement("span", { className: altered ? "" : "hidden" }, key + " " + stringify)) }, react_1.default.createElement(v_eris_1.Column, { className: v_eris_1.Props.className("eris-parameter", { booltype: parameter.type == "bool" }) },
                    react_1.default.createElement(v_eris_1.Row, null, lang.get(parameter.desc)),
                    react_1.default.createElement(v_eris_1.Row, null,
                        react_1.default.cloneElement(build.types[parameter.type], {
                            params: parameter.params,
                            k: key,
                            value: settings.getSecureValue(key),
                            lang: lang,
                            icon: parameter.icon,
                            stack: parameter.stack,
                            extra: parameter.extra,
                            secure: parameter.secure,
                            settings: settings,
                            onChange: function (e, keyIndex) {
                                settings.change(key, e.value, keyIndex);
                            }
                        }),
                        react_1.default.createElement("span", null,
                            react_1.default.createElement(v_eris_1.Space, null),
                            settings.autoCorrected(key) ? lang.get("Settings::AutoCorrected", [settings.autoCorrected(key)]) : null))));
            })));
        });
    }, [qs.section, qs.item, settings.iteration, altered, lang.current]);
    var isLive = false;
    var liveContent = (0, react_1.useMemo)(function () {
        if (!isLive)
            return null;
        return react_1.default.cloneElement(react_1.default.createElement("div", null), {
            settings: settings
        });
    }, [isLive, settings.iteration]);
    var treeList = (0, react_1.useMemo)(function () {
        return route.list.map(function (item) {
            if (!item.list)
                return;
            var length = 0;
            return (react_1.default.createElement(v_eris_1.List.Item, { icon: item.icon, key: item.key, title: lang.get(item.title) }, item.list.map(function (data) {
                var sectionTable = route.table[data.key];
                if (!sectionTable)
                    return;
                length = 0;
                for (var _i = 0, sectionTable_2 = sectionTable; _i < sectionTable_2.length; _i++) {
                    var item_1 = sectionTable_2[_i];
                    var keySection = (0, use_settings_1.SettingsParamToKey3)(data.key, item_1.key);
                    var visibleSection = settings.getExclusionValue(keySection);
                    if (!visibleSection)
                        continue;
                    length++;
                }
                ;
                if (!length && isSelf.current)
                    return null;
                return react_1.default.createElement(v_eris_1.List.Item, { icon: data.icon, title: lang.get(data.title), key: data.key, value: build.buildQuery({ section: item.key, item: data.key }) }, "");
            })));
        });
    }, [route.table, route.list, build.currentLang, settings.iteration]);
    return (react_1.default.createElement("div", { className: "eris-content eris-content-selector" },
        react_1.default.createElement(content_menu_1.ContentMenu, null,
            react_1.default.createElement(v_eris_1.List, { value: build.buildQuery({ section: qs.section, item: qs.item }), onChange: function (event) {
                    build.setQueryString(event.value);
                }, padding: [3, 6, 3, 8] }, treeList)),
        react_1.default.createElement(content_container_1.ContentContainer, null,
            react_1.default.createElement("div", { className: "eris-content-header" },
                react_1.default.createElement("span", null,
                    react_1.default.createElement(v_eris_1.Button, { onClick: function () {
                            if (isSelf.current) {
                                build.setQueryString("/");
                                return;
                            }
                            ;
                            var temp = v_eris_1.Common.string(qs.token);
                            var split = temp.split(":");
                            var route = split[0];
                            route = String(route).charAt(0).toUpperCase() + String(route).slice(1);
                            var token = split[1];
                            var string = "/admin?section=" + route;
                            if (qs.filter)
                                string += "&filter=" + qs.filter;
                            build.setQueryString(string);
                        } }, "Back"),
                    react_1.default.createElement(v_eris_1.Space, null),
                    react_1.default.createElement(v_eris_1.Space, null),
                    react_1.default.createElement(v_eris_1.Space, null),
                    react_1.default.createElement(v_eris_1.Space, null),
                    react_1.default.createElement(v_eris_1.Space, null),
                    react_1.default.createElement(v_eris_1.Select, { className: isSelf.current ? "hidden" : "", stretch: true, headerless: true, row: true, style: { maxWidth: 400 }, value: type, list: (preparedTypes).map(function (item) {
                            return { icon: item.icon, value: item.token, title: item.name };
                        }), onChange: function (e) {
                            build.setQuery({ type: e.value });
                        } })),
                react_1.default.createElement("div", { style: { float: "right" } },
                    react_1.default.createElement(v_eris_1.Button, { className: (qs.type == "user" && !self ? "" : " hidden"), onClick: function () {
                            //						let needle = storage.list.find(( f ) => f.token == storage.getToken() );
                            //
                            //						if( !needle )
                            //							return;
                            //						window.localStorage.setItem( "RoleMirror", needle.token );
                            //						window.localStorage.setItem( "RoleMirrorName", needle.title );
                        } }, lang.get("Settings::Mirror")),
                    react_1.default.createElement(v_eris_1.Space, null),
                    react_1.default.createElement(v_eris_1.Button, { onClick: function () {
                            settings.save();
                        } }, lang.get("Settings::Save")))),
            react_1.default.createElement("div", { className: "eris-content-wrap" }, isLive ?
                (react_1.default.createElement("div", { className: "eris-content-live" },
                    react_1.default.createElement("div", { className: "eris-content-live-params" }, contentList),
                    react_1.default.createElement("div", { className: "eris-content-live-result" }, liveContent)))
                :
                    contentList))));
    return react_1.default.createElement("div", null);
};
exports.SettingsRoute = SettingsRoute;
//# sourceMappingURL=settings.route.js.map