"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.MetricsPlugin=void 0,exports.createMetricsPlugin=createMetricsPlugin;const shared_1=require("../../_deps/shared/index.js");class MetricsPlugin{name="metrics";version="1.0.0";description="Tracks performance metrics, message counts, and error rates";priority=20;options;metrics=[];startTime=Date.now();exportTimer;constructor(t={}){this.options={highResolution:t.highResolution??!0,maxMetrics:t.maxMetrics??1e4,onExport:t.onExport??(()=>{}),exportInterval:t.exportInterval??6e4}}onInit(){return this.startTime=Date.now(),this.options.exportInterval>0&&(this.exportTimer=setInterval(()=>{this.export()},this.options.exportInterval)),(0,shared_1.ok)(void 0)}onDestroy(){return this.exportTimer&&(clearInterval(this.exportTimer),this.exportTimer=void 0),this.export(),this.metrics=[],(0,shared_1.ok)(void 0)}onSend(t,e){const r=this.now(),s=this.now();return this.record({timestamp:Date.now(),event:"send",duration:s-r,success:!0}),(0,shared_1.ok)({payload:t})}onReceive(t,e){const r=this.now(),s=this.now();return this.record({timestamp:Date.now(),event:"receive",duration:s-r,success:!0}),(0,shared_1.ok)({payload:t})}onEncrypt(t,e){const r=this.now(),s=this.now();return this.record({timestamp:Date.now(),event:"encrypt",duration:s-r,success:!0}),(0,shared_1.ok)({payload:t})}onDecrypt(t,e){const r=this.now(),s=this.now();return this.record({timestamp:Date.now(),event:"decrypt",duration:s-r,success:!0}),(0,shared_1.ok)({payload:t})}getMetrics(){return[...this.metrics]}getAggregatedMetrics(){const t=this.metrics.filter(t=>"send"===t.event),e=this.metrics.filter(t=>"receive"===t.event),r=this.metrics.filter(t=>"encrypt"===t.event),s=this.metrics.filter(t=>"decrypt"===t.event),i=this.metrics.filter(t=>!t.success),n=t=>0===t.length?0:t.reduce((t,e)=>t+e.duration,0)/t.length,o=this.metrics.length;return{totalSends:t.length,totalReceives:e.length,totalEncrypts:r.length,totalDecrypts:s.length,avgSendDuration:n(t),avgReceiveDuration:n(e),avgEncryptDuration:n(r),avgDecryptDuration:n(s),totalErrors:i.length,errorRate:o>0?i.length/o:0,uptime:Date.now()-this.startTime}}clearMetrics(){this.metrics=[],this.startTime=Date.now()}export(){this.metrics.length>0&&this.options.onExport([...this.metrics])}getMetricsInRange(t,e){return this.metrics.filter(r=>r.timestamp>=t&&r.timestamp<=e)}getMetricsByEvent(t){return this.metrics.filter(e=>e.event===t)}record(t){this.metrics.push(t),this.metrics.length>this.options.maxMetrics&&this.metrics.shift()}now(){return this.options.highResolution&&"undefined"!=typeof performance?performance.now():Date.now()}}function createMetricsPlugin(t){return new MetricsPlugin(t)}exports.MetricsPlugin=MetricsPlugin;