"use strict";var O=require("node:worker_threads"),p=require("../node-features-CKvB621_.cjs"),R=require("../register-BIOvi1NN.cjs"),c=require("../resolve-ts-path-VlOGJlUT.cjs"),g=require("node:url"),P=require("../index-DTCJysGI.cjs"),_=require("../client-DjommMgI.cjs"),E=require("node:path"),T=require("node:fs");require("node:module"),require("get-tsconfig"),require("esbuild"),require("node:crypto"),require("node:os"),require("../temporary-directory-B83uKxJF.cjs"),require("node:net"),require("../get-pipe-path-b8D5AZgV.cjs");const d={active:!0},N=async e=>{if(!e)throw new Error(`tsx must be loaded with --import instead of --loader The --loader flag was deprecated in Node v20.6.0 and v18.19.0`);d.namespace=e.namespace,e.tsconfig!==!1&&c.loadTsconfig(e.tsconfig??process.env.TSX_TSCONFIG_PATH),e.port&&(d.port=e.port,e.port.on("message",r=>{r==="deactivate"&&(d.active=!1,e.port.postMessage({type:"deactivated"}))}))},U=()=>(c.loadTsconfig(process.env.TSX_TSCONFIG_PATH),"process.setSourceMapsEnabled(true);"),f=new Map,S=async e=>{if(f.has(e))return f.get(e);if(!await T.promises.access(e).then(()=>!0,()=>!1)){f.set(e,void 0);return}const t=await T.promises.readFile(e,"utf8");try{const s=JSON.parse(t);return f.set(e,s),s}catch{throw new Error(`Error parsing: ${e}`)}},F=async e=>{let r=new URL("package.json",e);for(;!r.pathname.endsWith("/node_modules/package.json");){const t=g.fileURLToPath(r),s=await S(t);if(s)return s;const o=r;if(r=new URL("../package.json",r),r.pathname===o.pathname)break}},j=async e=>(await F(e))?.type??"commonjs",D=e=>{const r=e.indexOf("?"),t=E.extname(r===-1?e:e.slice(0,r));if(t===".json")return"json";if(t===".mjs"||t===".mts")return"module";if(t===".cjs"||t===".cts")return"commonjs"},J=e=>{const r=D(e);if(r)return r;if(c.tsExtensionsPattern.test(e))return j(e)},m="tsx-namespace=",l=e=>{const r=e.indexOf(m);if(r===-1)return;const t=e[r-1];if(t!=="?"&&t!=="&")return;const s=r+m.length,o=e.indexOf("&",s);return o===-1?e.slice(s):e.slice(s,o)},h=p.isFeatureSupported(p.importAttributes)?"importAttributes":"importAssertions",M=async(e,r,t)=>{if(!d.active||d.namespace&&d.namespace!==l(e))return t(e,r);if(d.port){const n=new URL(e);n.searchParams.delete("tsx-namespace"),d.port.postMessage({type:"load",url:n.toString()})}_.parent.send&&_.parent.send({type:"dependency",path:e}),c.isJsonPattern.test(e)&&(r[h]||(r[h]={}),r[h].type="json");const s=await t(e,r);if(!s.source)return s;const o=e.startsWith("file://")?g.fileURLToPath(e):e,a=s.source.toString();if(s.format==="json"||c.tsExtensionsPattern.test(e)){const n=await P.transform(a,o,{tsconfigRaw:c.fileMatcher?.(o)});return{format:"module",source:c.inlineSourceMap(n)}}if(s.format==="module"){const n=P.transformDynamicImport(o,a);n&&(s.source=c.inlineSourceMap(n))}return s},y=async(e,r,t)=>{const s=await e(r,t);return!s.format&&s.url.startsWith(c.fileUrlPrefix)&&(s.format=await J(s.url)),s},A=[".js",".json",".ts",".tsx",".jsx"],w=async(e,r,t)=>{const[s,o]=e.split("?");let a;for(const n of A)try{return await y(t,s+n+(o?`?${o}`:""),r)}catch(i){if(a===void 0&&i instanceof Error){const{message:u}=i;i.message=i.message.replace(`${n}'`,"'"),i.stack=i.stack.replace(u,i.message),a=i}}throw a},q=async(e,r,t)=>{const s=c.isDirectoryPattern.test(e),o=s?"index":"/index",[a,n]=e.split("?");try{return await w(a+o+(n?`?${n}`:""),r,t)}catch(i){if(!s)try{return await w(e,r,t)}catch{}const u=i,{message:v}=u;throw u.message=u.message.replace(`${o.replace("/",E.sep)}'`,"'"),u.stack=u.stack.replace(v,u.message),u}},k=async(e,r,t,s)=>{if(!d.active)return t(e,r);const o=r.parentURL&&l(r.parentURL);if(c.requestAcceptsQuery(e)){let a=l(e);if(o&&!a&&(a=o,e+=`${e.includes("?")?"&":"?"}${m}${o}`),d.namespace&&d.namespace!==a)return t(e,r);if(c.isDirectoryPattern.test(e))return await q(e,r,t)}else if(c.tsconfigPathsMatcher&&!r.parentURL?.includes("/node_modules/")){const a=c.tsconfigPathsMatcher(e);for(const n of a)try{return await k(g.pathToFileURL(n).toString(),r,t)}catch{}}if(c.tsExtensionsPattern.test(r.parentURL)||c.allowJs){const a=c.resolveTsPath(e);if(a)for(const n of a)try{return await y(t,n,r)}catch(i){const{code:u}=i;if(u!=="ERR_MODULE_NOT_FOUND"&&u!=="ERR_PACKAGE_PATH_NOT_EXPORTED")throw i}}try{const a=await y(t,e,r);if(c.requestAcceptsQuery(a.url)){const n=l(a.url);o&&!n&&(a.url+=`${a.url.includes("?")?"&":"?"}${m}${o}`)}return a}catch(a){if(a instanceof Error&&!s){const{code:n}=a;if(n==="ERR_UNSUPPORTED_DIR_IMPORT")try{return await q(e,r,t)}catch(i){if(i.code!=="ERR_PACKAGE_IMPORT_NOT_DEFINED")throw i}if(n==="ERR_MODULE_NOT_FOUND")try{return await w(e,r,t)}catch{}}throw a}};p.isFeatureSupported(p.moduleRegister)&&O.isMainThread&&R.register(),exports.globalPreload=U,exports.initialize=N,exports.load=M,exports.resolve=k;