import type { FolderTreeNode } from "@/lib/sidebar-nav-maps" import { findFolderPath } from "@/lib/sidebar-nav-folder-ids" import { getMailNavFolderLabel } from "@/lib/sidebar-nav-data" /** @deprecated Utiliser `getMailNavFolderLabel(inboxTab, folderIdToLabel)` ou `inboxTabDisplayLabel`. */ export const INBOX_CATEGORY_TAB_LABELS: Record = { primary: "Principale", } /** Clé stable pour historique navigation (dossier + onglet boîte de réception). */ export function mailNavVisitKey(folderId: string, inboxTab?: string): string { if (folderId === "inbox" && inboxTab && inboxTab !== "primary") { return `inbox::${inboxTab}` } return folderId } export function parseMailNavVisitKey(key: string): { folderId: string inboxTab?: string } { if (key.startsWith("inbox::")) { return { folderId: "inbox", inboxTab: key.slice("inbox::".length) } } return { folderId: key } } export type MailNavBreadcrumbItem = { label: string visitKey: string } export function getMailNavFolderBreadcrumbItems( folderId: string, folderTree: FolderTreeNode[], folderIdToLabel?: Record, inboxCategory?: { tabId: string; label: string } | null ): MailNavBreadcrumbItem[] { if (folderId === "inbox") { const base = getMailNavFolderLabel(folderId, folderIdToLabel) if (inboxCategory && inboxCategory.label !== "Principale") { return [ { label: base, visitKey: mailNavVisitKey("inbox") }, { label: inboxCategory.label, visitKey: mailNavVisitKey("inbox", inboxCategory.tabId), }, ] } return [{ label: base, visitKey: mailNavVisitKey(folderId) }] } const path = findFolderPath(folderTree, folderId) if (path?.length) { return path.map((n) => ({ label: n.label, visitKey: n.id })) } return [ { label: getMailNavFolderLabel(folderId, folderIdToLabel), visitKey: folderId, }, ] } export function getMailNavFolderBreadcrumbSegments( folderId: string, folderTree: FolderTreeNode[], folderIdToLabel?: Record, inboxCategoryLabel?: string | null, inboxTabId?: string | null ): string[] { const inboxCategory = folderId === "inbox" && inboxTabId && inboxCategoryLabel && inboxCategoryLabel !== "Principale" ? { tabId: inboxTabId, label: inboxCategoryLabel } : null return getMailNavFolderBreadcrumbItems( folderId, folderTree, folderIdToLabel, inboxCategory ).map((i) => i.label) } export function breadcrumbItemsForVisitKey( key: string, folderTree: FolderTreeNode[], folderIdToLabel?: Record ): MailNavBreadcrumbItem[] { const { folderId, inboxTab } = parseMailNavVisitKey(key) const inboxCategory = folderId === "inbox" && inboxTab && inboxTab !== "primary" ? { tabId: inboxTab, label: getMailNavFolderLabel(inboxTab, folderIdToLabel), } : null return getMailNavFolderBreadcrumbItems( folderId, folderTree, folderIdToLabel, inboxCategory ) } export function breadcrumbSegmentsForVisitKey( key: string, folderTree: FolderTreeNode[], folderIdToLabel?: Record ): string[] { return breadcrumbItemsForVisitKey(key, folderTree, folderIdToLabel).map( (i) => i.label ) } export function breadcrumbForVisitKey( key: string, folderTree: FolderTreeNode[], folderIdToLabel?: Record ): string { return breadcrumbSegmentsForVisitKey(key, folderTree, folderIdToLabel).join( " · " ) }