import type { PageSelectorItem } from '../types/components' import type { PluginNavItem } from '../types/platform' export interface PluginNavPageSelectorOptions { pluginIcon?: string } export function normalizeNavPath(path?: string): string { const raw = path?.trim() || '/' const prefixed = raw.startsWith('/') ? raw : `/${raw}` return prefixed.replace(/\/+$/, '') || '/' } export function pageIdFromPath(path: string, fallback: string): string { const normalized = normalizeNavPath(path) if (normalized === '/') return 'dashboard' return normalized.replace(/^\/+/, '').replace(/\/+/g, '-') || fallback } export function pluginNavItemToPageSelectorItem( item: PluginNavItem, index: number, options: PluginNavPageSelectorOptions = {}, ): PageSelectorItem { const path = normalizeNavPath(item.path) return { id: item.id || pageIdFromPath(path, `page-${index + 1}`), label: item.label, to: path, icon: item.icon || options.pluginIcon, } } export function currentPagePath( pathname: string | undefined, routePrefix?: string, ): string | undefined { if (!pathname) return undefined const normalizedPathname = normalizeNavPath(pathname) const normalizedRoutePrefix = normalizeNavPath(routePrefix) if (normalizedPathname === normalizedRoutePrefix) return '/' if ( normalizedRoutePrefix !== '/' && normalizedPathname.startsWith(`${normalizedRoutePrefix}/`) ) { return normalizeNavPath(normalizedPathname.slice(normalizedRoutePrefix.length)) } return normalizedPathname } export function currentItemIdFromLocation( pages: Array>, pathname: string | undefined, routePrefix?: string, ): string | undefined { const path = currentPagePath(pathname, routePrefix) if (!path) return undefined return pages.find((page) => normalizeNavPath(page.to || page.href) === path)?.id }