"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.computeToc = exports.useComputeToc = void 0;
const tslib_1 = require("tslib");
const types_1 = require("@stoplight/types");
const lodash_1 = require("lodash");
const React = tslib_1.__importStar(require("react"));
const Provider_1 = require("../containers/Provider");
const README_REGEXP = new RegExp(`${lodash_1.escapeRegExp('README.md')}$`, 'i');
function useComputeToc(branchNodes) {
    const icons = React.useContext(Provider_1.IconsContext);
    return React.useMemo(() => computeToc(branchNodes, icons), [branchNodes, icons]);
}
exports.useComputeToc = useComputeToc;
function computeToc(branchNodes, icons) {
    var _a;
    if (!branchNodes.length)
        return [];
    let contents = [];
    const folders = [];
    const rootNodes = [];
    const docsNodes = lodash_1.sortBy(branchNodes.filter(branchNode => branchNode.snapshot.type === types_1.NodeType.Article), branchNode => lodash_1.toLower(branchNode.node.uri));
    for (const nodeIndex in docsNodes) {
        if (!docsNodes[nodeIndex])
            continue;
        const branchNode = docsNodes[nodeIndex];
        const uri = branchNode.node.uri.replace(/^\/(?:docs\/)?/, '');
        const parts = uri.split('/');
        if (parts.length > 1) {
            const pathToItem = parts.slice(0, -1);
            for (const [pathIndex, folderName] of pathToItem.entries()) {
                if (!folderName)
                    continue;
                if (!folders.includes(`${folderName}/${pathIndex}`)) {
                    folders.push(`${folderName}/${pathIndex}`);
                    const id = `${nodeIndex}-${pathIndex}`;
                    const name = folderName
                        .split('-')
                        .map(item => lodash_1.upperFirst(item))
                        .join(' ');
                    if (Number(pathIndex) === 0) {
                        contents.push({
                            id,
                            name,
                            depth: 0,
                            type: 'divider',
                        });
                    }
                    else {
                        contents.push({
                            id,
                            name,
                            depth: Number(pathIndex) - 1,
                            type: 'group',
                            icon: icons.group,
                        });
                    }
                }
            }
            contents.push({
                id: branchNode.id,
                name: branchNode.snapshot.name,
                depth: Math.max(parts.length - 2, 0),
                type: 'item',
                icon: icons[branchNode.snapshot.type] || icons.item,
                to: branchNode.node.uri,
            });
        }
        else {
            const contentNode = {
                id: branchNode.id,
                name: branchNode.snapshot.name,
                depth: 0,
                type: 'item',
                icon: icons[branchNode.snapshot.type] || icons.item,
                to: branchNode.node.uri,
            };
            if (README_REGEXP.test(branchNode.node.uri)) {
                rootNodes.unshift(contentNode);
            }
            else {
                rootNodes.push(contentNode);
            }
        }
    }
    contents = rootNodes.concat(contents);
    const httpServiceNodes = lodash_1.sortBy(branchNodes.filter(branchNode => branchNode.snapshot.type === types_1.NodeType.HttpService), branchNode => lodash_1.toLower(branchNode.snapshot.name));
    for (const httpServiceNode of httpServiceNodes) {
        const parentUriRegexp = new RegExp(`^${lodash_1.escapeRegExp(httpServiceNode.node.uri)}\/`, 'i');
        const childNodes = branchNodes.filter(branchNode => parentUriRegexp.test(branchNode.node.uri) && branchNode.snapshot.type !== types_1.NodeType.HttpService);
        if (!childNodes.length)
            continue;
        const dividerNode = {
            id: httpServiceNode.id,
            name: httpServiceNode.snapshot.name,
            depth: 0,
            type: 'divider',
            icon: icons[httpServiceNode.snapshot.type] || icons.divider,
        };
        if (httpServiceNode.version && httpServiceNode.version !== '0.0') {
            dividerNode.meta = `v${httpServiceNode.version}`;
        }
        contents.push(dividerNode);
        contents.push({
            id: `${httpServiceNode.id}-overview`,
            name: 'Overview',
            depth: 0,
            icon: icons.item,
            type: 'item',
            to: httpServiceNode.node.uri,
        });
        const tags = {};
        const other = [];
        for (const childNode of childNodes) {
            if ((_a = childNode.snapshot.tagNames) === null || _a === void 0 ? void 0 : _a.length) {
                const tag = lodash_1.toLower(childNode.snapshot.tagNames[0]);
                if (!tags[tag]) {
                    tags[tag] = [];
                }
                tags[tag].push(childNode);
            }
            else {
                other.push(childNode);
            }
        }
        const sortedTags = lodash_1.sortBy(Object.keys(tags), t => lodash_1.toLower(t));
        for (const tagIndex in sortedTags) {
            if (!sortedTags[tagIndex])
                continue;
            const tag = sortedTags[tagIndex];
            contents.push({
                id: `${httpServiceNode.id}-${tag}-${tagIndex}`,
                name: lodash_1.startCase(tag),
                depth: 0,
                type: 'group',
                icon: icons.group,
            });
            for (const tagChild of lodash_1.sortBy(tags[tag], 'name')) {
                contents.push({
                    id: tagChild.id,
                    name: tagChild.snapshot.name,
                    depth: 1,
                    icon: icons[tagChild.snapshot.type] || icons.item,
                    type: 'item',
                    to: tagChild.node.uri,
                });
            }
        }
        if (other.length) {
            contents.push({
                id: `${httpServiceNode.id}-other`,
                name: 'Other',
                depth: 0,
                type: 'group',
                icon: icons.group,
            });
            for (const otherChild of lodash_1.sortBy(other, branchNode => lodash_1.toLower(branchNode.snapshot.name))) {
                contents.push({
                    id: otherChild.id,
                    name: otherChild.snapshot.name,
                    depth: 1,
                    icon: icons[otherChild.snapshot.type] || icons.item,
                    type: 'item',
                    to: otherChild.node.uri,
                });
            }
        }
    }
    const modelContents = [];
    const modelNodes = lodash_1.sortBy(branchNodes.filter(branchNode => branchNode.snapshot.type === types_1.NodeType.Model), branchNode => lodash_1.toLower(branchNode.snapshot.name));
    for (const modelNode of modelNodes) {
        if (contents.find(n => n.to === modelNode.node.uri))
            continue;
        const node = {
            id: modelNode.id,
            name: modelNode.snapshot.name,
            to: modelNode.node.uri,
            depth: 0,
            type: 'item',
            icon: icons[modelNode.snapshot.type] || icons.item,
        };
        if (modelNode.version && modelNode.version !== '0.0') {
            node.meta = `v${modelNode.version}`;
        }
        modelContents.push(node);
    }
    if (modelContents.length) {
        contents.push({
            id: 'models',
            name: 'Models',
            depth: 0,
            type: 'divider',
        });
        contents = contents.concat(modelContents);
    }
    return contents;
}
exports.computeToc = computeToc;
//# sourceMappingURL=useComputeToc.js.map