"use strict";function getApiContext(){return asyncLocalStorage.getStore()}function logStep(t,e){const s=getApiContext();s&&s.step(t,e)}function logSuccess(t){const e=getApiContext();e&&e.success(t)}function logFail(t){const e=getApiContext();e&&e.fail(t)}function logWarn(t){const e=getApiContext();e&&e.warn(t)}function logDebug(t){const e=getApiContext();e&&e.debug(t)}function generateRequestId(){requestCounter=(requestCounter+1)%1e6;return`${Date.now().toString(36)}-${requestCounter.toString(36)}`}function createApiContext(t,e,s,o){const n=generateRequestId(),r=[];let a="running";const i=Date.now(),u=logger_1.logger.live(t,e);(null==o?void 0:o.method)&&(null==o?void 0:o.url)&&u.setRequest(o.method,o.url);return{module:t,title:e,requestId:n,userId:s,_steps:r,_status:a,_startTime:i,_liveHandle:u,step(t,e="info"){r.push({message:t,status:e,time:Date.now()});u.step(t,e)},success(t){if(t){r.push({message:t,status:"success",time:Date.now()});u.step(t,"success")}a="success";this._status=a},fail(t){r.push({message:t,status:"error",time:Date.now()});u.step(t,"error");a="error";this._status=a},warn(t){r.push({message:t,status:"warn",time:Date.now()});u.step(t,"warn")},debug(t){if("debug"===process.env.LOG_LEVEL){r.push({message:t,status:"info",time:Date.now()});u.step(t,"info")}}}}function completeOperation(t){if(!t._liveHandle)return;const e=Date.now()-t._startTime;"success"===t._status?t._liveHandle.succeed(`${e}`):t._liveHandle.fail(`${e}`)}async function withLogger(t,e,s,o,n){var r;const a=createApiContext(t,e,null===(r=s.user)||void 0===r?void 0:r.id,n);try{const t=await asyncLocalStorage.run(a,async()=>await o(a));"running"===a._status&&(a._status="success");completeOperation(a);return t}catch(t){"running"===a._status&&a.fail(t instanceof Error?t.message:String(t));completeOperation(a);throw t}}function logged(t,e,s){return async(...o)=>{const n=createApiContext(t,e);try{const t=await asyncLocalStorage.run(n,async()=>await s(n,...o));"running"===n._status&&(n._status="success");completeOperation(n);return t}catch(t){"running"===n._status&&n.fail(t instanceof Error?t.message:String(t));completeOperation(n);throw t}}}async function withSubOperation(t,e){const s=getApiContext();s&&s.step(t);try{const o=await e();s&&s.step(`${t} completed`,"success");return o}catch(e){s&&s.step(`${t} failed: ${e instanceof Error?e.message:e}`,"error");throw e}}Object.defineProperty(exports,"__esModule",{value:!0});exports.getApiContext=getApiContext;exports.logStep=logStep;exports.logSuccess=logSuccess;exports.logFail=logFail;exports.logWarn=logWarn;exports.logDebug=logDebug;exports.withLogger=withLogger;exports.logged=logged;exports.withSubOperation=withSubOperation;const async_hooks_1=require("async_hooks"),logger_1=require("./logger"),asyncLocalStorage=new async_hooks_1.AsyncLocalStorage;let requestCounter=0;