{
  "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  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 power(n: number): bigint {\n  return 10n ** BigInt(n);\n}\n\nexport function parseDecimal(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) + b.coefficient * power(scale - b.scale),\n    scale\n  };\n}\n\nexport function formatDecimal(value: Decimal, precision: number): string {\n  let units: bigint;\n  if (value.scale <= precision) {\n    units = value.coefficient * power(precision - value.scale);\n  } else {\n    const divisor = power(value.scale - precision);\n    units = value.coefficient / divisor;\n    if ((value.coefficient % divisor) * 2n >= divisor) units += 1n;\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",
    "core.ts": "import {add, parseDecimal, formatDecimal} from './helpers.ts';\nimport type {Decimal} from './helpers.ts';\n\ntype Group = {\n  tenant: any;\n  entity: any;\n  firstIndex: number;\n  ties: Map<number, any>;\n};\n\nexport function run(payload: any, policy: any): any {\n  const duplicates: any[] = [];\n  const seenGlobal = new Set<any>();\n  const seenTenants = new Map<any, Set<any>>();\n  const tenants = new Map<any, Map<any, Group>>();\n  const groups: Group[] = [];\n  const field = policy.latest_by === 'sequence' ? 'seq' : 'ts';\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 = seenGlobal;\n    if (policy.scope_key === 'tenant') {\n      let tenantSeen = seenTenants.get(event.tenant);\n      if (!tenantSeen) {\n        tenantSeen = new Set<any>();\n        seenTenants.set(event.tenant, tenantSeen);\n      }\n      seen = tenantSeen;\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 entityGroups = tenants.get(event.tenant);\n    if (!entityGroups) {\n      entityGroups = new Map<any, Group>();\n      tenants.set(event.tenant, entityGroups);\n    }\n    let group = entityGroups.get(event.entity);\n    if (!group) {\n      group = {\n        tenant: event.tenant,\n        entity: event.entity,\n        firstIndex: index,\n        ties: new Map<number, any>()\n      };\n      entityGroups.set(event.entity, group);\n      groups.push(group);\n    }\n    const sortValue = event[field];\n    if (!group.ties.has(sortValue) || policy.tie_policy === 'last') {\n      group.ties.set(sortValue, event);\n    }\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 entities = groups.map(group => {\n    const events = Array.from(group.ties.values()).sort((a, b) => a[field] - b[field]);\n    let seq = 0;\n    let value: Decimal = {coefficient: 0n, scale: 0};\n    let deleted = false;\n    let hasDelete = false;\n    for (const event of events) {\n      if (event.kind === 'delete') hasDelete = true;\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 (!deleted || policy.allow_resurrection) {\n        deleted = false;\n        const amount = parseDecimal(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: formatDecimal(value, policy.precision),\n      deleted\n    };\n  });\n  return {entities, duplicates};\n}\n"
  }
}