// @bun
var{defineProperty:a,getOwnPropertyNames:s,getOwnPropertyDescriptor:y}=Object,c=Object.prototype.hasOwnProperty;function u(e){return this[e]}var h=(e)=>{var t=(l??=new WeakMap).get(e),n;if(t)return t;if(t=a({},"__esModule",{value:!0}),e&&typeof e==="object"||typeof e==="function"){for(var o of s(e))if(!c.call(t,o))a(t,o,{get:u.bind(e,o),enumerable:!(n=y(e,o))||n.enumerable})}return l.set(e,t),t},l;var p=(e)=>e;function f(e,t){this[e]=p.bind(null,t)}var b=(e,t)=>{for(var n in t)a(e,n,{get:t[n],enumerable:!0,configurable:!0,set:f.bind(t,n)})};var d=(e,t)=>()=>(e&&(t=e(e=0)),t);class v{onAnomaly;now;samples=new Map;constructor(e={}){this.onAnomaly=e.onAnomaly,this.now=e.now??(()=>new Date)}observe(e){let t=e.definition.anomalyDetection;if(!t?.enabled)return;if(!t.thresholds?.length)return;let n=`${e.name}.v${e.version}`,i=(this.samples.get(n)??0)+1;if(this.samples.set(n,i),typeof t.minimumSample==="number"&&i<t.minimumSample)return;for(let r of t.thresholds){let m=this.extractMetric(e,r.metric);if(typeof m!=="number")continue;if(typeof r.min==="number"&&m<r.min)this.emit(e,t,r.metric,m,"min");if(typeof r.max==="number"&&m>r.max)this.emit(e,t,r.metric,m,"max")}}extractMetric(e,t){let n=e.properties[t];if(typeof n==="number")return n;if(typeof n==="object"&&n!==null&&"value"in n){let o=n.value;return typeof o==="number"?o:void 0}return}emit(e,t,n,o,i){this.onAnomaly?.({dispatch:e,threshold:t,metric:n,value:o,type:i})}reset(){this.samples.clear()}}export{v as TelemetryAnomalyMonitor};
