"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AdminRoute = 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 react_router_dom_1 = require("react-router-dom");
var use_storage_1 = require("../../utility/use.storage");
var build_1 = require("../../components/Build/build");
var AdminCreateLine = function (route, types, props, updateTable) {
    var k = props.k, parameter = props.parameter, lang = props.lang, qs = props.qs, nav = props.nav, location = props.location, section = props.section, value = props.value, storage = props.storage, paramsOverride = props.paramsOverride;
    var storageValue = storage.getValue(k);
    var v = parameter.path ? (qs[parameter.path]) : (storageValue === undefined ? value : storageValue);
    if (v === undefined) {
        v = parameter.value;
    }
    ;
    updateTable[k] = v;
    return react_1.default.cloneElement(types[parameter.type], {
        lang: lang,
        params: parameter.params,
        extra: parameter.extra,
        filter: parameter.filter,
        dynamic: parameter.dynamic,
        paramsOverride: paramsOverride,
        value: v,
        qs: qs,
        route: route.sections[section].route,
        feedback: function (value, extra) {
            storage.change(k, value);
        },
        onChange: function (e) {
            var _a;
            storage.change(k, e.value);
            if (parameter.path)
                nav(v_eris_1.Common.setQuery(location, (_a = {}, _a[parameter.path] = e.value, _a)));
        }
    });
};
var AdminRoute = function () {
    var lang = (0, react_1.useContext)(v_eris_1.LangContext);
    var nav = (0, react_router_dom_1.useNavigate)();
    var location = (0, react_router_dom_1.useLocation)();
    var qs = v_eris_1.Common.parseQuery(location);
    var build = (0, react_1.useContext)(build_1.BuildContext);
    var route = build.routes["admin"];
    qs.section = v_eris_1.Common.string(qs.section || "");
    qs.token = v_eris_1.Common.string(qs.token || "");
    if (!route.sections[v_eris_1.Common.string(qs.section)])
        qs.section = "Users";
    var section = qs.section;
    var sectionItem = route.sections[section] || {};
    var sectionTable = route.table[section] || [];
    var routeStorage = (0, use_storage_1.useStorage)({
        token: "default",
        section: sectionItem.route || "",
        current: sectionItem.global,
        ignoreList: true,
    });
    var roles = (0, v_eris_1.useAsync)({
        method: "POST",
        url: "./roles/list/"
    }, {});
    roles.onResponse(function (response) {
        routeStorage.overwrite("roles", response.data);
    });
    (0, react_1.useEffect)(function () {
        roles.fetch();
    }, [qs.section]);
    var policy = (0, v_eris_1.useAsync)({
        method: "POST",
        url: "./policy/list/"
    }, {});
    policy.onResponse(function (response) {
        routeStorage.overwrite("policy", response.data);
    });
    (0, react_1.useEffect)(function () {
        policy.fetch();
    }, [qs.section]);
    var configurations = (0, v_eris_1.useAsync)({
        method: "POST",
        url: "./configurations/list/"
    }, {});
    configurations.onResponse(function (response) {
        routeStorage.overwrite("configurations", response.data);
    });
    (0, react_1.useEffect)(function () {
        configurations.fetch();
    }, [qs.section]);
    var paramsOverride = {
        save: {
            onChange: function (e, params) {
                e.stopPropagation();
                routeStorage.save({ parseKeys: true });
            }
        }
    };
    var contentList = (0, react_1.useMemo)(function () {
        var updateTable = {};
        var elements = sectionTable.map(function (item) {
            return (react_1.default.createElement(v_eris_1.Card, { className: "eris-line", key: item.key, header: item.title ? (react_1.default.createElement("span", null,
                    item.icon,
                    react_1.default.createElement(v_eris_1.Space, null),
                    lang.get(item.title))) : null }, (item.list || []).map(function (parameter) {
                var key = parameter.key; //PermissionParamToKey( qs.section as string, item.key, parameter.key );
                return react_1.default.createElement(v_eris_1.Card, { key: key, header: parameter.title ?
                        (react_1.default.createElement("div", null,
                            lang.get(parameter.title),
                            react_1.default.createElement(v_eris_1.Space, null)))
                        :
                            null }, react_1.default.createElement(v_eris_1.Column, { className: "content-parameter" },
                    parameter.desc ?
                        (react_1.default.createElement(v_eris_1.Row, { align: "center" },
                            react_1.default.createElement(v_eris_1.Row, null, (parameter.left || []).map(function (p1) {
                                var k = p1.key; //PermissionParamToKey( qs.section as string, item.key, p1.key );
                                return react_1.default.createElement(react_1.default.Fragment, { key: k },
                                    AdminCreateLine(route, build.types, {
                                        k: k,
                                        value: p1.value,
                                        parameter: p1,
                                        paramsOverride: paramsOverride[p1.key],
                                        lang: lang,
                                        qs: qs,
                                        nav: nav,
                                        location: location,
                                        section: section,
                                        storage: routeStorage
                                    }, updateTable),
                                    react_1.default.createElement(v_eris_1.Space, null));
                            })),
                            react_1.default.createElement(v_eris_1.Row, null, lang.get(parameter.desc)),
                            react_1.default.createElement(v_eris_1.Row, null, (parameter.right || []).map(function (p1) {
                                var k = p1.key; //PermissionParamToKey( qs.section as string, item.key, p1.key );
                                return react_1.default.createElement(react_1.default.Fragment, { key: k },
                                    react_1.default.createElement(v_eris_1.Space, null),
                                    AdminCreateLine(route, build.types, {
                                        k: k,
                                        value: p1.value,
                                        parameter: p1,
                                        paramsOverride: paramsOverride[p1.key],
                                        lang: lang,
                                        qs: qs,
                                        nav: nav,
                                        location: location,
                                        section: section,
                                        storage: routeStorage
                                    }, updateTable));
                            })))) : null,
                    react_1.default.createElement(v_eris_1.Row, null, AdminCreateLine(route, build.types, {
                        k: key,
                        value: item.value,
                        parameter: parameter,
                        paramsOverride: paramsOverride[parameter.key],
                        lang: lang,
                        qs: qs,
                        nav: nav,
                        location: location,
                        section: section,
                        storage: routeStorage
                    }, updateTable))));
            })));
        });
        routeStorage.changeMulti(updateTable);
        return elements;
    }, [qs.section, qs.token, routeStorage.it, build.currentLang]);
    return (react_1.default.createElement(use_storage_1.StorageContext.Provider, { value: routeStorage },
        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: v_eris_1.Common.setQuery(location, { section: qs.section }), onChange: function (event) { return nav(event.value); }, padding: [3, 6, 3, 8] }, route.list.map(function (item) {
                    var list = item.list || [];
                    return (react_1.default.createElement(v_eris_1.List.Item, { key: item.key, title: lang.get(item.title), icon: item.icon, value: v_eris_1.Common.setQuery(location, { section: item.key }) }, list.length ?
                        (list.map(function (child) {
                            return react_1.default.createElement(v_eris_1.List.Item, { key: item.key + ":" + child.key, title: lang.get(child.title), icon: child.icon, value: v_eris_1.Common.setQuery(location, { section: child.key }) }, lang.get(child.title));
                        }))
                        :
                            (lang.get(item.title))));
                }))),
            react_1.default.createElement(content_container_1.ContentContainer, null,
                react_1.default.createElement("div", { className: "eris-content-header" },
                    react_1.default.createElement(v_eris_1.Space, null),
                    react_1.default.createElement(v_eris_1.Space, null)),
                react_1.default.createElement("div", { className: "eris-content-wrap" },
                    react_1.default.createElement(v_eris_1.Card, { className: "eris-content-card" }, contentList))))));
};
exports.AdminRoute = AdminRoute;
//# sourceMappingURL=admin.route.js.map