import{existsSync as d,readFileSync as L}from"fs";import{copyFile as p,mkdir as _,readFile as c}from"fs/promises";import{createRequire as S}from"module";import{dirname as B}from"path";import{fileURLToPath as i,pathToFileURL as y}from"url";var z=S(import.meta.url),E=["initdb.wasm","pglite.data","pglite.wasm"],b={amcheck:"amcheck.tar.gz",bloom:"bloom.tar.gz",btree_gin:"btree_gin.tar.gz",btree_gist:"btree_gist.tar.gz",citext:"citext.tar.gz",cube:"cube.tar.gz",dict_int:"dict_int.tar.gz",dict_xsyn:"dict_xsyn.tar.gz",earthdistance:"earthdistance.tar.gz",file_fdw:"file_fdw.tar.gz",fuzzystrmatch:"fuzzystrmatch.tar.gz",hstore:"hstore.tar.gz",intarray:"intarray.tar.gz",isn:"isn.tar.gz",lo:"lo.tar.gz",ltree:"ltree.tar.gz",pageinspect:"pageinspect.tar.gz",pg_buffercache:"pg_buffercache.tar.gz",pg_freespacemap:"pg_freespacemap.tar.gz",pg_surgery:"pg_surgery.tar.gz",pg_trgm:"pg_trgm.tar.gz",pg_visibility:"pg_visibility.tar.gz",pg_walinspect:"pg_walinspect.tar.gz",seg:"seg.tar.gz",tablefunc:"tablefunc.tar.gz",tcn:"tcn.tar.gz",tsm_system_rows:"tsm_system_rows.tar.gz",tsm_system_time:"tsm_system_time.tar.gz",unaccent:"unaccent.tar.gz",uuid_ossp:"uuid-ossp.tar.gz",vector:"vector.tar.gz"},w=[...E.map(e=>({fileName:e,kind:"core",name:e})),...Object.entries(b).map(([e,t])=>({fileName:t,kind:"extension",name:e}))],l=new Map,A=Symbol.for("@prisma/dev/bundled-pglite-runtime-asset-sources"),a=null;async function Y(e){let t=u(e),n=i(t);await _(n,{recursive:!0});let s=[];for(let r of v()){let m=new URL(r.fileName,t),f=i(m);await p(r.sourcePath,f),s.push({...r,destinationPath:f})}return s}function v(){let e=P();return w.map(t=>({...t,sourcePath:i(new URL(t.fileName,e))}))}async function K(){await N();let e=U();if(e)return await G(e),await x(e);let t=M(),n=t.href,s=l.get(n);return s||(s=D(t),l.set(n,s)),await s}function X(e){let t=globalThis;t[A]={extensions:Object.fromEntries(Object.entries(e.extensions).map(([n,s])=>[n,o(s)])),fsBundle:o(e.fsBundle),initdbWasm:o(e.initdbWasm),wasmModule:o(e.wasmModule)}}function o(e,t=import.meta.url){return e instanceof URL?e:/^(?:https?:|data:|file:)/.test(e)?new URL(e):e.startsWith("/")||/^[a-zA-Z]:[\\/]/.test(e)?y(e):new URL(e,t)}async function x(e){let t=`bundled:${e.wasmModule.href}:${e.fsBundle.href}`,n=l.get(t);return n||(n=T(e),l.set(t,n)),await n}async function D(e){let[t,n]=await Promise.all([c(new URL("pglite.data",e)),c(new URL("pglite.wasm",e))]),s=h();return{extensions:Object.fromEntries(Object.entries(b).map(([r,m])=>[r,new URL(m,e)])),fsBundle:new Blob([Uint8Array.from(t)]),wasmModule:await s.compile(n)}}async function T(e){let[t,n]=await Promise.all([R(e.fsBundle),R(e.wasmModule)]),s=h();return{extensions:e.extensions,fsBundle:new Blob([Uint8Array.from(t)]),wasmModule:await s.compile(n)}}async function G(e){if(e.initdbWasm.protocol!=="file:")return;let t=new URL("initdb.wasm",u(new URL("./",e.initdbWasm))),n=i(t);d(n)||await p(i(e.initdbWasm),n)}function u(e){return e instanceof URL?g(e):g(y(e))}function g(e){return e.href.endsWith("/")?e:new URL(`${e.href}/`)}function P(){let e=z.resolve("@electric-sql/pglite");return u(B(e))}function M(){let e=u(new URL("./",import.meta.url));if(k(e))return e;if(I())return P();throw new Error("Unable to locate PGlite runtime assets. If you bundled @prisma/dev, copy them next to the bundle with copyPrismaDevRuntimeAssets().")}function k(e){return w.every(t=>d(i(new URL(t.fileName,e))))}function U(){return globalThis[A]??null}async function N(){!O()||U()||(a||(a=import("./runtime-assets-manifest.bun.js").then(()=>{}).catch(e=>{throw a=null,e})),await a)}function I(){let e=i(new URL("../package.json",import.meta.url));if(!d(e))return!1;try{return JSON.parse(L(e,"utf8")).name==="@prisma/dev"}catch{return!1}}function O(){return typeof globalThis.Bun<"u"}function h(){let e=globalThis.WebAssembly;if(!e)throw new Error("WebAssembly is not available in this runtime.");return e}async function R(e){if(e.protocol==="file:")return await c(e);let t=await fetch(e);if(!t.ok)throw new Error(`Failed to fetch runtime asset ${e.href}: ${t.status} ${t.statusText}`);return new Uint8Array(await t.arrayBuffer())}export{Y as a,v as b,K as c,X as d,o as e};
