import { apiFetch, apiFetchBlob } from "@/lib/auth/api-client"; import { CreateLabelTemplateInput, LabelTemplate, UpdateLabelTemplateInput } from "./types"; export function fetchLabelTemplates(): Promise { return apiFetch("/label-templates"); } export function createLabelTemplate(input: CreateLabelTemplateInput): Promise { return apiFetch("/label-templates", { method: "POST", body: JSON.stringify(input), }); } export function updateLabelTemplate( id: string, input: UpdateLabelTemplateInput, ): Promise { return apiFetch(`/label-templates/${encodeURIComponent(id)}`, { method: "PATCH", body: JSON.stringify(input), }); } export function deactivateLabelTemplate(id: string): Promise { return apiFetch(`/label-templates/${encodeURIComponent(id)}`, { method: "DELETE" }); } export interface PrintLabelsOptions { /** 1 = top left of the sheet; omitted prints from the start. */ startPosition?: number; /** A test print stamped „MUSTER – nicht verkaufsfähig“; always allowed. */ proof?: boolean; } /** * The label PDF. A 409 `LABEL_PARTICULARS_MISSING` throws an `ApiError` whose * `body.gaps` lists what is missing (`labelParticularsMissing` reads it). */ export async function printLabels( templateId: string, bottlingBatchId: string, options: PrintLabelsOptions = {}, ): Promise { const params = new URLSearchParams(); if (options.startPosition && options.startPosition > 1) { params.set("startPosition", String(options.startPosition)); } if (options.proof) params.set("proof", "true"); const query = params.size > 0 ? `?${params.toString()}` : ""; const { blob } = await apiFetchBlob( `/label-templates/${encodeURIComponent(templateId)}/print/${encodeURIComponent(bottlingBatchId)}${query}`, "etiketten.pdf", { cache: "no-store" }, ); return blob; }