"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Templates = void 0;
var tslib_1 = require("tslib");
var react_1 = tslib_1.__importStar(require("react"));
var v_eris_1 = require("v-eris");
var templates_block_1 = require("./templates.block");
var react_router_dom_1 = require("react-router-dom");
require("./templates.scss");
var Templates = function (props) {
    var className = props.className, rest = tslib_1.__rest(props, ["className"]);
    var lang = (0, react_1.useContext)(v_eris_1.LangContext);
    var location = (0, react_router_dom_1.useLocation)();
    var nav = (0, react_router_dom_1.useNavigate)();
    var qs = v_eris_1.Common.parseQuery(location);
    qs.section = v_eris_1.Common.string(qs.section || "");
    qs.subsection = v_eris_1.Common.string(qs.subsection || "");
    qs.token = v_eris_1.Common.string(qs.token || "");
    var section = props.templates[qs.section];
    if (!section || !props.value[qs.section] || !props.value[qs.section].length) {
        for (var templateKey in props.templates) {
            var item = props.templates[templateKey];
            if (!props.value[item.key] || !props.value[item.key].length)
                continue;
            qs.section = item.key;
            section = item;
            break;
        }
        ;
        if (!section) {
            for (var templateKey in props.templates) {
                qs.section = props.templates[templateKey].key;
                section = props.value[qs.section] || [];
                break;
            }
            ;
        }
        ;
    }
    ;
    var value = props.value[qs.section];
    var subsection = (section.children || []).find(function (f) { return f.key == qs.subsection; });
    if (!subsection) {
        qs.subsection = section.children && section.children[0] ? section.children[0].key : "";
        subsection = section.children ? (section.children[0] || []) : [];
    }
    ;
    var dynamic = (0, react_1.useMemo)(function () {
        var list = [];
        var dynamicData = value;
        if (!dynamicData || !dynamicData.length || !section)
            return { data: dynamicData, list: list, clusters: !!section.clusters };
        for (var _i = 0, dynamicData_1 = dynamicData; _i < dynamicData_1.length; _i++) {
            var item = dynamicData_1[_i];
            if (!item.list || !Array.isArray(item.list) || !item.list.length)
                continue;
            var cluster = section.clusters ? [] : list;
            for (var _a = 0, _b = item.list; _a < _b.length; _a++) {
                var itemData = _b[_a];
                cluster.push(itemData);
            }
            ;
            if (section.clusters)
                list.push({ value: item.type, title: lang.get(item.type), list: cluster, length: cluster.length || "" });
        }
        ;
        return { data: dynamicData, list: list, clusters: !!section.clusters };
    }, [qs.section, value]);
    var headerElements = (0, react_1.useMemo)(function () {
        var result = [];
        for (var itemKey in props.templates) {
            var item = props.templates[itemKey];
            var length_1 = 0;
            var array = props.value[item.key] || [];
            for (var _i = 0, array_1 = array; _i < array_1.length; _i++) {
                var part = array_1[_i];
                length_1 += (part.list || []).length;
            }
            ;
            if (!length_1)
                continue;
            result.push({
                icon: item.icon,
                value: item.key,
                key: item.key,
                title: lang.get(item.name),
                length: length_1 || ""
            });
        }
        ;
        return result;
    }, [
        props.value,
    ]);
    var sectionInfo = (0, react_1.useMemo)(function () {
        if (section.empty) {
            return dynamic.data.map(function (item, index) { return (react_1.default.createElement("div", { key: item.id + ":" + index }, react_1.default.createElement(section.module, { data: item }))); });
        }
        ;
        var array = dynamic.list;
        if (!array.length)
            return null;
        if (dynamic.clusters) {
            array = dynamic.list.find(function (f) { return f.value == qs.cluster; });
            if (array)
                array = array.list;
            else
                array = dynamic.list[0] ? (dynamic.list[0].list || []) : [];
        }
        ;
        var alone = array.length > 1 ? false : true;
        var selected = qs.token ? array.find(function (f) { return qs.token == f.id; }) : null;
        if (selected) {
            array = [selected];
        }
        ;
        return array.map(function (item, index) { return (react_1.default.createElement(templates_block_1.TemplatesBlock, { key: item.id + ":" + index, token: item.id, section: section, subsection: subsection, selected: selected, alone: alone, qs: qs, dynamic: dynamic, data: item })); });
    }, [qs, value]);
    return (react_1.default.createElement("div", { className: v_eris_1.Props.className("eris-templates-container") },
        react_1.default.createElement(v_eris_1.Select, { style: { display: headerElements.length > 1 ? undefined : "none" }, stretch: true, headerless: true, row: true, value: qs.section, onSelect: function (v) {
                var s = props.templates[v.value];
                nav(v_eris_1.Common.setQuery(location, { section: v.value, subsection: s && s.children && s.children[0] ? (s.children[0].key || s.children[0].name) : "", cluster: "" }));
            }, list: headerElements }),
        react_1.default.createElement(v_eris_1.Divider, { style: { display: headerElements.length > 1 ? undefined : "none" } }),
        dynamic.clusters && dynamic.list.length > 1 ?
            (react_1.default.createElement("div", null,
                react_1.default.createElement(v_eris_1.Select, { stretch: true, headerless: true, row: true, value: qs.cluster, onSelect: function (v) {
                        nav(v_eris_1.Common.setQuery(location, { section: qs.section, cluster: v.value, token: "" }));
                    }, list: dynamic.list }),
                react_1.default.createElement(v_eris_1.Divider, null)))
            :
                null,
        react_1.default.createElement("div", null, section.key == "settings" ?
            (sectionInfo)
            :
                (react_1.default.createElement("div", { className: v_eris_1.Props.className("eris-templates") },
                    react_1.default.createElement(v_eris_1.Card, { header: react_1.default.createElement("div", { className: "eris-templates-title" },
                            section.icon,
                            react_1.default.createElement(v_eris_1.Space, null),
                            lang.get(section.name)) }, sectionInfo))))));
};
exports.Templates = Templates;
//# sourceMappingURL=templates.js.map