import { InventoryValidationError } from './errors.js'

export const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i

export interface InventoryItem {
  skuId: string
  vendorId: string | null
  quantityTotal: number | null
  quantitySold: number
}

export interface ReserveArgs {
  skuId: string
  qty: number
  holderRef: string
  now: Date
  ttlMs?: number
}

export function assertUuid(value: string, field: string): void {
  if (!UUID_RE.test(value)) {
    throw new InventoryValidationError(field)
  }
}

export function assertPositiveInt(qty: number, field = 'qty'): void {
  if (!Number.isInteger(qty) || qty <= 0) {
    throw new InventoryValidationError(field)
  }
}
