!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var o=t();for(var a in o)("object"==typeof exports?exports:e)[a]=o[a]}}(global,(()=>(()=>{"use strict";var e={n:t=>{var o=t&&t.__esModule?()=>t.default:()=>t;return e.d(o,{a:o}),o},d:(t,o)=>{for(var a in o)e.o(o,a)&&!e.o(t,a)&&Object.defineProperty(t,a,{enumerable:!0,get:o[a]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{default:()=>u});const o=require("@babel/core"),a=require("crypto"),n=require("@code-hud/hud-node-sdk/config");var i=e.n(n);class r extends Error{code=1e3;name=this.constructor.name;constructor(e){super(e)}}class s extends r{code=1004}class d extends s{code=1005}const l=i().hudModuleName,c={babelrc:!1,configFile:!1};class p{functionID;body;awaitable;function_id_placeholder;body_placeholder;awaitable_placeholder;constructor(e,t,o,a,n,i){this.functionID=e,this.body=t,this.awaitable=o,this.function_id_placeholder=a,this.body_placeholder=n,this.awaitable_placeholder=i}}const h={VariableDeclarator:{exit(e,t){o.types.isIdentifier(e.node.init)&&e.node.init.name===t.function_id_placeholder&&(e.node.init=o.types.stringLiteral(t.functionID))}},CallExpression:{exit(e,t){o.types.isIdentifier(e.node.arguments[0])&&e.node.arguments[0].name===t.awaitable_placeholder&&(e.node.arguments[0]=t.awaitable)}},BlockStatement:{exit(e,t){o.types.isExpressionStatement(e.node.body[0])&&o.types.isIdentifier(e.node.body[0].expression)&&e.node.body[0].expression.name===t.body_placeholder&&(e.node.body=t.body)}}};function u({types:e}){return{pre(){const t=o.parseSync(this.opts.functionWrapper,c);if(!t)throw new d("Failed parsing ast");const a=o.parseSync(this.opts.awaitWrapper,c);if(!a)throw new d("Failed parsing ast");const n=o.parseSync(this.opts.fixStackFunction,c);if(!n)throw new d("Failed parsing ast");this.hud_state={hudImportExists:!1,wrapperTemplate:()=>e.cloneDeepWithoutLoc(t),awaitableTemplate:()=>e.cloneDeepWithoutLoc(a),fixStackFunction:n.program.body[0],callDepth:0}},visitor:{ImportDeclaration(e){e.node.source.value===l&&(this.hud_state.hudImportExists=!0)},Program:{exit(e){const t=[];for(const e of this.opts.initialStatements)t.push(o.template.statement(e)());e.node.body.unshift(...t)}},Function:{enter(){this.hud_state.callDepth+=1},exit(t){this.hud_state.callDepth-=1;const n=a.randomUUID();let i=[];i=e.isBlockStatement(t.node.body)?t.node.body.body:[e.returnStatement(t.node.body)];const r=new p(n,i,e.nullLiteral(),this.opts.functionIdPlaceholder,this.opts.bodyPlaceholder,this.opts.awaitablePlaceholder),s=this.hud_state.wrapperTemplate();o.traverse(s,h,void 0,r),t.node.body=e.blockStatement(s.program.body)}},AwaitExpression:{exit(t){if(e.isIdentifier(t.node.argument)&&t.node.argument.name===this.opts.awaitableVariable)return;if(this.hud_state.callDepth<=0)return;const a=new p("",[],t.node.argument,this.opts.functionIdPlaceholder,this.opts.bodyPlaceholder,this.opts.awaitablePlaceholder),n=this.hud_state.awaitableTemplate();if(o.traverse(n,h,void 0,a),!e.isExpressionStatement(n.program.body[0]))throw new d("Expected expression statement");t.replaceWith(n.program.body[0].expression)}},TryStatement:{exit(e){this.hud_state.callDepth<=0||(e.node.handler&&e.node.handler.body.body.unshift(this.hud_state.fixStackFunction),e.node.finalizer&&e.node.finalizer.body.unshift(this.hud_state.fixStackFunction))}}}}}return t})()));