import { navigationMap, specialMenuItems } from '@/locales/navigation';

/**
 * 递归处理导航数据，实现国际化
 * @param {Array} navigationData - 原始导航数据
 * @param {Function} t - 翻译函数
 * @returns {Array} 处理后的导航数据
 */
export function translateNavigation(navigationData, t) {
  if (!Array.isArray(navigationData)) {
    return navigationData;
  }

  return navigationData.map(item => {
    const translatedItem = { ...item };
    
    // 根据 composingKey 获取翻译键
    const translationKey = navigationMap[item.composingKey];
    
    if (translationKey) {
      // 使用翻译，如果翻译不存在则保持原文
      const translatedText = t(translationKey);
      if (translatedText !== translationKey) {
        translatedItem.name = translatedText;
        translatedItem.label = translatedText;
      }
    }
    
    // 处理特殊菜单项
    if (specialMenuItems[item.composingKey]) {
      const specialHandler = specialMenuItems[item.composingKey];
      if (typeof specialHandler === 'function') {
        const specialResult = specialHandler(item, t);
        Object.assign(translatedItem, specialResult);
      }
    }
    
    // 递归处理子菜单
    if (item.children && item.children.length > 0) {
      translatedItem.children = translateNavigation(item.children, t);
    }
    
    return translatedItem;
  });
}

/**
 * 获取面包屑导航的国际化文本
 * @param {string} composingKey - 组合键
 * @param {Function} t - 翻译函数
 * @returns {string} 翻译后的文本
 */
export function getBreadcrumbText(composingKey, t) {
  const translationKey = navigationMap[composingKey];
  if (translationKey) {
    const translatedText = t(translationKey);
    return translatedText !== translationKey ? translatedText : composingKey;
  }
  return composingKey;
}

/**
 * 根据路由路径查找对应的导航项
 * @param {Array} navigationData - 导航数据
 * @param {string} path - 路由路径
 * @returns {Object|null} 找到的导航项
 */
export function findNavigationByPath(navigationData, path) {
  if (!Array.isArray(navigationData)) {
    return null;
  }

  for (const item of navigationData) {
    if (item.url === path) {
      return item;
    }
    
    if (item.children && item.children.length > 0) {
      const found = findNavigationByPath(item.children, path);
      if (found) {
        return found;
      }
    }
  }
  
  return null;
}

/**
 * 根据composingKey查找导航项
 * @param {Array} navigationData - 导航数据
 * @param {string} composingKey - 组合键
 * @returns {Object|null} 找到的导航项
 */
export function findNavigationByKey(navigationData, composingKey) {
  if (!Array.isArray(navigationData)) {
    return null;
  }

  for (const item of navigationData) {
    if (item.composingKey === composingKey) {
      return item;
    }
    
    if (item.children && item.children.length > 0) {
      const found = findNavigationByKey(item.children, composingKey);
      if (found) {
        return found;
      }
    }
  }
  
  return null;
}

/**
 * 构建面包屑路径
 * @param {Array} navigationData - 导航数据
 * @param {string} currentPath - 当前路径
 * @param {Function} t - 翻译函数
 * @returns {Array} 面包屑数组
 */
export function buildBreadcrumb(navigationData, currentPath, t) {
  const breadcrumb = [];
  
  function findPath(items, targetPath, path = []) {
    for (const item of items) {
      const currentPath = [...path, item];
      
      if (item.url === targetPath) {
        return currentPath;
      }
      
      if (item.children && item.children.length > 0) {
        const found = findPath(item.children, targetPath, currentPath);
        if (found) {
          return found;
        }
      }
    }
    return null;
  }
  
  const pathItems = findPath(navigationData, currentPath);
  if (pathItems) {
    return pathItems.map(item => ({
      ...item,
      name: getBreadcrumbText(item.composingKey, t),
      label: getBreadcrumbText(item.composingKey, t)
    }));
  }
  
  return breadcrumb;
}

/**
 * 获取菜单的层级深度
 * @param {Array} navigationData - 导航数据
 * @returns {number} 最大深度
 */
export function getNavigationDepth(navigationData) {
  if (!Array.isArray(navigationData) || navigationData.length === 0) {
    return 0;
  }
  
  let maxDepth = 1;
  
  for (const item of navigationData) {
    if (item.children && item.children.length > 0) {
      const childDepth = getNavigationDepth(item.children) + 1;
      maxDepth = Math.max(maxDepth, childDepth);
    }
  }
  
  return maxDepth;
}

/**
 * 扁平化导航数据
 * @param {Array} navigationData - 导航数据
 * @returns {Array} 扁平化后的数组
 */
export function flattenNavigation(navigationData) {
  const result = [];
  
  function flatten(items) {
    for (const item of items) {
      result.push(item);
      if (item.children && item.children.length > 0) {
        flatten(item.children);
      }
    }
  }
  
  if (Array.isArray(navigationData)) {
    flatten(navigationData);
  }
  
  return result;
}
