import { computed, ref } from 'vue' import type { Ref } from 'vue' import type { WeightEntryData } from '~/services/dto/reception-data' import type { WeightData } from '~/services/dto/weight-data' import { createWeight, updateWeight } from '~/services/weight' export type WeighingMode = 'gross' | 'tare' export interface UseWeighingOptions { mode: WeighingMode entity: Ref<{ id: number; currentStep: number; isValid: boolean; weights?: WeightEntryData[] | null } | null> entityName: 'reception' | 'shipment' apiResource: string titleLabel: string getWeightFromScale: () => Promise updateEntity: (id: number, payload: any) => Promise loadEntity?: (id: number) => Promise } export const useWeighing = ({ mode, entity, entityName, apiResource, titleLabel, getWeightFromScale, updateEntity, loadEntity }: UseWeighingOptions) => { const weightData = ref(null) const isFetching = ref(false) const currentWeightEntry = computed(() => { const weights = entity.value?.weights ?? [] return weights.find((entry) => entry.type === mode) ?? null }) const displayWeight = computed(() => weightData.value?.weight ?? currentWeightEntry.value?.weight ?? null) const displayDsd = computed(() => weightData.value?.dsd ?? currentWeightEntry.value?.dsd ?? '-') const title = computed(() => titleLabel) const showLoadingBox = computed(() => isFetching.value) const fetchWeight = async () => { isFetching.value = true weightData.value = await getWeightFromScale().finally(() => { isFetching.value = false }) } const saveWeight = async () => { if (!entity.value) return const existingEntry = currentWeightEntry.value const baseDsd = weightData.value?.dsd ?? existingEntry?.dsd ?? null const baseWeight = weightData.value?.weight ?? existingEntry?.weight ?? null const baseWeighedAt = weightData.value?.weighedAt ?? existingEntry?.weighedAt ?? null if (baseWeight === null) return const relationPayload: Record = {} relationPayload[entityName] = `/api/${apiResource}/${entity.value.id}` if (existingEntry?.id) { await updateWeight(existingEntry.id, { type: mode, dsd: baseDsd, weight: baseWeight, weighedAt: baseWeighedAt }) } else { await createWeight({ ...relationPayload, type: mode, dsd: baseDsd, weight: baseWeight, weighedAt: baseWeighedAt }) } const nextStep = mode === 'tare' ? entity.value.currentStep : entity.value.currentStep + 1 await updateEntity(entity.value.id, { currentStep: nextStep, isValid: entity.value.isValid }) if (loadEntity) { await loadEntity(entity.value.id) } } return { weightData, currentWeightEntry, displayWeight, displayDsd, title, showLoadingBox, fetchWeight, saveWeight } } // Backward-compatible aliases export const useWeighingShipment = ({ modeShipment, shipment, updateShipment, loadShipment }: { modeShipment: WeighingMode shipment: Ref updateShipment: (id: number, payload: any) => Promise loadShipment?: (id: number) => Promise }) => { return useWeighing({ mode: modeShipment, entity: shipment, entityName: 'shipment', apiResource: 'shipments', titleLabel: modeShipment === 'gross' ? 'Pesée à vide' : 'Pesée à plein', getWeightFromScale: async () => { const { getWeightShipment } = await import('~/services/shipment') return getWeightShipment() }, updateEntity: updateShipment, loadEntity: loadShipment }) }