import type { KbxBarcodeEvent, KbxBarcodeSource } from '@kbx/contracts'

export function normalizeKbxBarcode(rawValue:string,source:KbxBarcodeSource,minLength=3,maxLength=256,occurredAt=Date.now()):KbxBarcodeEvent|null{
  const normalizedValue=rawValue.trim()
  if(normalizedValue.length<minLength||normalizedValue.length>maxLength)return null
  return {rawValue,normalizedValue,source,occurredAt}
}

export function isKbxBarcodeDuplicate(current:KbxBarcodeEvent,last:Pick<KbxBarcodeEvent,'normalizedValue'|'occurredAt'>|null,debounceMs:number){
  return Boolean(last&&current.normalizedValue===last.normalizedValue&&current.occurredAt-last.occurredAt<debounceMs)
}
