import { __assign, __rest, __spreadArray } from "tslib";
import React, { useContext, useEffect } from "react";
import { LangContext, Lang, Common } from "v-eris";
import "../../../assets/styles/themes.scss";
import "../../../assets/styles/eris.scss";
import "../../../assets/styles/core.scss";
import "../../../assets/styles/components.scss";
import "../../../assets/styles/fixes.scss";
import { AuthContext } from "../Auth/auth";
import { useSettings } from "../../utility/use.settings";
import { useLocation, useNavigate } from "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,
};
export var BuildContext = React.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 = __assign(__assign({}, table), append);
        return result;
    };
    ;
    BuildCommands.joinTypes = function (table, append) {
        var result = __assign(__assign({}, table), append);
        return result;
    };
    ;
    BuildCommands.joinTable = function (table, append) {
        var result = __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 ? __spreadArray(__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(__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 = __spreadArray(__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;
}());
export { BuildCommands };
;
export var BuildInside = function (props) {
    var children = props.children, languages = props.languages, currentLang = props.currentLang, routes = props.routes, types = props.types;
    var lang = useContext(LangContext);
    var auth = useContext(AuthContext);
    var userToken = window.localStorage.getItem("RoleMirror") || "self";
    var settings = useSettings({
        token: "users:" + userToken,
        access: auth.access,
        table: props.settingsTable,
        user: auth.token
    });
    var location = useLocation();
    var nav = useNavigate();
    var qs = Common.parseQuery(location);
    var currentQS = {};
    for (var key in qs) {
        currentQS[key] = Common.string(qs[key] || "") || "";
    }
    ;
    var buildQuery = function (value, clear) {
        return Common.setQuery(location, value, clear);
    };
    var setQueryString = function (value) {
        nav(value);
    };
    var clearQuery = function () {
        setQueryString(Common.setQuery(location, {}, true));
    };
    var setQuery = function (value, clear) {
        setQueryString(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";
    useEffect(function () {
        lang.dispatch(["add", "en", {}]);
        for (var langName in languages) {
            lang.dispatch(["add", langName, languages[langName]]);
        }
        ;
    }, [languages]);
    useEffect(function () {
        lang.dispatch(["select", selectedLang]);
    }, [selectedLang]);
    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] = __assign(__assign({}, routes[key]), { sections: sections, table: routes[key].table || {}, list: routes[key].list || [] });
        }
        ;
        if (!newRoutes[""] && newRoutes[firstRoute])
            newRoutes[""] = __assign(__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.createElement(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);
};
export 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.createElement(Lang, null, inside);
    };
    var providers = (function (inside) {
        var n = modules.length - 1;
        var list = providersDefault(React.createElement("div", { className: "eris eris-container" }, inside));
        for (; n > -1; n--) {
            var _a = modules[n](), context = _a.context, r = __rest(_a, ["context"]);
            list = React.createElement(context.Provider, { value: r }, list);
        }
        ;
        return list;
    });
    return (React.createElement(React.Fragment, null, providers(React.createElement(BuildInside, { currentLang: currentLang, currentTheme: props.currentTheme, settingsTable: settingsTable, languages: languages, routes: routes, types: types }, children))));
};
export var BuildModules = function (props) {
    var modules = props.modules, children = props.children;
    var providers = (function (inside) {
        var n = modules.length - 1;
        var list = React.createElement(React.Fragment, null, inside);
        for (; n > -1; n--) {
            var _a = modules[n](), context = _a.context, r = __rest(_a, ["context"]);
            list = React.createElement(context.Provider, { value: r }, list);
        }
        ;
        return list;
    });
    return (React.createElement(React.Fragment, null, providers(children)));
};
//# sourceMappingURL=build.js.map