import type { MenuTreeNode } from '@/types/api/role'

export interface MenuItem {
  index: string
  id: number
  label: string
  children?: MenuItem[]
}

/**
 * 把后端返回的 MenuTreeNode 树转换为导航菜单使用的 MenuItem 树。
 */
export function transformMenuTree(
  nodes: MenuTreeNode[] = [],
  topName: string = '/',
): MenuItem[] {
  return nodes.map((n) => {
    // 只有"无 children 且 route 是真实路径"才是可点的叶子
    const hasRealRoute = Boolean(n.route) && n.route !== '/'
    const isLeaf = !n.children?.length && hasRealRoute
    return {
      id: n.id,
      label: n.name || n.title,
      // 叶子：直接用 route；非叶子：
      index: isLeaf ? n.route! : topName,
      children: n.children?.length
        ? transformMenuTree(n.children, topName)
        : undefined,
    }
  })
}

const menu: MenuItem[] = [
  {
    index: '/dashboard',
    id: 1,
    label: '概览',
  },
  {
    index: 'logistics',
    id: 2,
    label: '物流',
    children: [
      {
        index: 'logistics-manage',
        id: 21,
        label: '物流管理',
        children: [
          { index: '/logistics/logisticsCompany', id: 211, label: '物流公司' },
          { index: '/logistics/logisticsMethod', id: 212, label: '物流方式' },
          {
            index: '/logistics/logisticsPartition',
            id: 213,
            label: '物流分区',
          },
          {
            index: '/logistics/logisticsQuotation',
            id: 214,
            label: '物流报价',
          },
          {
            index: '/logistics/logisticsCalculate',
            id: 215,
            label: '运费试算',
          },
          { index: '/logistics/logisticsTracking', id: 216, label: '物流跟踪' },
          { index: '/logistics/shippingAddress', id: 217, label: '发货地址' },
        ],
      },
      {
        index: 'logistics-rules',
        id: 22,
        label: '规则设置',
        children: [
          { index: '/logistics/declarationRule', id: 221, label: '申报规则' },
          {
            index: '/logistics/sortingConfiguration',
            id: 222,
            label: '分拣配置',
          },
        ],
      },
    ],
  },
  {
    index: 'warehouse',
    id: 3,
    label: '库存',
    children: [
      {
        index: 'warehouse-inventory',
        id: 31,
        label: '库存管理',
        children: [
          {
            index: '/warehouse/stocking-application',
            id: 311,
            label: '入库申请单',
          },
          { index: '/warehouse/warning', id: 312, label: '仓库预警' },
          { index: '/warehouse/receipt-doc', id: 313, label: '入库单' },
          { index: '/warehouse/issue-doc', id: 314, label: '出库单' },
          { index: '/warehouse/stocking-plan', id: 315, label: '备货计划' },
        ],
      },
      {
        index: 'warehouse-manage',
        id: 32,
        label: '仓库管理',
        children: [
          { index: '/warehouse/manage', id: 321, label: '仓库管理' },
          { index: '/warehouse/position', id: 322, label: '库位管理' },
        ],
      },
      {
        index: 'warehouse-rules',
        id: 33,
        label: '仓库规则',
        children: [
          {
            index: '/warehouse/warehouse-rule-setting',
            id: 331,
            label: '共享库存设置',
          },
        ],
      },
    ],
  },
  {
    index: 'order',
    id: 4,
    label: '订单',
    children: [
      {
        index: 'order-process',
        id: 41,
        label: '订单处理',
        children: [
          { index: '/order/list', id: 411, label: '定制订单' },
          { index: '/pod-order/list', id: 412, label: 'POD订单' },
          { index: '/pod-cn-order/list', id: 413, label: 'POD订单(CN)' },
          { index: '/pod-us-order/list', id: 414, label: 'POD订单(US)' },
          {
            index: '/order/factory-order-new',
            id: 415,
            label: '工厂订单(NEW)',
          },
          {
            index: '/order/cancel-order-process',
            id: 416,
            label: '取消后订单处理',
          },
        ],
      },
      {
        index: 'order-query',
        id: 42,
        label: '订单查询',
        children: [
          {
            index: '/pod-cn-order/orderTracking',
            id: 421,
            label: 'POD(CN)订单跟踪',
          },
          {
            index: '/pod-us-order/orderTracking',
            id: 422,
            label: 'POD(US)订单跟踪',
          },
        ],
      },
      {
        index: 'order-rules',
        id: 43,
        label: '订单规则',
        children: [
          {
            index: '/pod-us-order/podUsSchedulingRules',
            id: 431,
            label: 'POD(US)派单规则',
          },
          {
            index: '/order/allocation-sorting-rule',
            id: 432,
            label: '配货分拣规则',
          },
        ],
      },
    ],
  },
  {
    index: 'supply',
    id: 5,
    label: '供应',
    children: [
      {
        index: 'supply-production',
        id: 51,
        label: '采购生产',
        children: [
          {
            index: '/supply/out-of-stock-statistics',
            id: 511,
            label: '缺货统计',
          },
          { index: '/supply/stocking-order', id: 512, label: '备货订单' },
          { index: '/typesetting-management/list', id: 513, label: '打版管理' },
        ],
      },
      {
        index: 'supply-manage',
        id: 52,
        label: '供应管理',
        children: [
          { index: '/supply/supplierManagement', id: 521, label: '供应商管理' },
        ],
      },
    ],
  },
  {
    index: 'finance',
    id: 6,
    label: '财务',
    children: [
      {
        index: 'finance-reconciliation',
        id: 61,
        label: '对账管理',
        children: [
          { index: '/account/statement-note', id: 611, label: '定制对账单' },
          { index: '/account/pod-bill-order', id: 612, label: 'POD(CN)对账单' },
          {
            index: '/account/pod-us-bill-order',
            id: 613,
            label: 'POD(US)对账单',
          },
        ],
      },
    ],
  },
  {
    index: 'delivery',
    id: 7,
    label: '发货单',
    children: [
      {
        index: 'delivery-query',
        id: 71,
        label: '发货单查询',
        children: [
          { index: '/system/delivery-note', id: 711, label: '定制发货单' },
          { index: '/pod-delivery-note/list', id: 712, label: 'POD发货单' },
        ],
      },
    ],
  },
  {
    index: 'system',
    id: 8,
    label: '系统',
    children: [
      {
        index: 'system-manage',
        id: 81,
        label: '系统管理',
        children: [
          { index: '/system/user', id: 811, label: '用户管理' },
          { index: '/system/customers', id: 812, label: '客户管理' },
          // { index: '/system/role-manage', id: 812, label: '功能角色管理' },
        ],
      },
      {
        index: 'system-settings',
        id: 82,
        label: '系统设置',
        children: [
          { index: '/setting/settingIndex', id: 821, label: '工厂设置' },
          { index: '/system/downloadClient', id: 822, label: '下载生产客户端' },
          { index: '/system/role-manage', id: 823, label: '功能角色管理' },
        ],
      },
    ],
  },
]
export default menu
