"use strict";var I=Object.create;var m=Object.defineProperty;var B=Object.getOwnPropertyDescriptor;var N=Object.getOwnPropertyNames;var F=Object.getPrototypeOf,S=Object.prototype.hasOwnProperty;var D=(i,t)=>{for(var s in t)m(i,s,{get:t[s],enumerable:!0})},$=(i,t,s,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let p of N(t))!S.call(i,p)&&p!==s&&m(i,p,{get:()=>t[p],enumerable:!(r=B(t,p))||r.enumerable});return i};var P=(i,t,s)=>(s=i!=null?I(F(i)):{},$(t||!i||!i.__esModule?m(s,"default",{value:i,enumerable:!0}):s,i)),W=i=>$(m({},"__esModule",{value:!0}),i);var A={};D(A,{default:()=>z});module.exports=W(A);var h=P(require("sharp"),1),d={jpeg:"jpeg",jpg:"jpeg",webp:"webp",avif:"avif",png:"png"},k={jpeg:"image/jpeg",webp:"image/webp",avif:"image/avif",png:"image/png"},z=()=>({name:"@exact-realty/esbuild-plugin-responsive-images",setup(i){i.onLoad({filter:/.*/,namespace:"@exact-realty/esbuild-plugin-responsive-images/loader"},async t=>{let{path:s,pluginData:r}=t;if(!(r instanceof Object)||!(r.params instanceof URLSearchParams))return{errors:[{text:"Invalid pluginData"}]};let p=r.params.get("inputFormat"),l=r.params.has("outputFormats")?Array.from(new Set(String(r.params.get("outputFormats")).split(",").map(e=>d[e.trim()]??e))):[],g=r.params.has("sizes")?Array.from(new Set(String(r.params.get("sizes")).split(",").map(e=>e.trim()))).sort((e,a)=>Number(e.slice(0,-1))-Number(a.slice(0,-1))):[],x=Math.round(Number(r.params.get("displayWidth")))||1024,o=[];p&&!(p in d)&&o.push({text:`Unsupported input format '${p}'. Must be one of ${Object.keys(d).join(", ")}.`}),l.forEach(e=>{e in d||o.push({text:`Unsupported output format '${e}'. Must be one of ${Object.keys(d).join(", ")}.`})}),g.length===0&&o.push({text:"At least one output size must be specified."});let c=g.reduce((e,a)=>/^0*[1-9][0-9]*w$/.test(a)?e|1:/^(0*[1-9][0-9]*(\.[0-9]*)?|0*\.0*[1-9][0-9]*)x$/.test(a)?e|2:e|4,0);if((c&3)===3&&o.push({text:"Invalid size description: mixed width descriptors (###w) and pixel density descriptors (###x)"}),(c&4)===4&&o.push({text:"Invalid size description: invalid descriptions. Only width (###w) and pixel density density descriptors (###x) are supported."}),o.length)return{errors:o};let w=(0,h.default)(s),f=await w.metadata();if(l.length===0)if(f.format&&f.format in d)l.push(f.format);else return{errors:[{text:"No output format specified, and unable to identify format in source image"}]};let v=(await Promise.all(g.map(async(e,a)=>{let n=e.endsWith("w")?Number(e.slice(0,-1)):e.endsWith("x")?Math.round(Number(e.slice(0,-1))*x):NaN;if(!Number.isInteger(n))throw new Error("Invalid width");if(f.width!==void 0&&n>=f.width){if(a!==0)return;n=f.width}return[e,await w.clone().resize({width:n,withoutEnlargement:!0}).toFormat("png").toBuffer({resolveWithObject:!0})]}))).filter(Boolean),b=(await Promise.all(v.flatMap(([e,a])=>l.map(async n=>{if(a.info.format===n)return[e,n,a];switch(n){case"jpeg":return[e,n,await(0,h.default)(a.data).toFormat(n,{mozjpeg:!0}).toBuffer({resolveWithObject:!0})];case"png":case"avif":case"webp":return[e,n,await(0,h.default)(a.data).toFormat(n).toBuffer({resolveWithObject:!0})]}}))).finally(()=>{v.length=0})).filter(Boolean),u=b.reduce((e,[a,n,j],O)=>(n in e||(e[n]=[]),e[n].push([a,j,O]),e),Object.create(null)),y="png"in u&&"jpeg"in u?u.png[0][1].info.size>u.jpeg[0][1].info.size?"jpeg":"png":"png"in u?"png":"jpeg"in u?"jpeg":"webp"in u?"webp":Object.keys(u)[0];return{contents:` ${b.map((e,a)=>`import _i${+a}_ from '${Buffer.from(t.path).toString("base64")}.${+a}.${e[0].replace(".","-")}.${e[1]}';`).join("")} export const src = _i${u[y][0][2]}_; export const width = ${+u[y][0][1].info.width}; export const height = ${+u[y][0][1].info.height}; export const originalWidth = ${f.width}; export const originalHeight = ${f.height}; export const sources = [ ${Object.entries(u).map(([e,a])=>"["+JSON.stringify(k[e??e])+","+a.map(([n,,j])=>[`_i${+j}_`,JSON.stringify(" "),JSON.stringify(n)].join("+")).join('+ "," +')+"]").join(",")} ];`,loader:"js",pluginData:b}}),i.onLoad({filter:/.*/,namespace:"@exact-realty/esbuild-plugin-responsive-images/importer"},t=>{let{pluginData:s}=t;return s instanceof Buffer?{contents:s,loader:"file"}:{errors:[{text:"Invalid plugin data. Expecting Buffer."}]}}),i.onResolve({filter:/^respimg\+file:/},async t=>{let{path:s,resolveDir:r}=t,p=new URL(s),l=await i.resolve(decodeURIComponent(p.pathname),{resolveDir:r,kind:"import-statement"});return l.errors.length>0?{errors:l.errors}:{external:!1,namespace:"@exact-realty/esbuild-plugin-responsive-images/loader",path:l.path,suffix:void 0,watchDirs:[],watchFiles:[l.path],pluginData:{params:p.searchParams}}}),i.onResolve({filter:/.*/,namespace:"@exact-realty/esbuild-plugin-responsive-images/loader"},t=>{let{path:s,pluginData:r}=t,[p,l,g,x]=t.path.split("."),o=+l;if(!Array.isArray(r)||!Number.isInteger(o)||o<0||o>r.length||!Array.isArray(r[o])||!(r[o][2]instanceof Object)||!(r[o][2].data instanceof Buffer))return{errors:[{text:`Invalid path or plugin data. Path: ${s}.`}]};let c=Buffer.from(p,"base64").toString();return{external:!1,namespace:"@exact-realty/esbuild-plugin-responsive-images/importer",path:[c.lastIndexOf(".")>0?c.slice(0,c.lastIndexOf(".")):c,g.replace("-","."),x].join("."),suffix:void 0,watchDirs:[],watchFiles:[],pluginData:r[o][2].data}})}});