// @bun
var{defineProperty:s,getOwnPropertyNames:y,getOwnPropertyDescriptor:d}=Object,p=Object.prototype.hasOwnProperty;function f(e){return this[e]}var k=(e)=>{var r=(c??=new WeakMap).get(e),n;if(r)return r;if(r=s({},"__esModule",{value:!0}),e&&typeof e==="object"||typeof e==="function"){for(var i of y(e))if(!p.call(r,i))s(r,i,{get:f.bind(e,i),enumerable:!(n=d(e,i))||n.enumerable})}return c.set(e,r),r},c;var g=(e)=>e;function u(e,r){this[e]=g.bind(null,r)}var D=(e,r)=>{for(var n in r)s(e,n,{get:r[n],enumerable:!0,configurable:!0,set:u.bind(r,n)})};var R=(e,r)=>()=>(e&&(r=e(e=0)),r);function v(){return globalThis.crypto?.randomUUID?.()??`id_${Date.now().toString(36)}_${Math.random().toString(36).slice(2,10)}`}var T=(e)=>{if(e==null)return e;if(typeof e==="string")return"REDACTED";if(typeof e==="number")return 0;if(typeof e==="boolean")return!1;if(Array.isArray(e))return e.map(()=>"REDACTED");if(typeof e==="object")return Object.fromEntries(Object.keys(e).map((r)=>[r,"REDACTED"]));return"REDACTED"};class h{providers=new Map;registry;anomalyMonitor;random;clock;constructor(e){this.registry=e.registry,this.anomalyMonitor=e.anomalyMonitor,this.random=e.random??Math.random,this.clock=e.clock??(()=>new Date);for(let r of e.providers??[])this.providers.set(r.id,r)}registerProvider(e){this.providers.set(e.id,e)}unregisterProvider(e){this.providers.delete(e)}async track(e,r,n,i={}){let t=this.registry.findEventDef(e,r);if(!t)return!1;let o=this.registry.getSpecForEvent(t.key,t.version);if(!o)return!1;if(!this.shouldSample(t.sampling??o.config?.defaultSamplingRate))return!1;let m=this.redactProperties(t,n),a={id:v(),name:t.key,version:t.version,occurredAt:this.clock().toISOString(),properties:m,privacy:t.privacy,context:i,tags:t.tags,spec:o,definition:t};return await Promise.all([...this.providers.values()].map((l)=>l.send(a))),this.anomalyMonitor?.observe(a),!0}shouldSample(e){if(typeof e==="number")return this.random()<e;if(!e)return!0;return this.random()<e.rate}redactProperties(e,r){let n={};for(let[i,t]of Object.entries(r)){let o=e.properties[i];if(!o){n[i]=t;continue}if(o.redact||o.pii||e.privacy==="sensitive")n[i]=T(t);else n[i]=t}return n}}export{h as TelemetryTracker};
