{"files":{"main.ts":"import { run } from './core.ts';\nimport { POLICY } from './policy.ts';\nexport function evaluate(payload: any): any { return run(payload, POLICY); }\n","core.ts":"import { parseDecimal, addDecimals, formatDecimal, Decimal } from './helpers.ts';\n\nexport function run(payload: any, policy: any): any {\n  const duplicates: any[] = [];\n  const seen = new Set<string>();\n  const groups = new Map<string, { tenant: any; entity: any; firstIndex: number; events: any[] }>();\n\n  for (let index = 0; index < payload.events.length; index++) {\n    const event = payload.events[index];\n    const age = payload.now - event.ts;\n    if (age > policy.max_age || (age === policy.max_age && !policy.inclusive_age)) continue;\n\n    const duplicateKey = JSON.stringify(policy.scope_key === 'tenant' ? [event.tenant, event.id] : [event.id]);\n    if (seen.has(duplicateKey)) {\n      if (policy.audit_duplicate) duplicates.push(event.id);\n      continue;\n    }\n    seen.add(duplicateKey);\n\n    const groupKey = JSON.stringify([event.tenant, event.entity]);\n    let group = groups.get(groupKey);\n    if (!group) {\n      group = { tenant: event.tenant, entity: event.entity, firstIndex: index, events: [] };\n      groups.set(groupKey, group);\n    }\n    group.events.push(event);\n  }\n\n  const results: { tenant: any; entity: any; value: string; deleted: boolean; firstIndex: number }[] = [];\n  const field = policy.latest_by === 'sequence' || policy.latest_by === 'seq' ? 'seq' : 'ts';\n\n  for (const group of groups.values()) {\n    const bySortValue = new Map<number, any>();\n    for (const event of group.events) {\n      if (!bySortValue.has(event[field]) || policy.tie_policy === 'last') {\n        bySortValue.set(event[field], event);\n      }\n    }\n    const retained = Array.from(bySortValue.values()).sort((a, b) => a[field] - b[field]);\n    let seq = 0;\n    let value: Decimal = { coefficient: BigInt(0), scale: 0 };\n    let deleted = false;\n\n    for (const event of retained) {\n      if (policy.gap_policy === 'ignore' && event.seq !== seq + 1) continue;\n      seq = event.seq;\n      if (event.kind === 'delete') {\n        deleted = true;\n      } else if (event.kind === 'set' && (!deleted || policy.allow_resurrection)) {\n        const amount = parseDecimal(event.amount);\n        value = policy.amount_mode === 'absolute' ? amount : addDecimals(value, amount);\n        deleted = false;\n      }\n    }\n\n    if (policy.delete_wins && retained.some(event => event.kind === 'delete')) deleted = true;\n    results.push({\n      tenant: group.tenant,\n      entity: group.entity,\n      value: formatDecimal(value, policy.precision),\n      deleted,\n      firstIndex: group.firstIndex\n    });\n  }\n\n  const compare = (a: any, b: any): number => a < b ? -1 : a > b ? 1 : 0;\n  results.sort(policy.order === 'key'\n    ? (a, b) => compare(a.tenant, b.tenant) || compare(a.entity, b.entity)\n    : (a, b) => a.firstIndex - b.firstIndex);\n\n  return {\n    entities: results.map(({ tenant, entity, value, deleted }) => ({ tenant, entity, value, deleted })),\n    duplicates\n  };\n}\n","policy.ts":"export const POLICY: any = {\n  allow_resurrection: true,\n  amount_mode: 'absolute',\n  audit_duplicate: true,\n  delete_wins: true,\n  gap_policy: 'accept',\n  inclusive_age: true,\n  latest_by: 'sequence',\n  max_age: 60,\n  order: 'key',\n  precision: 0,\n  scope_key: 'tenant',\n  tie_policy: 'first'\n};\n","helpers.ts":"export type Decimal = { coefficient: bigint; scale: number };\n\nfunction powerOfTen(exponent: number): bigint {\n  return BigInt(10) ** BigInt(exponent);\n}\n\nexport function parseDecimal(text: string): Decimal {\n  const parts = text.split('.');\n  const fraction = parts[1] || '';\n  return {\n    coefficient: BigInt((parts[0] || '0') + fraction),\n    scale: fraction.length\n  };\n}\n\nexport function addDecimals(a: Decimal, b: Decimal): Decimal {\n  const scale = Math.max(a.scale, b.scale);\n  return {\n    coefficient: a.coefficient * powerOfTen(scale - a.scale) + b.coefficient * powerOfTen(scale - b.scale),\n    scale\n  };\n}\n\nexport function formatDecimal(value: Decimal, precision: number): string {\n  let rounded = value.coefficient;\n  if (value.scale > precision) {\n    const divisor = powerOfTen(value.scale - precision);\n    const remainder = rounded % divisor;\n    rounded = rounded / divisor;\n    if (remainder * BigInt(2) >= divisor) rounded += BigInt(1);\n  } else if (value.scale < precision) {\n    rounded *= powerOfTen(precision - value.scale);\n  }\n  const digits = rounded.toString();\n  if (precision === 0) return digits;\n  const padded = digits.padStart(precision + 1, '0');\n  return padded.slice(0, -precision) + '.' + padded.slice(-precision);\n}\n"}}