import e from"postcss-value-parser";import{hasFallback as t}from"@csstools/utilities";function isComma(e){return!!e&&"div"===e.type&&","===e.value}const n=/^(?:cross-fade|image|(?:repeating-)?(?:conic|linear|radial)-gradient|url|var)$/i;function getImage(t){return!(!t||!t.type)&&("string"===t.type?"url("+e.stringify(t)+")":!("function"!==t.type||!n.test(t.value))&&e.stringify(t))}const i=new Map([["dpcm",2.54],["dpi",1],["dppx",96],["x",96]]);function getMedia(e,t,n){if("boolean"==typeof e)return!1;const i=Math.floor(e/96*100)/100;return t.atRule({name:"media",params:`(-webkit-min-device-pixel-ratio: ${i}), (min-resolution: ${e}dpi)`,source:n.source})}function getMediaDPI(t){if(!t)return!1;if("word"!==t.type)return!1;if(!isNumericNode(t))return!1;const n=e.unit(t.value);if(!n)return!1;const r=i.get(n.unit.toLowerCase());return!!r&&Number(n.number)*r}function isNumericNode(t){if(!t||!t.value)return!1;try{return!1!==e.unit(t.value)}catch{return!1}}const handleInvalidation=(e,t,n)=>{if("warn"===e.oninvalid)e.decl.warn(e.result,t,{word:String(n)});else if("throw"===e.oninvalid)throw e.decl.error(t,{word:String(n)})},processImageSet=(t,n,i)=>{const r=n.parent;if(!r)return;const o=new Map,s=n.value;for(let r=0;r<t.length;r++){const{imageSetFunction:a,imageSetOptionNodes:u}=t[r],l=new Map,c=u.length;let f=-1;for(;f<c;){const t=f<0||isComma(u[f]),r=getImage(u[f+1]),c=getMediaDPI(u[f+2]),p=getMedia(c,i.postcss,n);if(!t)return void handleInvalidation(i,"expected a comma",e.stringify(u));if(!r)return void handleInvalidation(i,"unexpected image",e.stringify(u));if(!p||!c||l.has(c))return void handleInvalidation(i,"unexpected resolution",e.stringify(u));if(l.set(c,p),o.has(c)){const t=o.get(c);t.value=t.value.replace(e.stringify(a),r.trim()),o.set(c,t)}else o.set(c,{atRule:p,value:s.replace(e.stringify(a),r.trim())});f+=3}}for(const{atRule:e,value:t}of o.values()){const i=r.clone().removeAll(),o=n.clone({value:t});i.append(o),e.append(i)}const a=Array.from(o.keys()).sort((e,t)=>e-t),u=a.map(e=>o.get(e)?.atRule).filter(e=>!!e);if(!u.length)return;const l=o.get(a[0])?.value;if(!l)return;const c=u.slice(1);c.length&&r.after(c),n.cloneBefore({value:l.trim()}),i.preserve||(n.remove(),r.nodes?.length||r.remove())},r=/(?:^|[^\w-])(?:-webkit-)?image-set\(/i,o=/^(?:-webkit-)?image-set$/i,creator=n=>{const i=!("preserve"in Object(n))||Boolean(n?.preserve),s="onInvalid"in Object(n)?n?.onInvalid:"ignore";if("oninvalid"in Object(n))throw new Error('"oninvalid" was changed to "onInvalid" to match other plugins with similar options');return{postcssPlugin:"postcss-image-set-function",Declaration(n,{result:a,postcss:u}){const l=n.value;if(!r.test(l))return;if(t(n))return;let c;try{c=e(l)}catch{n.warn(a,`Failed to parse value '${l}' as an image-set function. Leaving the original value intact.`)}if(void 0===c)return;const f=[];c.walk(t=>{if("function"!==t.type)return;if(!o.test(t.value))return;let i=!1;if(e.walk(t.nodes,e=>{"function"===e.type&&o.test(e.value)&&(i=!0)}),i)return handleInvalidation({decl:n,oninvalid:s,result:a},"nested image-set functions are not allowed",e.stringify(t)),!1;const r=t.nodes.filter(e=>"comment"!==e.type&&"space"!==e.type);f.push({imageSetFunction:t,imageSetOptionNodes:r})}),processImageSet(f,n,{decl:n,oninvalid:s,preserve:i,result:a,postcss:u})}}};creator.postcss=!0;export{creator as default,creator as"module.exports"};
