// @bun
var{defineProperty:k,getOwnPropertyNames:X,getOwnPropertyDescriptor:F}=Object,N=Object.prototype.hasOwnProperty;function _(j){return this[j]}var d=(j)=>{var q=(O??=new WeakMap).get(j),C;if(q)return q;if(q=k({},"__esModule",{value:!0}),j&&typeof j==="object"||typeof j==="function"){for(var y of X(j))if(!N.call(q,y))k(q,y,{get:_.bind(j,y),enumerable:!(C=F(j,y))||C.enumerable})}return O.set(j,q),q},O;var w=(j)=>j;function E(j,q){this[j]=w.bind(null,q)}var M=(j,q)=>{for(var C in q)k(j,C,{get:q[C],enumerable:!0,configurable:!0,set:E.bind(q,C)})};var v=(j,q)=>()=>(j&&(q=j(j=0)),q);var f=import.meta.require;var b={};M(b,{rewriteImagesWithSiblings:()=>P});import H from"path";async function P(j,q){let C=q.formats??B;if(!q.publicDir)return j;let y=x(j),J=[],Y=/<img\b[^>]*?>/gi,K=[],Q;while((Q=Y.exec(j))!==null){let z=Q[0],G=z.match(/\bsrc=["']([^"']+)["']/i);if(!G)continue;if(R(Q.index,y))continue;K.push({idx:Q.index,length:z.length,src:G[1],raw:z})}if(K.length===0)return j;let Z=await Promise.all(K.map(async(z)=>{let G=await S(z.src,q.publicDir,C);return{match:z,sources:G}}));for(let{match:z,sources:G}of Z){if(G.length===0)continue;let L=`<picture>${G.map(($)=>`<source type="${$.mime}" srcset="${D($.url)}">`).join("")}${z.raw}</picture>`;J.push({start:z.idx,end:z.idx+z.length,replacement:L})}if(J.length===0)return j;let V="",W=0;for(let z of J)V+=j.slice(W,z.start),V+=z.replacement,W=z.end;return V+=j.slice(W),V}async function S(j,q,C){if(!T(j))return[];let y=H.extname(j).toLowerCase();if(!A.has(y))return[];if(!j.startsWith("/"))return[];let J=H.join(q,j.slice(1)),Y=H.dirname(J),K=H.basename(J,y),Q=H.resolve(Y),Z=H.resolve(q);if(!Q.startsWith(Z))return[];let V=H.posix.dirname(j),W=C.map(async(G)=>{let I=H.join(Y,`${K}${G.ext}`);if(!await Bun.file(I).exists())return null;return{url:H.posix.join(V,`${K}${G.ext}`),mime:G.mime}});return(await Promise.all(W)).filter((G)=>G!==null)}function T(j){if(!j)return!1;if(j.startsWith("http://")||j.startsWith("https://"))return!1;if(j.startsWith("//"))return!1;if(j.startsWith("data:"))return!1;return!0}function x(j){let q=[],C=/<picture\b[^>]*>[\s\S]*?<\/picture>/gi,y;while((y=C.exec(j))!==null)q.push([y.index,y.index+y[0].length]);return q}function R(j,q){for(let[C,y]of q)if(j>=C&&j<y)return!0;return!1}function D(j){return j.replace(/&/g,"&amp;").replace(/"/g,"&quot;")}var A,B;var g=v(()=>{A=new Set([".jpg",".jpeg",".png",".gif"]),B=[{ext:".avif",mime:"image/avif"},{ext:".webp",mime:"image/webp"}]});g();export{P as rewriteImagesWithSiblings};
