"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 __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
exports.bfs = exports.filterTree = exports.findSubtreeInPathByUrl = exports.findTreeInPath = exports.usePromise = exports.formatPathToTitle = exports.debug = exports.TOP_TITLE_H = void 0;
var startCase_1 = __importDefault(require("lodash/startCase"));
var react_1 = require("react");
var cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));
// export const version = require('../package.json').version
exports.TOP_TITLE_H = '60px';
exports.debug = function (x) {
    if (process.env.DEBUG) {
        console.log('[DEBUG]', x);
    }
};
function formatPathToTitle(path) {
    var endPath = path
        .split('/')
        .map(function (x) { return x.trim(); })
        .filter(Boolean)
        .reverse()[0];
    var withoutExt = endPath.split('.')[0];
    return startCase_1["default"](withoutExt);
}
exports.formatPathToTitle = formatPathToTitle;
function usePromise(p, defaultValue) {
    var _a = react_1.useState(defaultValue), v = _a[0], set = _a[1];
    react_1.useEffect(function () {
        p()
            .then(set)["catch"](function (e) {
            console.error('error in usePromise\n' + e);
        });
    }, [p]);
    return [v];
}
exports.usePromise = usePromise;
function isPromise(p) {
    return p && typeof p.then === 'function';
}
function findTreeInPath(tree, path) {
    var _a;
    // remove leading and trailing slashes
    path = path.replace(/^\/|\/$/g, '');
    if (!((_a = tree === null || tree === void 0 ? void 0 : tree.children) === null || _a === void 0 ? void 0 : _a.length)) {
        return null;
    }
    if (tree.path === path) {
        return tree;
    }
    for (var _i = 0, _b = tree.children; _i < _b.length; _i++) {
        var child = _b[_i];
        var found = findTreeInPath(child, path);
        if (found) {
            return found;
        }
    }
}
exports.findTreeInPath = findTreeInPath;
function findSubtreeInPathByUrl(tree, url) {
    var _a;
    if (!((_a = tree === null || tree === void 0 ? void 0 : tree.children) === null || _a === void 0 ? void 0 : _a.length)) {
        return null;
    }
    for (var i = 0; i < tree.children.length; i++) {
        var child = tree.children[i];
        if (child.url === url) {
            return {
                previous: tree.children[i - 1],
                current: tree,
                next: tree.children[i + 1]
            };
        }
        var found = findSubtreeInPathByUrl(child, url);
        if (found) {
            return found;
        }
    }
}
exports.findSubtreeInPathByUrl = findSubtreeInPathByUrl;
function filterTree(tree, filter) {
    if (!tree) {
        return tree;
    }
    return __assign(__assign({}, tree), { children: filterChildren(cloneDeep_1["default"]((tree === null || tree === void 0 ? void 0 : tree.children) || []), filter) });
}
exports.filterTree = filterTree;
function filterChildren(data, filter) {
    var r = data.filter(function (o) {
        if (o === null || o === void 0 ? void 0 : o.children)
            o.children = filterChildren(o.children, filter);
        return filter(o);
    });
    return r;
}
function bfs(tree) {
    var results = [];
    // tree.depth = 0
    var queue = [tree];
    var n;
    while (queue.length > 0) {
        n = queue.shift();
        results.push(n);
        if (!n.children) {
            continue;
        }
        for (var i = 0; i < n.children.length; i++) {
            var child = n.children[i];
            // child.depth = (n.depth || 0) + 1
            queue.push(child);
        }
    }
    return results;
}
exports.bfs = bfs;
//# sourceMappingURL=support.js.map