import type { SidebarSection } from '~/shared/types/sidebar'

const sections = ref<SidebarSection[]>([])
const disabledRoutes = ref<string[]>([])
const loaded = ref(false)

export function useSidebar() {
    async function loadSidebar(): Promise<void> {
        const api = useApi()
        const data = await api.get<{ sections: SidebarSection[]; disabledRoutes: string[] }>(
            '/sidebar', {}, { toast: false },
        )
        sections.value = data.sections ?? []
        disabledRoutes.value = data.disabledRoutes ?? []
        loaded.value = true
    }

    function isRouteDisabled(path: string): boolean {
        return disabledRoutes.value.some(
            (disabled) => path === disabled || path.startsWith(disabled + '/'),
        )
    }

    function resetSidebar(): void {
        sections.value = []
        disabledRoutes.value = []
        loaded.value = false
    }

    return { sections, disabledRoutes, loaded, loadSidebar, isRouteDisabled, resetSidebar }
}
