import type { Ref } from 'vue' import { useWeighing } from '~/composables/useWeighing' import { usePdfPrinter } from '~/composables/usePdfPrinter' import type { WeightData } from '~/services/dto/weight-data' interface UseWeighingStepOptions { mode: 'gross' | 'tare' entity: Ref entityName: 'reception' | 'shipment' apiResource: string titleLabel: string isFinal: boolean getWeightFromScale: () => Promise updateEntity: (id: number, payload: any) => Promise loadEntity: (id: number) => Promise clearEntity: () => void buildReceiptFilename: (entity: any) => string } export const useWeighingStep = (options: UseWeighingStepOptions) => { const router = useRouter() const { printPdf } = usePdfPrinter() const { weightData, currentWeightEntry, displayWeight, displayDsd, title, showLoadingBox, fetchWeight, saveWeight } = useWeighing({ mode: options.mode, entity: options.entity, entityName: options.entityName, apiResource: options.apiResource, titleLabel: options.titleLabel, getWeightFromScale: options.getWeightFromScale, updateEntity: options.updateEntity, loadEntity: options.loadEntity }) const showGenerateReceipt = computed( () => options.isFinal && displayWeight.value !== null ) const printReceipt = async () => { if (!import.meta.client || !options.entity.value) return await saveWeight() const entity = options.entity.value const filename = options.buildReceiptFilename(entity) await printPdf(`/${options.apiResource}/${entity.id}/receipt`, filename) await new Promise((resolve) => setTimeout(resolve, 600)) const result = await options.updateEntity(entity.id, { isValid: true }) if (!result) return options.clearEntity() await router.push('/') } return { weightData, currentWeightEntry, displayWeight, displayDsd, title, showLoadingBox, fetchWeight, saveWeight, showGenerateReceipt, printReceipt } }