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