export type MaterialSubKey =
  | 'materialLockers'
  | 'materialInventory'
  | 'materialType'
  | 'materialStandard'
  | 'materialStandardPropertyValues'
  | 'materialInformation'
  | 'materialLoan'
  | 'materialInspectionPlan'
  | 'materialInspectionRecord'
  | 'materialReplacementRecord'
  | 'materialBlacklist'
  | 'materialInstructions'
  | 'materialBaseDataStatistics'
  | 'materialClaimReturnStatistics';

const MATERIAL_SUB_KEYS: MaterialSubKey[] = [
  'materialLockers',
  'materialInventory',
  'materialType',
  'materialStandard',
  'materialStandardPropertyValues',
  'materialInformation',
  'materialLoan',
  'materialInspectionPlan',
  'materialInspectionRecord',
  'materialReplacementRecord',
  'materialBlacklist',
  'materialInstructions',
  'materialBaseDataStatistics',
  'materialClaimReturnStatistics',
];

/**
 * 根据路由 path 解析物资子模块 key（与旧版 Vue 路由 lockers / inventory / type / standard /
 * information / coll / inspectionplan / Inspectionrecords / replacementrecords / blacklist / instructions 对齐）
 */
export function mapMaterialRoutePathToSubKey(path: string | null | undefined): MaterialSubKey | null {
  if (path == null) return null;
  const raw = String(path).trim();
  if (!raw) return null;
  const u = raw.toLowerCase().replace(/\\/g, '/');
  if (u === '/material' || u === 'material') return null;

  if (u.includes('blacklist')) return 'materialBlacklist';
  if (u.includes('instructions')) return 'materialInstructions';

  // get-permission-info：/clientSystem/MaterialDataStatistics/statistics/basicData、…/collectionReturn
  if (
    u.includes('statistics/base') ||
    u.includes('base-data') ||
    u.includes('basicdata') ||
    u.includes('basedata') ||
    u.includes('lockerone') ||
    u.includes('locker-one') ||
    u.includes('statisticians/lockerone')
  ) {
    return 'materialBaseDataStatistics';
  }
  if (
    u.includes('statistics/claim') ||
    u.includes('claim-return') ||
    u.includes('claimreturn') ||
    u.includes('collectionreturn') ||
    (u.includes('collection') && u.includes('return') && u.includes('statistics')) ||
    u.includes('lockertwo') ||
    u.includes('locker-two') ||
    u.includes('statisticians/lockertwo')
  ) {
    return 'materialClaimReturnStatistics';
  }

  if (
    u.includes('replacementrecords') ||
    u.includes('replacement-records') ||
    (u.includes('replacement') && (u.includes('record') || u.includes('records')))
  ) {
    return 'materialReplacementRecord';
  }

  if (
    u.includes('inspectionrecords') ||
    u.includes('inspection-records') ||
    u.includes('inspectionrecord') ||
    u.includes('checkrecord') ||
    u.includes('check-record')
  ) {
    return 'materialInspectionRecord';
  }

  if (u.includes('inspectionplan') || u.includes('inspection-plan')) return 'materialInspectionPlan';

  if (
    u.includes('/coll') ||
    u.includes('material/coll') ||
    u === 'coll' ||
    u.endsWith('/coll') ||
    u.includes('/loan') ||
    u.includes('materialloan') ||
    u.includes('borrow')
  ) {
    return 'materialLoan';
  }

  if (u.includes('information')) return 'materialInformation';
  /** 须在 generic `standard` 之前匹配 */
  if (u.includes('property-values') || u.includes('propertyvalues')) return 'materialStandardPropertyValues';
  if (u.includes('standard')) return 'materialStandard';
  if (u.includes('/type') || /\/type(\/|$|\?)/.test(u)) return 'materialType';
  if (u.includes('inventory')) return 'materialInventory';
  if (u.includes('locker') || u.includes('cabinet')) return 'materialLockers';

  return null;
}

/**
 * @param subMenu Dashboard 当前子菜单 key 或后端生成的占位 key
 * @param routePath 子菜单项上的 path（优先，用于纠正 key 与路由不一致）
 */
export function normalizeMaterialSubMenu(subMenu: string, routePath?: string | null): MaterialSubKey {
  const s = String(subMenu || '').trim();
  const lower = s.toLowerCase();

  const fromPath = mapMaterialRoutePathToSubKey(routePath);
  if (fromPath) return fromPath;

  if ((MATERIAL_SUB_KEYS as readonly string[]).includes(s)) return s as MaterialSubKey;

  const fromSub = mapMaterialRoutePathToSubKey(s);
  if (fromSub) return fromSub;

  if (s.includes('基础数据') && s.includes('统计')) return 'materialBaseDataStatistics';
  if (s.includes('领取') && s.includes('归还') && s.includes('统计')) return 'materialClaimReturnStatistics';
  if (s.includes('黑名单')) return 'materialBlacklist';
  if (s.includes('使用说明')) return 'materialInstructions';
  if (s.includes('物资柜')) return 'materialLockers';
  if (s.includes('盘点')) return 'materialInventory';
  if (s.includes('物资规格种类') || s.includes('规格种类')) return 'materialStandard';
  if (s.includes('规格设置')) return 'materialStandardPropertyValues';
  if (s.includes('物资类型')) return 'materialType';
  if (s.includes('规格')) return 'materialStandard';
  if (s.includes('类型')) return 'materialType';
  if (s.includes('物资清单') || s.includes('清单')) return 'materialInformation';
  if (s.includes('领取') || s.includes('归还') || s.includes('借还')) return 'materialLoan';
  if (s.includes('检查记录')) return 'materialInspectionRecord';
  if (s.includes('检查计划')) return 'materialInspectionPlan';
  if (s.includes('更换记录')) return 'materialReplacementRecord';

  if (lower.includes('blacklist')) return 'materialBlacklist';
  if (lower.includes('instruction')) return 'materialInstructions';
  if (lower.includes('locker') || lower.includes('cabinet')) return 'materialLockers';
  if (lower.includes('inventory')) return 'materialInventory';
  if (lower.includes('information') || lower.includes('materials')) return 'materialInformation';
  if (lower.includes('standard')) return 'materialStandard';
  if (lower.includes('/type') || lower.includes('materialtype')) return 'materialType';
  if (lower.includes('loan') || lower.includes('coll')) return 'materialLoan';
  if (lower.includes('inspectionrecord') || lower.includes('checkrecord')) return 'materialInspectionRecord';
  if (lower.includes('inspectionplan') || lower.includes('inspection-plan')) return 'materialInspectionPlan';
  if (lower.includes('replacement') || lower.includes('change')) return 'materialReplacementRecord';

  return 'materialLockers';
}
