import type { InventoryItemPayload } from "@/types/inventory"; export function getErrorMessage(error: unknown) { if (typeof error === "object" && error !== null && "response" in error) { const response = (error as { response?: { data?: { detail?: string; message?: string } } }).response; return response?.data?.detail ?? response?.data?.message ?? "Aktion konnte nicht abgeschlossen werden"; } return "Aktion konnte nicht abgeschlossen werden"; } export function formatDate(value: string) { return new Intl.DateTimeFormat("de-DE", { dateStyle: "short", timeStyle: "short" }).format(new Date(value)); } export function formatMoney(cents: number | null, currency = "EUR") { if (cents === null) { return "Keine Angabe"; } return new Intl.NumberFormat("de-DE", { style: "currency", currency }).format(cents / 100); } export function euroToCents(value: string) { const normalized = value.trim().replace(",", "."); if (!normalized) { return null; } const amount = Number(normalized); if (!Number.isFinite(amount) || amount < 0) { return null; } return Math.round(amount * 100); } export function centsToEuroInput(cents: number | null) { if (cents === null) { return ""; } return (cents / 100).toFixed(2).replace(".", ","); } export const emptyItemPayload: InventoryItemPayload = { sku: null, name: "", description: null, category_id: null, manufacturer: null, manufacturer_part_number: null, supplier_id: null, supplier_part_number: null, compatible_devices: null, location_id: null, quantity_on_hand: 0, quantity_reserved: 0, reorder_level: 0, unit: "Stk.", purchase_price_cents: null, selling_price_cents: null, currency: "EUR", tax_rate_percent: "19.00", notes: null, is_active: true, };