"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.globalTimeoutConfig=exports.OperationTimeoutController=exports.TimeoutConfig=exports.DEFAULT_TIMEOUTS=exports.TimeoutError=void 0,exports.createTimeoutController=createTimeoutController,exports.withTimeout=withTimeout,exports.withTimeoutResult=withTimeoutResult,exports.createOperationTimeoutSignal=createOperationTimeoutSignal,exports.createOperationTimeout=createOperationTimeout,exports.isTimeoutError=isTimeoutError,exports.getTimeoutFromError=getTimeoutFromError,exports.createTimeoutConfigFromEnv=createTimeoutConfigFromEnv;const shared_1=require("../_deps/shared/index.js");class TimeoutError extends Error{code="TIMEOUT";operation;timeoutMs;timestamp;constructor(t,e){super(`Operation '${t}' timed out after ${e}ms. Actions: (1) Check network connectivity, (2) Increase timeout threshold, (3) Verify service availability, (4) Implement retry with exponential backoff.`),this.name="TimeoutError",this.operation=t,this.timeoutMs=e,this.timestamp=Date.now()}static withContext(t,e,r){const o=new TimeoutError(t,e);return r&&Object.assign(o,{context:r}),o}}exports.TimeoutError=TimeoutError,exports.DEFAULT_TIMEOUTS={registry:1e4,gateway:15e3,transport:3e4,default:3e4};class TimeoutConfig{registry;gateway;transport;default;constructor(t={}){this.registry=t.registry??exports.DEFAULT_TIMEOUTS.registry,this.gateway=t.gateway??exports.DEFAULT_TIMEOUTS.gateway,this.transport=t.transport??exports.DEFAULT_TIMEOUTS.transport,this.default=t.default??exports.DEFAULT_TIMEOUTS.default,this.validateTimeout("registry",this.registry),this.validateTimeout("gateway",this.gateway),this.validateTimeout("transport",this.transport),this.validateTimeout("default",this.default)}getRegistry(t){return t??this.registry}getGateway(t){return t??this.gateway}getTransport(t){return t??this.transport}getDefault(t){return t??this.default}get(t,e){switch(t){case"registry":return this.getRegistry(e);case"gateway":return this.getGateway(e);case"transport":return this.getTransport(e);case"default":return this.getDefault(e)}}with(t){return new TimeoutConfig({registry:t.registry??this.registry,gateway:t.gateway??this.gateway,transport:t.transport??this.transport,default:t.default??this.default})}validateTimeout(t,e){if(!Number.isFinite(e)||e<=0)throw new Error(`Invalid timeout for '${t}': ${e}. Must be positive finite number.`);e>3e5&&console.warn(`[xbind:timeout] Large timeout for '${t}': ${e}ms. Consider reducing to avoid indefinite hangs.`)}toJSON(){return{registry:this.registry,gateway:this.gateway,transport:this.transport,default:this.default}}}exports.TimeoutConfig=TimeoutConfig;class OperationTimeoutController{signal;timeoutMs;operation;controller;timer;timedOut=!1;constructor(t,e="operation"){this.controller=new AbortController,this.signal=this.controller.signal,this.timeoutMs=t,this.operation=e,this.timer=setTimeout(()=>{this.timedOut=!0,this.controller.abort()},t)}cancel(){clearTimeout(this.timer),this.controller.signal.aborted||this.controller.abort()}clear(){clearTimeout(this.timer)}didTimeout(){return this.timedOut}getTimeoutError(){return this.timedOut?new TimeoutError(this.operation,this.timeoutMs):null}}function createTimeoutController(t,e="operation"){return new OperationTimeoutController(t,e)}async function withTimeout(t,e,r="operation"){const o=createTimeoutController(e,r);try{const i=await Promise.race([t(),new Promise((t,i)=>{o.signal.addEventListener("abort",()=>{i(new TimeoutError(r,e))})})]);return o.clear(),i}catch(t){throw o.clear(),t}}async function withTimeoutResult(t,e,r="operation"){try{const o=await withTimeout(t,e,r);return(0,shared_1.ok)(o)}catch(t){if(t instanceof TimeoutError)return(0,shared_1.err)("TIMEOUT");throw t}}function createOperationTimeoutSignal(t,e="operation"){return createTimeoutController(t,e).signal}function createOperationTimeout(t,e,r,o){return createTimeoutController(t.get(e,o),r)}function isTimeoutError(t){return t instanceof TimeoutError}function getTimeoutFromError(t){return isTimeoutError(t)?t.timeoutMs:void 0}function createTimeoutConfigFromEnv(){const t={},e=process.env.XBIND_TIMEOUT_REGISTRY;if(e){const r=parseInt(e,10);isNaN(r)||(t.registry=r)}const r=process.env.XBIND_TIMEOUT_GATEWAY;if(r){const e=parseInt(r,10);isNaN(e)||(t.gateway=e)}const o=process.env.XBIND_TIMEOUT_TRANSPORT;if(o){const e=parseInt(o,10);isNaN(e)||(t.transport=e)}const i=process.env.XBIND_TIMEOUT_DEFAULT;if(i){const e=parseInt(i,10);isNaN(e)||(t.default=e)}return new TimeoutConfig(t)}exports.OperationTimeoutController=OperationTimeoutController,exports.globalTimeoutConfig=createTimeoutConfigFromEnv();