import { ref } from 'vue' import { useApi } from '~/composables/useApi' export type PieceHistoryActor = { id: string label: string } export type PieceHistoryEntry = { id: string action: 'create' | 'update' | 'delete' | string createdAt: string actor: PieceHistoryActor | null diff: Record | null snapshot: Record | null } const extractItems = (payload: any): PieceHistoryEntry[] => { if (Array.isArray(payload?.items)) { return payload.items } if (Array.isArray(payload?.member)) { return payload.member } if (Array.isArray(payload?.['hydra:member'])) { return payload['hydra:member'] } return [] } export function usePieceHistory () { const { get } = useApi() const history = ref([]) const loading = ref(false) const error = ref(null) const loadHistory = async (pieceId: string) => { loading.value = true error.value = null try { const result = await get(`/pieces/${pieceId}/history`) if (!result.success) { error.value = result.error ?? 'Impossible de charger l’historique.' history.value = [] return result } history.value = extractItems(result.data) as PieceHistoryEntry[] return { success: true, data: history.value } } catch (err: any) { const message = err?.message ?? 'Erreur inconnue' error.value = message history.value = [] return { success: false, error: message } } finally { loading.value = false } } return { history, loading, error, loadHistory, } }