"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.DualModeAdapter=exports.DualModeErrorCode=void 0;const shared_1=require("../_deps/shared/index.js");var DualModeErrorCode;!function(a){a.BOTH_FAILED="DUAL_MODE_BOTH_FAILED",a.XBIND_FAILED="DUAL_MODE_XBIND_FAILED",a.FALLBACK_FAILED="DUAL_MODE_FALLBACK_FAILED",a.INVALID_CONFIG="DUAL_MODE_INVALID_CONFIG"}(DualModeErrorCode||(exports.DualModeErrorCode=DualModeErrorCode={}));class DualModeAdapter{xbind;fallback;metrics;constructor(a={}){this.xbind=a.xbind||null,this.fallback=a.fallback||null,this.metrics={totalCalls:0,xbindCalls:0,fallbackCalls:0,xbindPercentage:0,lastCall:(new Date).toISOString(),lastCallMethod:"xbind"}}setXBind(a){this.xbind=a}removeFallback(){this.fallback=null}async call(a,e={}){if(this.metrics.totalCalls++,this.xbind)try{const r=await this.callViaXBind(a,e);if(r.ok)return this.metrics.xbindCalls++,this.updateMetrics("xbind"),r;if(this.fallback){const t=await this.callViaFallback(a,e);return t.ok?(this.metrics.fallbackCalls++,this.updateMetrics("fallback"),t):(0,shared_1.err)({code:DualModeErrorCode.BOTH_FAILED,message:"Both XBind and fallback failed",xbindError:r.error,fallbackError:t.error})}return(0,shared_1.err)({code:DualModeErrorCode.XBIND_FAILED,message:"XBind failed and no fallback configured",xbindError:r.error})}catch(r){if(this.fallback){const r=await this.callViaFallback(a,e);if(r.ok)return this.metrics.fallbackCalls++,this.updateMetrics("fallback"),r}return(0,shared_1.err)({code:DualModeErrorCode.XBIND_FAILED,message:r instanceof Error?r.message:"XBind exception",xbindError:r})}if(this.fallback){const r=await this.callViaFallback(a,e);return r.ok?(this.metrics.fallbackCalls++,this.updateMetrics("fallback"),r):(0,shared_1.err)({code:DualModeErrorCode.FALLBACK_FAILED,message:"Fallback failed",fallbackError:r.error})}return(0,shared_1.err)({code:DualModeErrorCode.INVALID_CONFIG,message:"Neither XBind nor fallback configured"})}getMetrics(){return{...this.metrics}}resetMetrics(){this.metrics={totalCalls:0,xbindCalls:0,fallbackCalls:0,xbindPercentage:0,lastCall:(new Date).toISOString(),lastCallMethod:"xbind"}}async callViaXBind(a,e){if(!this.xbind)return(0,shared_1.err)({message:"XBind not configured"});try{const r=this.xbind.did;return await this.xbind.send({to:r,payload:{action:a,params:e},scope:a})}catch(a){return(0,shared_1.err)({message:a instanceof Error?a.message:"Unknown error"})}}async callViaFallback(a,e){if(!this.fallback)return(0,shared_1.err)({message:"Fallback not configured"});try{const r=`${this.fallback.url}/${a}`,t=this.fallback.method||"POST",l={"Content-Type":"application/json",...this.fallback.headers};"api-key"===this.fallback.type&&this.fallback.key&&(l.Authorization=`Bearer ${this.fallback.key}`);const s=await fetch(r,{method:t,headers:l,body:"GET"!==t?JSON.stringify(e):void 0});if(!s.ok)return(0,shared_1.err)({message:`HTTP ${s.status}`});const i=await s.json();return(0,shared_1.ok)(i)}catch(a){return(0,shared_1.err)({message:a instanceof Error?a.message:"Network error"})}}updateMetrics(a){this.metrics.lastCall=(new Date).toISOString(),this.metrics.lastCallMethod=a,this.metrics.xbindPercentage=this.metrics.totalCalls>0?this.metrics.xbindCalls/this.metrics.totalCalls*100:0}}exports.DualModeAdapter=DualModeAdapter;