import type {
	PermissionActionMenuItem,
	PermissionMenuDefinition,
	PermissionMenuNode,
	PermissionRouteMenuItem
} from '@/types/permission'

type TranslateFn = (key: string, params?: Record<string, string | number>) => string

// 后端返回菜单编码，前端在这里维护菜单编码到路由、图标和展示分区的映射。
export const permissionMenuDefinitions: PermissionMenuDefinition[] = [
	{
		menuCode: 'sys_ai_workflow',
		path: '/workflow',
		icon: 'workflow',
		section: 'main',
		titleKey: 'sidebar.menu.orchestration'
	},
	{
		menuCode: 'sys_ai_knowledge_base',
		path: '/knowledge',
		icon: 'book',
		section: 'main',
		titleKey: 'sidebar.menu.knowledge'
	},
	{
		menuCode: 'sys_ai_agent',
		path: '/agent',
		icon: 'platForm',
		section: 'main',
		titleKey: 'sidebar.menu.management'
	},
	{
		menuCode: 'sys_ai_chat',
		path: '/chat',
		icon: 'chatMessage',
		section: 'main',
		titleKey: 'sidebar.menu.chat',
		openMode: 'blank'
	},
	{
		menuCode: 'sys_ai_ollama',
		path: '/ollama',
		icon: 'ollama',
		section: 'bottom',
		titleKey: 'sidebar.menu.ollama'
	},
	{
		menuCode: 'sys_ai_models',
		path: '/models',
		icon: 'model',
		section: 'bottom',
		titleKey: 'sidebar.menu.models'
	},
	{
		menuCode: 'sys_ai_web_search',
		path: '/web-search',
		icon: 'websearch',
		section: 'bottom',
		titleKey: 'sidebar.menu.webSearch'
	},
	{
		menuCode: 'sys_ai_storage',
		path: '/storage',
		icon: 'storage',
		section: 'bottom',
		titleKey: 'sidebar.menu.storage'
	},
	{
		menuCode: 'sys_ai_vector_store',
		path: '/vector-store',
		icon: 'vector',
		section: 'bottom',
		titleKey: 'sidebar.menu.vectorStore'
	},
	{
		menuCode: 'sys_ai_mcp',
		path: '/mcp',
		icon: 'mcp',
		section: 'bottom',
		titleKey: 'sidebar.menu.mcp'
	},
	{
		menuCode: 'sys_ai_prompts',
		path: '/prompts',
		icon: 'prompts',
		section: 'bottom',
		titleKey: 'sidebar.menu.prompts'
	},
	{
		menuCode: 'sys_ai_skills',
		path: '/skills',
		icon: 'skills',
		section: 'bottom',
		titleKey: 'sidebar.menu.skills'
	},
	{
		menuCode: 'sys_ai_exec_logs',
		path: '/execution',
		icon: 'play',
		section: 'bottom',
		titleKey: 'sidebar.menu.execution'
	}
]

const permissionMenuDefinitionMap = new Map(
	permissionMenuDefinitions.map((definition) => [definition.menuCode, definition])
)

export const resolvePermissionMenuDefinition = (menuCode?: string) => {
	if (!menuCode) return undefined
	return permissionMenuDefinitionMap.get(menuCode)
}

export const resolveMenuRoutePath = (menuCode?: string) =>
	resolvePermissionMenuDefinition(menuCode)?.path

// 侧边栏只渲染后端已授权且前端有路由映射的菜单。
export const buildSidebarRouteMenus = (
	menus: PermissionMenuNode[],
	t: TranslateFn
): PermissionRouteMenuItem[] =>
	menus
		.map((menu) => {
			const definition = resolvePermissionMenuDefinition(menu.code)
			if (!definition) return null

			return {
				type: 'route' as const,
				menuCode: menu.code,
				path: definition.path,
				icon: definition.icon,
				label: menu.name || t(definition.titleKey),
				section: definition.section,
				openMode: definition.openMode || 'route',
				menuIndex: menu.menuIndex
			}
		})
		.filter((item): item is PermissionRouteMenuItem => item !== null)
		.sort((left, right) => left.menuIndex - right.menuIndex)

export const getSidebarActionMenus = (t: TranslateFn): PermissionActionMenuItem[] => [
	{
		type: 'action',
		action: 'settings',
		icon: 'help',
		label: t('sidebar.menu.help'),
		settingsName: 'help',
		showChevron: true
	},
	{
		type: 'action',
		action: 'settings',
		icon: 'setting',
		label: t('sidebar.menu.settings'),
		settingsName: 'settings',
		showChevron: true
	}
]
