import type { Ref } from 'vue' import type { FulfillmentScene, MiniCategory, MiniMenuSection } from '@/shared' import { getCategories, getMenu } from '@/services' import type { useAppStore } from '@/stores' type AppStoreInstance = ReturnType export function createMenuDataActions (payload: { appStore: AppStoreInstance categories: Ref errorMessage: Ref loading: Ref sections: Ref }) { const { appStore, categories, errorMessage, loading, sections } = payload async function loadMenu () { loading.value = true errorMessage.value = '' try { await appStore.initBootstrap() await appStore.initStores() const [nextCategories, nextSections] = await Promise.all([ getCategories(appStore.currentStore.id, appStore.scene, appStore.channel), getMenu(appStore.currentStore.id, appStore.scene, appStore.channel) ]) categories.value = nextCategories sections.value = nextSections } catch (error: unknown) { errorMessage.value = error instanceof Error ? error.message : '菜单加载失败,请检查接口是否可用' } finally { loading.value = false } } async function handleSceneChange (value: string) { appStore.setScene(value as FulfillmentScene) await loadMenu() } return { handleSceneChange, loadMenu } }