{"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  allow_resurrection: false,\n  amount_mode: 'delta',\n  audit_duplicate: false,\n  delete_wins: false,\n  gap_policy: 'ignore',\n  inclusive_age: false,\n  latest_by: 'timestamp',\n  max_age: 120,\n  order: 'input',\n  precision: 2,\n  scope_key: 'global',\n  tie_policy: 'last'\n};\n","helpers.ts":"export type Decimal = { units: bigint; scale: number };\n\nfunction power10(n: number): bigint {\n  return BigInt(10) ** BigInt(n);\n}\n\nexport function parseDecimal(text: string): Decimal {\n  const parts = text.split('.');\n  const fraction = parts[1] || '';\n  return {\n    units: BigInt((parts[0] || '0') + fraction),\n    scale: fraction.length\n  };\n}\n\nexport function addDecimal(a: Decimal, b: Decimal): Decimal {\n  const scale = Math.max(a.scale, b.scale);\n  return {\n    units: a.units * power10(scale - a.scale) + b.units * power10(scale - b.scale),\n    scale\n  };\n}\n\nexport function formatDecimal(value: Decimal, precision: number): string {\n  let units = value.units;\n  if (value.scale > precision) {\n    const divisor = power10(value.scale - precision);\n    const remainder = units % divisor;\n    units = units / divisor;\n    if (remainder * BigInt(2) >= divisor) units += BigInt(1);\n  } else {\n    units *= power10(precision - value.scale);\n  }\n  const digits = units.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: string | number, b: string | number): number {\n  return a < b ? -1 : a > b ? 1 : 0;\n}\n","core.ts":"import {addDecimal, compare, Decimal, formatDecimal, parseDecimal} from './helpers.ts';\n\ntype IndexedEvent = { event: any; index: number };\ntype Group = { tenant: string; entity: string; firstIndex: number; events: IndexedEvent[] };\n\nexport function run(payload: any, policy: any): any {\n  const duplicates: string[] = [];\n  const seen = new Set<string>();\n  const groups = new Map<string, 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 || (age === policy.max_age && !policy.inclusive_age)) continue;\n\n    const duplicateKey = policy.scope_key === 'tenant'\n      ? JSON.stringify([event.tenant, event.id])\n      : 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, index});\n  }\n\n  const orderedGroups = Array.from(groups.values());\n  orderedGroups.sort((a, b) => policy.order === 'key'\n    ? compare(a.tenant, b.tenant) || compare(a.entity, b.entity)\n    : compare(a.firstIndex, b.firstIndex));\n\n  const field = policy.latest_by === 'seq' ? 'seq' : 'ts';\n  const entities = orderedGroups.map(group => {\n    const byField = new Map<number, IndexedEvent>();\n    for (const item of group.events) {\n      const key = item.event[field];\n      if (!byField.has(key) || policy.tie_policy === 'last') byField.set(key, item);\n    }\n    const retained = Array.from(byField.values());\n    retained.sort((a, b) => compare(a.event[field], b.event[field]));\n\n    let seq = 0;\n    let value: Decimal = {units: BigInt(0), scale: 0};\n    let deleted = false;\n    for (const item of retained) {\n      const event = item.event;\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 = parseDecimal(event.amount);\n        value = policy.amount_mode === 'absolute' ? amount : addDecimal(value, amount);\n      }\n    }\n    if (policy.delete_wins && retained.some(item => item.event.kind === 'delete')) deleted = true;\n    return {\n      tenant: group.tenant,\n      entity: group.entity,\n      value: formatDecimal(value, policy.precision),\n      deleted\n    };\n  });\n\n  return {entities, duplicates};\n}\n"}}