import { defineStore } from 'pinia' import type { ReceptionData } from '~/services/dto/reception-data' import { createReception, getReception, updateReception } from '~/services/reception' const isReceptionData = (value: unknown): value is ReceptionData => { return Boolean(value && typeof value === 'object' && 'id' in value) } export const useReceptionStore = defineStore('reception', { state: () => ({ current: null as ReceptionData | null, isLoading: false, errorMessage: null as string | null }), actions: { setCurrent(reception: ReceptionData | null) { this.current = reception }, clearError() { this.errorMessage = null }, async loadReception(id: number) { this.isLoading = true this.errorMessage = null try { const result = await getReception(id) if (!isReceptionData(result)) { this.errorMessage = 'Réception introuvable.' this.current = null return null } this.current = result return result } finally { this.isLoading = false } }, async createReception() { this.isLoading = true this.errorMessage = null try { const result = await createReception() if (!isReceptionData(result)) { this.errorMessage = 'Impossible de créer la réception.' return null } this.current = result return result } finally { this.isLoading = false } }, async updateReception(id: number, payload: Partial) { this.isLoading = true this.errorMessage = null try { const result = await updateReception(id, payload) if (!isReceptionData(result)) { this.errorMessage = 'Impossible de mettre à jour la réception.' return null } this.current = result return result } finally { this.isLoading = false } } } })