/**
 * This script marks the current path as active in the sidebar.
 */

const markActive = (link) => {
    const parentItem = link.closest('.sidebar-item');
    if (parentItem) {
        parentItem.classList.add('active');
    } else {
        link.classList.add('active');
    }
}

const checkLink = (fuzzy) => {
}

document.addEventListener('DOMContentLoaded', () => {
    const currentPath = window.location.pathname;
    const sidebarLinks = [...document.querySelectorAll('a.sidebar-link')]

    const exactMatches = sidebarLinks.filter(link => link.getAttribute('href') === currentPath)
    if (exactMatches.length > 0) {
        markActive(exactMatches[0])
    } else {
        fuzzyMatches = sidebarLinks.filter(link => currentPath.startsWith(link.getAttribute('href')))
        if (fuzzyMatches.length > 0) {
            const longestMatch = fuzzyMatches.sort((a, b) => b.href.length - a.href.length)[0]
            markActive(longestMatch)
        }
    }
})
