/** 首页 tabs-type 对应精选菜品专题 slug（与 quick-topic 子组件一致） */
export const QUICK_TOPIC_SLUGS = [
  'member-zone',
  'live-seafood-air',
  'must-eat-list',
  'new-calendar',
  'fresh-seafood-today',
] as const

export type QuickTopicSlug = (typeof QUICK_TOPIC_SLUGS)[number]

export function isQuickTopicSlug(value: string): value is QuickTopicSlug {
  return (QUICK_TOPIC_SLUGS as readonly string[]).includes(value)
}

/**
 * 将 /app/merchantCategory/list 单项映射为 quick-topic 的 topic。
 * 优先读后端字段 topicKey / code / topic；否则按列表顺序与五个专题一一对应。
 */
export function resolveQuickTopicFromMerchantCategory(
  item: Record<string, unknown>,
  index: number,
): QuickTopicSlug {
  const raw = item.topicKey ?? item.code ?? item.topic
  if (typeof raw === 'string' && isQuickTopicSlug(raw)) {
    return raw
  }
  const safeIndex = index >= 0 && index < QUICK_TOPIC_SLUGS.length ? index : 0
  return QUICK_TOPIC_SLUGS[safeIndex]
}

export function buildQuickTopicUrl(
  topic: QuickTopicSlug,
  extra: { merchantCategoryId?: string | number; categoryName?: string } = {},
) {
  const parts = [`topic=${encodeURIComponent(topic)}`]
  if (extra.merchantCategoryId != null && extra.merchantCategoryId !== '') {
    parts.push(`merchantCategoryId=${encodeURIComponent(String(extra.merchantCategoryId))}`)
  }
  if (extra.categoryName) {
    parts.push(`categoryName=${encodeURIComponent(extra.categoryName)}`)
  }
  return `/pages-store/pages/dishes/quick-topic?${parts.join('&')}`
}
