{"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: 'tenant',\n  latest_by: 'seq',\n  tie_policy: 'first',\n  delete_wins: true,\n  allow_resurrection: true,\n  amount_mode: 'absolute',\n  gap_policy: 'accept',\n  precision: 0,\n  audit_duplicate: true,\n  order: 'key',\n  max_age: 60,\n  inclusive_age: true\n};\n","helpers.ts":"export type Dec = { n: bigint; s: number };\n\nexport function parseDec(x: any): Dec {\n  const str = String(x).trim();\n  const m = /^\\+?(\\d*)(?:\\.(\\d*))?$/.exec(str);\n  if (!m) return { n: 0n, s: 0 };\n  const ip = m[1] || '0';\n  const fp = m[2] || '';\n  return { n: BigInt(ip + fp), s: fp.length };\n}\n\nexport function addDec(a: Dec, b: Dec): Dec {\n  const s = Math.max(a.s, b.s);\n  const an = a.n * 10n ** BigInt(s - a.s);\n  const bn = b.n * 10n ** BigInt(s - b.s);\n  return { n: an + bn, s };\n}\n\nexport function fmtRound(v: Dec, p: number): string {\n  let q: bigint;\n  if (v.s <= p) {\n    q = v.n * 10n ** BigInt(p - v.s);\n  } else {\n    const d = 10n ** BigInt(v.s - p);\n    q = v.n / d;\n    const r = v.n % d;\n    if (r * 2n >= d) q += 1n;\n  }\n  let str = q.toString();\n  if (p === 0) return str;\n  while (str.length <= p) str = '0' + str;\n  return str.slice(0, str.length - p) + '.' + str.slice(str.length - p);\n}\n\nexport function cmp(a: string, b: string): number {\n  return a < b ? -1 : a > b ? 1 : 0;\n}\n","core.ts":"import { parseDec, addDec, fmtRound, cmp } from './helpers.ts';\n\nexport function run(payload: any, policy: any): any {\n  const now = payload.now;\n  const events: any[] = payload.events || [];\n  const field = policy.latest_by === 'ts' ? 'ts' : 'seq';\n  const kept: any[] = [];\n  for (const e of events) {\n    const age = now - e.ts;\n    if (age > policy.max_age) continue;\n    if (age === policy.max_age && !policy.inclusive_age) continue;\n    kept.push(e);\n  }\n  const seen = new Set<string>();\n  const duplicates: any[] = [];\n  const uniq: any[] = [];\n  for (const e of kept) {\n    const k = policy.scope_key === 'tenant' ? JSON.stringify([e.tenant, e.id]) : JSON.stringify(e.id);\n    if (seen.has(k)) {\n      if (policy.audit_duplicate) duplicates.push(e.id);\n      continue;\n    }\n    seen.add(k);\n    uniq.push(e);\n  }\n  const groups = new Map<string, { tenant: any; entity: any; first: number; evs: any[] }>();\n  uniq.forEach((e, i) => {\n    const k = JSON.stringify([e.tenant, e.entity]);\n    let g = groups.get(k);\n    if (!g) { g = { tenant: e.tenant, entity: e.entity, first: i, evs: [] }; groups.set(k, g); }\n    g.evs.push(e);\n  });\n  const out: any[] = [];\n  for (const g of groups.values()) {\n    const byVal = new Map<number, any>();\n    for (const e of g.evs) {\n      const v = e[field];\n      if (!byVal.has(v) || policy.tie_policy === 'last') byVal.set(v, e);\n    }\n    const evs = Array.from(byVal.values()).sort((a, b) => a[field] - b[field]);\n    let seq = 0;\n    let value = { n: 0n, s: 0 };\n    let deleted = false;\n    let anyDelete = false;\n    for (const e of evs) {\n      if (e.kind === 'delete') anyDelete = true;\n      if (policy.gap_policy === 'ignore' && e.seq !== seq + 1) continue;\n      seq = e.seq;\n      if (e.kind === 'delete') {\n        deleted = true;\n      } else if (e.kind === 'set') {\n        if (!deleted || policy.allow_resurrection) {\n          deleted = false;\n          const a = parseDec(e.amount);\n          value = policy.amount_mode === 'delta' ? addDec(value, a) : a;\n        }\n      }\n    }\n    if (policy.delete_wins && anyDelete) deleted = true;\n    out.push({ first: g.first, tenant: g.tenant, entity: g.entity, value: fmtRound(value, policy.precision), deleted });\n  }\n  if (policy.order === 'key') {\n    out.sort((a, b) => cmp(String(a.tenant), String(b.tenant)) || cmp(String(a.entity), String(b.entity)));\n  } else {\n    out.sort((a, b) => a.first - b.first);\n  }\n  return {\n    entities: out.map(o => ({ tenant: o.tenant, entity: o.entity, value: o.value, deleted: o.deleted })),\n    duplicates\n  };\n}\n"}}