{"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 10n ** BigInt(n);\n}\n\nexport function decimal(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 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) +\n      b.coefficient * power(scale - b.scale),\n    scale\n  };\n}\n\nexport function format(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 /= divisor;\n    if (remainder * 2n >= divisor) coefficient += 1n;\n  } else {\n    coefficient *= power(precision - value.scale);\n  }\n  const digits = coefficient.toString().padStart(precision + 1, '0');\n  if (precision === 0) return digits;\n  return digits.slice(0, -precision) + '.' + digits.slice(-precision);\n}\n","core.ts":"import {decimal, add, format, type Decimal} from './helpers.ts';\n\ntype Group = {\n  tenant: any;\n  entity: any;\n  firstIndex: number;\n  events: any[];\n};\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, 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 ||\n        (age === policy.max_age && !policy.inclusive_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 = tenants.get(event.tenant);\n    if (!entities) {\n      entities = new Map<any, Group>();\n      tenants.set(event.tenant, entities);\n    }\n    let group = entities.get(event.entity);\n    if (!group) {\n      group = {\n        tenant: event.tenant,\n        entity: event.entity,\n        firstIndex: index,\n        events: []\n      };\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) => {\n      if (a.tenant < b.tenant) return -1;\n      if (a.tenant > b.tenant) return 1;\n      if (a.entity < b.entity) return -1;\n      if (a.entity > b.entity) return 1;\n      return 0;\n    });\n  } else {\n    groups.sort((a, b) => a.firstIndex - b.firstIndex);\n  }\n\n  const field = policy.latest_by === 'sequence' ? 'seq' : 'ts';\n  const entities = groups.map(group => {\n    const byField = new Map<number, any>();\n    for (const event of group.events) {\n      const key = event[field];\n      if (!byField.has(key) || policy.tie_policy === 'last') {\n        byField.set(key, event);\n      }\n    }\n    const retained = Array.from(byField.values());\n    retained.sort((a, b) => a[field] - b[field]);\n\n    let seq = 0;\n    let value: Decimal = {coefficient: 0n, 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(event => event.kind === 'delete')) {\n      deleted = true;\n    }\n    return {\n      tenant: group.tenant,\n      entity: group.entity,\n      value: format(value, policy.precision),\n      deleted\n    };\n  });\n  return {entities, duplicates};\n}\n"}}