{"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: false,\n  latest_by: 'timestamp',\n  scope_key: 'global',\n  tie_policy: 'last',\n  allow_resurrection: false,\n  amount_mode: 'delta',\n  gap_policy: 'ignore',\n  precision: 2,\n  inclusive_age: false,\n  max_age: 120,\n  audit_duplicate: false,\n  order: 'input'\n};\n","helpers.ts":"export type Decimal = { coefficient: bigint; scale: number };\n\nfunction power(n: number): bigint {\n  return BigInt(10) ** BigInt(n);\n}\n\nexport function decimal(text: string): Decimal {\n  const match = /^\\+?(\\d+)(?:\\.(\\d*))?$/.exec(text) || /^\\+?()(?:\\.(\\d+))$/.exec(text);\n  if (!match) throw new Error('Invalid decimal amount');\n  const fraction = match[2] || '';\n  return { coefficient: BigInt((match[1] || '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 * power(scale - a.scale) + b.coefficient * power(scale - b.scale),\n    scale\n  };\n}\n\nexport function fixed(value: Decimal, precision: number): string {\n  let coefficient = value.coefficient;\n  if (value.scale > precision) {\n    const divisor = power(value.scale - precision);\n    const remainder = coefficient % divisor;\n    coefficient = coefficient / divisor;\n    if (remainder * BigInt(2) >= divisor) coefficient += BigInt(1);\n  } else {\n    coefficient *= power(precision - value.scale);\n  }\n  const digits = coefficient.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\nexport function compare(a: any, b: any): number {\n  return a < b ? -1 : a > b ? 1 : 0;\n}\n","core.ts":"import {add, decimal, fixed, compare} from './helpers.ts';\nimport type {Decimal} from './helpers.ts';\n\ntype IndexedEvent = { event: any; index: number };\ntype Group = { tenant: any; entity: any; first: number; events: IndexedEvent[] };\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, Group>();\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    const identity = JSON.stringify(policy.scope_key === 'tenant' ? [event.tenant, event.id] : [event.id]);\n    if (seen.has(identity)) {\n      if (policy.audit_duplicate) duplicates.push(event.id);\n      continue;\n    }\n    seen.add(identity);\n    const key = JSON.stringify([event.tenant, event.entity]);\n    let group = groups.get(key);\n    if (!group) {\n      group = {tenant: event.tenant, entity: event.entity, first: index, events: []};\n      groups.set(key, group);\n    }\n    group.events.push({event, index});\n  }\n\n  const ordered = Array.from(groups.values());\n  ordered.sort(policy.order === 'key'\n    ? (a, b) => compare(a.tenant, b.tenant) || compare(a.entity, b.entity)\n    : (a, b) => a.first - b.first);\n  const field = policy.latest_by === 'seq' ? 'seq' : 'ts';\n  const entities = ordered.map(group => {\n    const bySortValue = new Map<number, IndexedEvent>();\n    for (const item of group.events) {\n      const key = item.event[field];\n      if (!bySortValue.has(key) || policy.tie_policy === 'last') bySortValue.set(key, item);\n    }\n    const retained = Array.from(bySortValue.values());\n    retained.sort((a, b) => compare(a.event[field], b.event[field]));\n    let seq = 0;\n    let value: Decimal = {coefficient: BigInt(0), scale: 0};\n    let deleted = false;\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        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 && retained.some(item => item.event.kind === 'delete')) deleted = true;\n    return {tenant: group.tenant, entity: group.entity, value: fixed(value, policy.precision), deleted};\n  });\n  return {entities, duplicates};\n}\n"}}