"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BuildModules = exports.Build = exports.BuildInside = exports.BuildCommands = exports.BuildContext = void 0;
var tslib_1 = require("tslib");
var react_1 = tslib_1.__importStar(require("react"));
var v_eris_1 = require("v-eris");
require("../../../assets/styles/themes.scss");
require("../../../assets/styles/eris.scss");
require("../../../assets/styles/core.scss");
require("../../../assets/styles/components.scss");
require("../../../assets/styles/fixes.scss");
var auth_1 = require("../Auth/auth");
var use_settings_1 = require("../../utility/use.settings");
var react_router_dom_1 = require("react-router-dom");
;
;
;
;
;
;
;
;
var BuildContextDefault = {
    routes: {},
    settings: {},
    settingsTable: {},
    currentLang: "",
    currentTheme: "",
    route: "",
    query: {},
    buildQuery: function (value, clear) { return ""; },
    setQueryString: function (value) { },
    setQuery: function (value, clear) { },
    clearQuery: function () { },
    types: {},
    denyAccess: false,
};
exports.BuildContext = react_1.default.createContext(BuildContextDefault);
var ParseSectionInside = function (item, sections) {
    sections[item.key] = item;
    if (!item.list)
        return;
    for (var _i = 0, _a = (item.list || []); _i < _a.length; _i++) {
        var inside = _a[_i];
        ParseSectionInside(inside, sections);
    }
    ;
};
var BuildCommands = /** @class */ (function () {
    function BuildCommands() {
    }
    BuildCommands.joinLang = function (table, append) {
        var result = tslib_1.__assign(tslib_1.__assign({}, table), append);
        return result;
    };
    ;
    BuildCommands.joinTypes = function (table, append) {
        var result = tslib_1.__assign(tslib_1.__assign({}, table), append);
        return result;
    };
    ;
    BuildCommands.joinTable = function (table, append) {
        var result = tslib_1.__assign({}, table);
        for (var key in append) {
            if (!result[key])
                result[key] = append[key];
            else {
                var array = result[key].list;
                result[key] = append[key];
                result[key].list = array && append[key].list ? tslib_1.__spreadArray(tslib_1.__spreadArray([], array, true), append[key].list, true) : append[key].list;
            }
        }
        ;
        return result;
    };
    ;
    BuildCommands.joinList = function (table, append) {
        var result = [];
        for (var _i = 0, table_1 = table; _i < table_1.length; _i++) {
            var item = table_1[_i];
            result.push(tslib_1.__assign({}, item));
        }
        ;
        var _loop_1 = function (item) {
            var needleIndex = result.findIndex(function (f) { return f.key == item.key; });
            if (needleIndex < 0) {
                result.push(item);
                return "continue";
            }
            ;
            if (result[needleIndex].list && item.list)
                result[needleIndex].list = tslib_1.__spreadArray(tslib_1.__spreadArray([], result[needleIndex].list, true), item.list, true);
            else
                result[needleIndex].list = item.list;
            for (var key in item) {
                if (key == "list")
                    continue;
                result[needleIndex][key] = item[key];
            }
            ;
        };
        for (var _a = 0, append_1 = append; _a < append_1.length; _a++) {
            var item = append_1[_a];
            _loop_1(item);
        }
        ;
        return result;
    };
    ;
    return BuildCommands;
}());
exports.BuildCommands = BuildCommands;
;
var BuildInside = function (props) {
    var children = props.children, languages = props.languages, currentLang = props.currentLang, routes = props.routes, types = props.types;
    var lang = (0, react_1.useContext)(v_eris_1.LangContext);
    var auth = (0, react_1.useContext)(auth_1.AuthContext);
    var userToken = window.localStorage.getItem("RoleMirror") || "self";
    var settings = (0, use_settings_1.useSettings)({
        token: "users:" + userToken,
        access: auth.access,
        table: props.settingsTable,
        user: auth.token
    });
    var location = (0, react_router_dom_1.useLocation)();
    var nav = (0, react_router_dom_1.useNavigate)();
    var qs = v_eris_1.Common.parseQuery(location);
    var currentQS = {};
    for (var key in qs) {
        currentQS[key] = v_eris_1.Common.string(qs[key] || "") || "";
    }
    ;
    var buildQuery = function (value, clear) {
        return v_eris_1.Common.setQuery(location, value, clear);
    };
    var setQueryString = function (value) {
        nav(value);
    };
    var clearQuery = function () {
        setQueryString(v_eris_1.Common.setQuery(location, {}, true));
    };
    var setQuery = function (value, clear) {
        setQueryString(v_eris_1.Common.setQuery(location, value, clear));
    };
    var selectedLang = (currentLang ? currentLang : settings.getSecureValue("General::Language::CurrentLanguage")) || "en";
    var currentTheme = (props.currentTheme ? props.currentTheme : settings.getSecureValue("General::Theme::CurrentTheme")) || "white";
    (0, react_1.useEffect)(function () {
        lang.dispatch(["add", "en", {}]);
        for (var langName in languages) {
            lang.dispatch(["add", langName, languages[langName]]);
        }
        ;
    }, [languages]);
    (0, react_1.useEffect)(function () {
        lang.dispatch(["select", selectedLang]);
    }, [selectedLang]);
    (0, react_1.useEffect)(function () {
        document.body.dataset.theme = currentTheme;
    }, [currentTheme]);
    var pathname = location.pathname;
    if (pathname && pathname[0] == "/")
        pathname = pathname.substring(1);
    var firstRoute = "";
    var newRoutes = {};
    if (routes) {
        for (var key in routes) {
            if (!firstRoute)
                firstRoute = key;
            var sections = {};
            for (var _i = 0, _a = (routes[key].list || []); _i < _a.length; _i++) {
                var item = _a[_i];
                ParseSectionInside(item, sections);
            }
            ;
            newRoutes[key] = tslib_1.__assign(tslib_1.__assign({}, routes[key]), { sections: sections, table: routes[key].table || {}, list: routes[key].list || [] });
        }
        ;
        if (!newRoutes[""] && newRoutes[firstRoute])
            newRoutes[""] = tslib_1.__assign(tslib_1.__assign({}, newRoutes[firstRoute]), { hidden: true });
    }
    ;
    var route = routes && routes[pathname] ? pathname : firstRoute;
    var deny = false;
    if (routes && routes[route] && routes[route].access > auth.access)
        deny = true;
    return react_1.default.createElement(exports.BuildContext.Provider, { value: {
            routes: newRoutes,
            currentLang: lang.current,
            currentTheme: currentTheme,
            route: route,
            settings: settings,
            settingsTable: props.settingsTable,
            denyAccess: deny,
            query: currentQS,
            setQuery: setQuery,
            buildQuery: buildQuery,
            setQueryString: setQueryString,
            clearQuery: clearQuery,
            types: types || {}
        } }, children);
};
exports.BuildInside = BuildInside;
var Build = function (props) {
    var modules = props.modules, children = props.children, languages = props.languages, currentLang = props.currentLang, settingsTable = props.settingsTable, routes = props.routes, types = props.types;
    var providersDefault = function (inside) {
        return react_1.default.createElement(v_eris_1.Lang, null, inside);
    };
    var providers = (function (inside) {
        var n = modules.length - 1;
        var list = providersDefault(react_1.default.createElement("div", { className: "eris eris-container" }, inside));
        for (; n > -1; n--) {
            var _a = modules[n](), context = _a.context, r = tslib_1.__rest(_a, ["context"]);
            list = react_1.default.createElement(context.Provider, { value: r }, list);
        }
        ;
        return list;
    });
    return (react_1.default.createElement(react_1.default.Fragment, null, providers(react_1.default.createElement(exports.BuildInside, { currentLang: currentLang, currentTheme: props.currentTheme, settingsTable: settingsTable, languages: languages, routes: routes, types: types }, children))));
};
exports.Build = Build;
var BuildModules = function (props) {
    var modules = props.modules, children = props.children;
    var providers = (function (inside) {
        var n = modules.length - 1;
        var list = react_1.default.createElement(react_1.default.Fragment, null, inside);
        for (; n > -1; n--) {
            var _a = modules[n](), context = _a.context, r = tslib_1.__rest(_a, ["context"]);
            list = react_1.default.createElement(context.Provider, { value: r }, list);
        }
        ;
        return list;
    });
    return (react_1.default.createElement(react_1.default.Fragment, null, providers(children)));
};
exports.BuildModules = BuildModules;
//# sourceMappingURL=build.js.map