{"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 {addDecimal, parseDecimal, formatDecimal, compareKeys, Decimal} from './helpers.ts';\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 tenants = new Map<any, Map<any, any>>();\n  const groups: 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    let seen = globalSeen;\n    if (policy.scope_key === 'tenant') {\n      let local = tenantSeen.get(event.tenant);\n      if (!local) {\n        local = new Set<any>();\n        tenantSeen.set(event.tenant, local);\n      }\n      seen = local;\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 = tenants.get(event.tenant);\n    if (!entities) {\n      entities = new Map<any, any>();\n      tenants.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  const results: any[] = [];\n  const field = policy.latest_by === 'sequence' ? 'seq' : 'ts';\n  for (const group of groups) {\n    const ties = new Map<any, any>();\n    for (const event of group.events) {\n      const key = event[field];\n      if (!ties.has(key) || policy.tie_policy === 'last') ties.set(key, event);\n    }\n    const retained = Array.from(ties.values());\n    retained.sort((a, b) => compareKeys(a[field], b[field]));\n\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 = parseDecimal(event.amount);\n        value = policy.amount_mode === 'absolute' ? amount : addDecimal(value, amount);\n      }\n    }\n    if (policy.delete_wins && retained.some(event => event.kind === 'delete')) deleted = true;\n    results.push({\n      firstIndex: group.firstIndex,\n      output: {\n        tenant: group.tenant,\n        entity: group.entity,\n        value: formatDecimal(value, policy.precision),\n        deleted\n      }\n    });\n  }\n\n  if (policy.order === 'key') {\n    results.sort((a, b) => compareKeys(a.output.tenant, b.output.tenant) || compareKeys(a.output.entity, b.output.entity));\n  } else {\n    results.sort((a, b) => a.firstIndex - b.firstIndex);\n  }\n  return {entities: results.map(result => result.output), duplicates};\n}\n","policy.ts":"export const POLICY: any = {\n  scope_key: 'global',\n  latest_by: 'timestamp',\n  tie_policy: 'last',\n  delete_wins: false,\n  allow_resurrection: false,\n  amount_mode: 'delta',\n  precision: 2,\n  gap_policy: 'ignore',\n  audit_duplicate: false,\n  order: 'input',\n  max_age: 120,\n  inclusive_age: false\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 {coefficient: BigInt((parts[0] || '0') + fraction), scale: fraction.length};\n}\n\nexport function addDecimal(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: bigint;\n  if (value.scale <= precision) {\n    rounded = value.coefficient * powerOfTen(precision - value.scale);\n  } else {\n    const divisor = powerOfTen(value.scale - precision);\n    rounded = value.coefficient / divisor;\n    if ((value.coefficient % divisor) * BigInt(2) >= divisor) rounded += BigInt(1);\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 compareKeys(a: any, b: any): number {\n  return a < b ? -1 : a > b ? 1 : 0;\n}\n"}}