import { stagePrintFile, printFile, applyPrintResult } from '../../utils/printDispatch'

// Receipt (M_InOut) labels → the small product-label printer (labelprinter-2), fit-to-page.
export default defineEventHandler(async (event) => {
  let data: any = {}
  const body = await readBody(event)

  try {
    const filePath = await stagePrintFile(body.pathName, body.fileName, Buffer.from(body.fileContent, 'base64'))
    const result = await printFile(event, { queue: 'labelprinter-2', options: '-o fit-to-page', filePath, source: 'print/inout-labels' })
    data = applyPrintResult({ ...body }, result)
  } catch(err: any) {
    data = err
  }

  return data
})
