import{MessageChannel as O,isMainThread as S}from"worker_threads";import{i as F,s as N}from"../node-features-32913946.mjs";import U from"module";import{installSourceMapSupport as w}from"../source-map.mjs";import d from"path";import{fileURLToPath as _,pathToFileURL as b}from"url";import{b as v,t as A}from"../index-a0bec4d2.mjs";import{r as D}from"../resolve-ts-path-eb3847f5.mjs";import{parseTsconfig as L,getTsconfig as x,createFilesMatcher as J,createPathsMatcher as I}from"get-tsconfig";import P from"fs";import"esbuild";import"crypto";import"os";const $=()=>{const{port1:t,port2:s}=new O;w(),process.send&&t.addListener("message",r=>{r.type==="dependency"&&process.send(r)}),t.unref(),U.register("./index.mjs",{parentURL:import.meta.url,data:{port:s},transferList:[s]})},m=new Map;async function C(t){if(m.has(t))return m.get(t);if(!await P.promises.access(t).then(()=>!0,()=>!1)){m.set(t,void 0);return}const r=await P.promises.readFile(t,"utf8");try{const o=JSON.parse(r);return m.set(t,o),o}catch{throw new Error(`Error parsing: ${t}`)}}async function W(t){let s=new URL("package.json",t);for(;!s.pathname.endsWith("/node_modules/package.json");){const r=_(s),o=await C(r);if(o)return o;const n=s;if(s=new URL("../package.json",s),s.pathname===n.pathname)break}}async function q(t){var s;const r=await W(t);return(s=r==null?void 0:r.type)!=null?s:"commonjs"}const f=process.env.TSX_TSCONFIG_PATH?{path:d.resolve(process.env.TSX_TSCONFIG_PATH),config:L(process.env.TSX_TSCONFIG_PATH)}:x(),G=f&&J(f),T=f&&I(f),E="file://",g=/\.([cm]?ts|[tj]sx)($|\?)/,H=/\.json(?:$|\?)/,X=t=>{const s=d.extname(t);if(s===".json")return"json";if(s===".mjs"||s===".mts")return"module";if(s===".cjs"||s===".cts")return"commonjs"},K=t=>{const s=X(t);if(s)return s;if(g.test(t))return q(t)},k=w(),R=/\/(?:$|\?)/;let u=process.send?process.send.bind(process):void 0;const Q=async t=>{if(!t)throw new Error(`tsx must be loaded with --import instead of --loader The --loader flag was deprecated in Node v20.6.0`);const{port:s}=t;u=s.postMessage.bind(s)},z=({port:t})=>(u=t.postMessage.bind(t),` const require = getBuiltin('module').createRequire("${import.meta.url}"); require('tsx/source-map').installSourceMapSupport(port); if (process.send) { port.addListener('message', (message) => { if (message.type === 'dependency') { process.send(message); } }); } port.unref(); // Allows process to exit without waiting for port to close `),h=async(t,s,r)=>{const o=await t(s,r);return!o.format&&o.url.startsWith(E)&&(o.format=await K(o.url)),o},B=[".js",".json",".ts",".tsx",".jsx"];async function l(t,s,r){const[o,n]=t.split("?");let c;for(const a of B)try{return await h(r,o+a+(n?`?${n}`:""),s)}catch(e){if(c===void 0&&e instanceof Error){const{message:i}=e;e.message=e.message.replace(`${a}'`,"'"),e.stack=e.stack.replace(i,e.message),c=e}}throw c}async function M(t,s,r){const o=R.test(t),n=o?"index":"/index",[c,a]=t.split("?");try{return await l(c+n+(a?`?${a}`:""),s,r)}catch(e){if(!o)try{return await l(t,s,r)}catch{}const i=e,{message:p}=i;throw i.message=i.message.replace(`${n.replace("/",d.sep)}'`,"'"),i.stack=i.stack.replace(p,i.message),i}}const V=/^\.{1,2}\//,j=async function(t,s,r,o){var n;if(R.test(t))return await M(t,s,r);const c=t.startsWith(E)||V.test(t);if(T&&!c&&!((n=s.parentURL)!=null&&n.includes("/node_modules/"))){const a=T(t);for(const e of a)try{return await j(b(e).toString(),s,r)}catch{}}if(g.test(s.parentURL)){const a=D(t);if(a)for(const e of a)try{return await h(r,e,s)}catch(i){const{code:p}=i;if(p!=="ERR_MODULE_NOT_FOUND"&&p!=="ERR_PACKAGE_PATH_NOT_EXPORTED")throw i}}try{return await h(r,t,s)}catch(a){if(a instanceof Error&&!o){const{code:e}=a;if(e==="ERR_UNSUPPORTED_DIR_IMPORT")try{return await M(t,s,r)}catch(i){if(i.code!=="ERR_PACKAGE_IMPORT_NOT_DEFINED")throw i}if(e==="ERR_MODULE_NOT_FOUND")try{return await l(t,s,r)}catch{}}throw a}},y=F?"importAttributes":"importAssertions",Y=async function(t,s,r){var o;u&&u({type:"dependency",path:t}),H.test(t)&&(s[y]||(s[y]={}),s[y].type="json");const n=await r(t,s);if(!n.source)return n;const c=t.startsWith("file://")?_(t):t,a=n.source.toString();if(n.format==="json"||g.test(t)){const e=await v(a,c,{tsconfigRaw:(o=G)==null?void 0:o(c)});return{format:"module",source:k(e)}}if(n.format==="module"){const e=A(c,a);e&&(n.source=k(e))}return n};N&&S&&$();export{z as globalPreload,Q as initialize,Y as load,j as resolve};