"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
exports.applySidebarOrdering = exports.NavTree = void 0;
var react_1 = require("@chakra-ui/react");
var orderBy_1 = __importDefault(require("lodash/orderBy"));
var react_2 = __importStar(require("react"));
var NavLink_1 = require("./NavLink");
var support_1 = require("../support");
var FILES_EXTENSION_REGEX = /\.(tsx?|jsx?)/;
var NavTree = function (_a) {
    var _b = _a.tree, tree = _b === void 0 ? { children: [] } : _b, rest = __rest(_a, ["tree"]);
    var _c = {}, _d = _c.sidebarOrdering, sidebarOrdering = _d === void 0 ? undefined : _d, _e = _c.docsRootPath, docsRootPath = _e === void 0 ? 'pages' : _e;
    tree = applySidebarOrdering({ tree: tree, order: sidebarOrdering });
    // console.log(tree)
    tree = support_1.findTreeInPath(tree, docsRootPath) || tree;
    // console.log(tree)
    if (!tree) {
        console.error(new Error("sidenav tree is null"));
        tree = { name: '', children: [] };
    }
    return (react_2["default"].createElement(react_1.Stack, __assign({ as: 'nav', "aria-label": 'Main navigation' }, rest),
        react_2["default"].createElement(react_1.Box, null, tree.children.map(function (x) { return (react_2["default"].createElement(NavTreeComponent, __assign({ hideDivider: true, key: x.path || x.title }, x))); }))));
};
exports.NavTree = NavTree;
function equalWithoutExtension(a, b) {
    return (a.replace(FILES_EXTENSION_REGEX, '') ===
        b.replace(FILES_EXTENSION_REGEX, ''));
}
function applySidebarOrdering(_a) {
    var order = _a.order, tree = _a.tree;
    // TODO order sidebar based on files metadata (last modified time)
    order = order || {};
    if (!tree.children) {
        return tree;
    }
    // put the non listed entries last
    tree.children = orderBy_1["default"](tree.children, function (x) {
        var index = Object.keys(order).findIndex(function (k) {
            return equalWithoutExtension(k, x.name);
        });
        if (index === -1) {
            return Infinity;
        }
        return index;
    });
    // put the folders last
    tree.children = orderBy_1["default"](tree.children, function (x, i) {
        var _a;
        if ((_a = x === null || x === void 0 ? void 0 : x.children) === null || _a === void 0 ? void 0 : _a.length) {
            return Infinity;
        }
        return 0;
    });
    // remove entries with `false` value
    tree.children = tree.children.filter(function (x) {
        var found = Object.keys(order).find(function (k) {
            return equalWithoutExtension(k, x.name);
        });
        if (found && order[found] === false) {
            return false;
        }
        return true;
    });
    // recurse
    tree.children.forEach(function (node) {
        applySidebarOrdering({
            tree: node,
            order: order[node.name] ||
                order[node.name.replace(FILES_EXTENSION_REGEX, '')]
        });
    });
    return tree;
}
exports.applySidebarOrdering = applySidebarOrdering;
var NavTreeComponent = function (_a) {
    var _b = _a.name, name = _b === void 0 ? '' : _b, children = _a.children, _c = _a.depth, depth = _c === void 0 ? 1 : _c, _d = _a.hideDivider, hideDivider = _d === void 0 ? false : _d, _e = _a.url, url = _e === void 0 ? '' : _e, _f = _a.title, title = _f === void 0 ? '' : _f, path = _a.path, rest = __rest(_a, ["name", "children", "depth", "hideDivider", "url", "title", "path"]);
    var isFolder = !url;
    var formattedTitle = title || name;
    var subTree = react_2.useMemo(function () {
        return children &&
            children.map(function (x) {
                return (react_2["default"].createElement(NavTreeComponent, __assign({ key: x.path || x.title }, x, { depth: depth + 1 })));
            });
    }, [children]);
    if (isFolder) {
        return (react_2["default"].createElement(CollapsableTreeNode, { path: path, depth: depth, title: formattedTitle, subTree: subTree }));
    }
    return (react_2["default"].createElement(react_1.Stack, { spacing: '0px' },
        react_2["default"].createElement(NavLink_1.ComponentLink, { opacity: 0.8, pl: '0.4em', my: '0.1em', href: url, isTruncated: true }, formattedTitle),
        subTree));
};
function CollapsableTreeNode(_a) {
    var title = _a.title, path = _a.path, depth = _a.depth, subTree = _a.subTree;
    var key = 'sidenav-state-' + path;
    var _b = react_1.useDisclosure({
        defaultIsOpen: Boolean(localStorage.getItem(key))
    }), onToggle = _b.onToggle, isOpen = _b.isOpen;
    react_2.useEffect(function () {
        localStorage.setItem(key, isOpen ? 'true' : '');
    }, [isOpen]);
    return (react_2["default"].createElement(react_1.Stack, { spacing: '0px' },
        react_2["default"].createElement(react_1.Box, { display: 'flex', alignItems: 'center', cursor: 'pointer', onClick: onToggle, py: '0.2em', my: '0.2em' },
            react_2["default"].createElement(react_1.Box, { mr: '0.4em', boxSize: '0.6em', opacity: 0.6, display: 'inline-block', as: isOpen ? CollapseDown : CollapseRight }),
            title),
        react_2["default"].createElement(react_1.Collapse, { style: { paddingLeft: '20px' }, "in": isOpen }, subTree)));
}
var CollapseRight = function (props) {
    return (react_2["default"].createElement("svg", __assign({ viewBox: '0 0 5 8', fill: 'currentColor', xmlns: 'http://www.w3.org/2000/svg' }, props),
        react_2["default"].createElement("path", { d: 'M0 0.724246C0 0.111374 0.681914 -0.223425 1.13107 0.168926L4.66916 3.25957C5.11028 3.6449 5.11028 4.3551 4.66916 4.74043L1.13107 7.83107C0.681913 8.22342 0 7.88863 0 7.27575V0.724246Z', fill: 'currentColor' })));
};
var CollapseDown = function (props) {
    return (react_2["default"].createElement("svg", __assign({ viewBox: '0 0 8 6', fill: 'currentColor', xmlns: 'http://www.w3.org/2000/svg' }, props),
        react_2["default"].createElement("path", { d: 'M7.27575 0.5C7.88863 0.5 8.22342 1.18191 7.83107 1.63107L4.74043 5.16916C4.3551 5.61028 3.6449 5.61028 3.25957 5.16916L0.168926 1.63107C-0.223425 1.18191 0.111375 0.5 0.724247 0.5L7.27575 0.5Z', fill: 'currentColor' })));
};
//# sourceMappingURL=NavTree.js.map