import { useMemo } from 'react';
import { useLocation } from 'react-router-dom';
import { useActiveSectionId } from '@redocly/theme/core/openapi';
import { pathMatchedLink } from '../../../utils/index.js';
import { DEFAULT_WEBHOOKS_TAG_NAME } from '../../../constants.js';
export function useMenuItems({ items }) {
    const location = useLocation();
    const activeItemId = useActiveSectionId(location, false, false);
    return useMemo(() => mapAndSetActiveItems(items, activeItemId, location), [activeItemId, items, location]);
}
function mapAndSetActiveItems(items, activeItemId = '', location) {
    const newItems = [];
    convertOpenAPIDocs2Sidebar({
        contentItems: items,
        sidebarItems: newItems,
        activeItemId,
        location,
    });
    return newItems;
}
function isItemActive(item, activeItemId = '', location) {
    if (!item.href)
        return false;
    if (activeItemId) {
        return item.id === activeItemId;
    }
    return pathMatchedLink(location, item.href);
}
function isSubItemActive(item, activeItemId = '', location) {
    const items = item.items || [];
    return items.some((i) => {
        return isItemActive(i, activeItemId, location) || isSubItemActive(i, activeItemId, location);
    });
}
function convertOpenAPIDocs2Sidebar(data) {
    const { contentItems, sidebarItems, activeItemId, location } = data;
    let addWebhookSeparator = true;
    for (const item of contentItems) {
        const active = isItemActive(item, activeItemId, location);
        const hasActiveSubItem = isSubItemActive(item, activeItemId, location);
        switch (item.type) {
            case 'group':
                sidebarItems.push({ type: 'separator', label: item.name, active, hasActiveSubItem });
                convertOpenAPIDocs2Sidebar({ ...data, contentItems: item.items, sidebarItems });
                break;
            case 'tag':
                const tagGroup = {
                    type: 'group',
                    label: item.name,
                    items: [],
                    link: item.href,
                    active,
                    hasActiveSubItem,
                    modified: true,
                };
                sidebarItems.push(tagGroup);
                convertOpenAPIDocs2Sidebar({
                    contentItems: item.items,
                    sidebarItems: tagGroup.items || [],
                    activeItemId,
                    location,
                });
                break;
            case 'operation':
            case 'schema':
                if (item.isWebhook &&
                    addWebhookSeparator &&
                    item.parent.name !== DEFAULT_WEBHOOKS_TAG_NAME) {
                    sidebarItems.push({ type: 'separator', label: 'Webhooks', variant: 'secondary' });
                    addWebhookSeparator = false;
                }
                sidebarItems.push({
                    type: 'link',
                    label: item.name,
                    httpVerb: item.type === 'schema' ? 'schema' : item.httpVerb,
                    link: item.href,
                    badges: item.badges,
                    active,
                    hasActiveSubItem,
                    modified: true,
                    deprecated: item.deprecated,
                });
                break;
            case 'section':
                if (item.depth === -1) {
                    continue;
                }
                const sectionItem = {
                    type: item.items.length ? 'group' : 'link',
                    label: item.name,
                    link: item.href,
                    items: [],
                    active,
                    hasActiveSubItem,
                    modified: true,
                };
                sidebarItems.push(sectionItem);
                if (item.items.length) {
                    convertOpenAPIDocs2Sidebar({
                        contentItems: item.items,
                        sidebarItems: sectionItem.items,
                        activeItemId,
                        location,
                    });
                }
                break;
        }
    }
}
//# sourceMappingURL=useMenuItems.js.map