import { defineStore } from 'pinia'
import { ref } from 'vue'

/**
 * 菜谱分类列表页入口：由首页分类标签写入待选 id，列表页取出后清空，避免依赖路由 query。
 */
export const useCategoryNavStore = defineStore('categoryNav', () => {
  const pendingRecipeCategoryId = ref<string | null>(null)

  function setPendingRecipeCategoryId(id: string | number | null | undefined) {
    if (id == null || id === '') {
      pendingRecipeCategoryId.value = null
      return
    }
    pendingRecipeCategoryId.value = String(id)
  }

  /** 读取并清空，保证仅消费一次 */
  function consumePendingRecipeCategoryId(): string | null {
    const v = pendingRecipeCategoryId.value
    pendingRecipeCategoryId.value = null
    return v
  }

  return {
    pendingRecipeCategoryId,
    setPendingRecipeCategoryId,
    consumePendingRecipeCategoryId,
  }
})
