import { resolveFileUrl } from '@/utils/image'

const CATEGORY_PLACEHOLDER = '/static/logo.png'

export function isHotCategory(hotFlag) {
  return String(hotFlag) === '1'
}

export function mapCategoryNode(row, isChild = false) {
  if (!row) return null
  return {
    categoryId: row.categoryId,
    categoryName: row.categoryName,
    categoryPic: row.categoryPic,
    displayPic: resolveFileUrl(row.categoryPic) || CATEGORY_PLACEHOLDER,
    hotFlag: row.hotFlag,
    isHot: isHotCategory(row.hotFlag),
    sortNo: row.sortNo,
    children: isChild ? undefined : (row.children || []).map((c) => mapCategoryNode(c, true)).filter(Boolean)
  }
}

export function mapCategoryTree(list) {
  if (!Array.isArray(list)) return []
  return list.map((row) => mapCategoryNode(row, false)).filter(Boolean)
}

export function mapLevel2Tabs(list) {
  if (!Array.isArray(list)) return []
  return list.map((row) => mapCategoryNode(row, true)).filter(Boolean)
}
