"use strict";var e=require("@csstools/css-parser-algorithms"),i=require("@csstools/css-tokenizer"),n=require("@csstools/media-query-list-parser"),a=require("@csstools/css-calc");const t=/[A-Z]/g;function toLowerCaseAZ(e){return e.replace(t,(e=>String.fromCharCode(e.charCodeAt(0)+32)))}const r={width:"px",height:"px","device-width":"px","device-height":"px","aspect-ratio":"","device-aspect-ratio":"",color:"","color-index":"",monochrome:"",resolution:"dpi"},o={width:!1,height:!1,"device-width":!1,"device-height":!1,"aspect-ratio":!1,"device-aspect-ratio":!1,color:!0,"color-index":!0,monochrome:!0,resolution:"dpi"};function featureNamePrefix(e){return e===n.MediaFeatureLT.LT||e===n.MediaFeatureLT.LT_OR_EQ?"max-":e===n.MediaFeatureGT.GT||e===n.MediaFeatureGT.GT_OR_EQ?"min-":""}const s={">":1,"<":-1},u=.001,d=.02;function transformSingleNameValuePair(t,l,c,p){let T=c.before,m=c.after;if(p||(T=c.after,m=c.before),!p){const e=n.invertComparison(l);if(!1===e)return;l=e}if(l===n.MediaFeatureEQ.EQ||l===n.MediaFeatureLT.LT_OR_EQ||l===n.MediaFeatureGT.GT_OR_EQ)return Array.isArray(c.value)?n.newMediaFeaturePlain(featureNamePrefix(l)+t,...T,...c.value.flatMap((e=>e.tokens())),...m):n.newMediaFeaturePlain(featureNamePrefix(l)+t,...T,...c.value.tokens(),...m);let f,v,y=!1;if(Array.isArray(c.value)){if(!n.matchesRatioExactly(c.value))return;if("aspect-ratio"!==t&&"device-aspect-ratio"!==t)return;const e=n.matchesRatio(c.value);if(-1===e)return;y=!0,f=c.value[e[0]],v=[...c.value.slice(e[0]+1).flatMap((e=>e.tokens()))]}else f=c.value,v=[];const h=r[t.toLowerCase()];if(e.isFunctionNode(f)){const r=toLowerCaseAZ(f.getName());if(a.mathFunctionNames.has(r)||"env"===r){const[[r]]=a.calcFromComponentValues([[f]],{precision:5,toCanonicalUnits:!0});if(!r||!e.isTokenNode(r)||r.value[0]!==i.TokenType.Number&&r.value[0]!==i.TokenType.Percentage&&r.value[0]!==i.TokenType.Dimension||!Number.isInteger(r.value[4].value)){let e;if(void 0!==h){const n=s[l]*("px"===h?d:u);e=[i.TokenType.Dimension,`${n.toString()}${h}`,-1,-1,{value:n,unit:h,type:i.NumberType.Integer}]}else if(!0===o[t]){const n=s[l];e=[i.TokenType.Number,n.toString(),-1,-1,{value:n,type:i.NumberType.Integer}]}else if(y){const n=s[l]*u;e=[i.TokenType.Number,n.toString(),-1,-1,{value:n,type:i.NumberType.Integer}]}else{const n=s[l];e=[i.TokenType.Number,n.toString(),-1,-1,{value:n,type:i.NumberType.Integer}]}return n.newMediaFeaturePlain(featureNamePrefix(l)+t,...T,[i.TokenType.Function,"calc(",-1,-1,{value:"calc("}],[i.TokenType.OpenParen,"(",-1,-1,void 0],...f.tokens().slice(1),[i.TokenType.Whitespace," ",-1,-1,void 0],[i.TokenType.Delim,"+",-1,-1,{value:"+"}],[i.TokenType.Whitespace," ",-1,-1,void 0],e,[i.TokenType.CloseParen,")",-1,-1,void 0],...v,...m)}f=r}}if(!e.isTokenNode(f))return;let g,k=f.value,M="";if(void 0!==h&&k[0]===i.TokenType.Number&&0===k[4].value)g=s[l],M=h;else if(k[0]===i.TokenType.Number&&0===k[4].value)g=s[l],M="";else if(k[0]===i.TokenType.Dimension&&0===k[4].value)g=s[l],M=k[4].unit;else if(k[0]===i.TokenType.Number&&!0===o[t])g=k[4].value+s[l];else if(k[0]===i.TokenType.Dimension&&"px"===k[4].unit&&k[4].type===i.NumberType.Integer)g=Number(Math.round(Number(k[4].value+d*s[l]+"e6"))+"e-6");else{if(k[0]!==i.TokenType.Dimension&&k[0]!==i.TokenType.Number)return;g=Number(Math.round(Number(k[4].value+u*s[l]+"e6"))+"e-6")}return M&&(k=[i.TokenType.Dimension,k[1],k[2],k[3],{value:k[4].value,unit:M,type:k[4].type}]),k[4].value=g,k[0]===i.TokenType.Dimension?k[1]=k[4].value.toString()+k[4].unit:k[1]=k[4].value.toString(),n.newMediaFeaturePlain(featureNamePrefix(l)+t,...T,k,...v,...m)}const l=new Set(["aspect-ratio","color","color-index","device-aspect-ratio","device-height","device-width","height","horizontal-viewport-segments","monochrome","resolution","vertical-viewport-segments","width"]);function transform(a){return a.map(((a,t)=>{const r=e.gatherNodeAncestry(a);a.walk((e=>{const t=e.node;if(!n.isMediaFeatureRange(t))return;const o=e.parent;if(!n.isMediaFeature(o))return;const s=toLowerCaseAZ(t.name.getName());if(!l.has(s))return;if(n.isMediaFeatureRangeNameValue(t)||n.isMediaFeatureRangeValueName(t)){const e=t.operatorKind();if(!1===e)return;const i=transformSingleNameValuePair(s,e,t.value,n.isMediaFeatureRangeNameValue(t));return void(i&&(o.feature=i.feature))}const u=r.get(o);if(!n.isMediaInParens(u))return;let d=null,c=null;{const e=t.valueOneOperatorKind();if(!1===e)return;const i=transformSingleNameValuePair(s,e,t.valueOne,!1);if(!i)return;e===n.MediaFeatureLT.LT||e===n.MediaFeatureLT.LT_OR_EQ?(d=i,d.before=o.before):(c=i,c.after=o.after)}{const e=t.valueTwoOperatorKind();if(!1===e)return;const i=transformSingleNameValuePair(s,e,t.valueTwo,!0);if(!i)return;e===n.MediaFeatureLT.LT||e===n.MediaFeatureLT.LT_OR_EQ?(c=i,c.before=o.before):(d=i,d.after=o.after)}if(!d||!c)return;const p=new n.MediaInParens(d),T=new n.MediaInParens(c),m=getMediaConditionListWithAndFromAncestry(u,r);if(m)return m.leading===u?(m.leading=p,void(m.list=[new n.MediaAnd([[i.TokenType.Whitespace," ",-1,-1,void 0],[i.TokenType.Ident,"and",-1,-1,{value:"and"}],[i.TokenType.Whitespace," ",-1,-1,void 0]],T),...m.list])):void m.list.splice(m.indexOf(r.get(u)),1,new n.MediaAnd([[i.TokenType.Whitespace," ",-1,-1,void 0],[i.TokenType.Ident,"and",-1,-1,{value:"and"}],[i.TokenType.Whitespace," ",-1,-1,void 0]],p),new n.MediaAnd([[i.TokenType.Whitespace," ",-1,-1,void 0],[i.TokenType.Ident,"and",-1,-1,{value:"and"}],[i.TokenType.Whitespace," ",-1,-1,void 0]],T));const f=new n.MediaConditionListWithAnd(p,[new n.MediaAnd([[i.TokenType.Whitespace," ",-1,-1,void 0],[i.TokenType.Ident,"and",-1,-1,{value:"and"}],[i.TokenType.Whitespace," ",-1,-1,void 0]],T)],[[i.TokenType.Whitespace," ",-1,-1,void 0]]),v=getMediaConditionInShallowMediaQueryFromAncestry(u,a,r);v?v.media=f:u.media=new n.MediaCondition(new n.MediaInParens(new n.MediaCondition(f),[[i.TokenType.Whitespace," ",-1,-1,void 0],[i.TokenType.OpenParen,"(",-1,-1,void 0]],[[i.TokenType.CloseParen,")",-1,-1,void 0]]))}));const o=a.tokens();return i.stringify(...o.filter(((e,n)=>(0!==n||0!==t||e[0]!==i.TokenType.Whitespace)&&(e[0]!==i.TokenType.Whitespace||!o[n+1]||o[n+1][0]!==i.TokenType.Whitespace))))})).join(",")}function getMediaConditionListWithAndFromAncestry(e,i){let a=e;if(a){if(a=i.get(a),n.isMediaConditionListWithAnd(a))return a;if(n.isMediaAnd(a))return a=i.get(a),n.isMediaConditionListWithAnd(a)?a:void 0}}function getMediaConditionInShallowMediaQueryFromAncestry(e,i,a){let t=e;if(!t)return;if(t=a.get(t),!n.isMediaCondition(t))return;const r=t;return t=a.get(t),n.isMediaQuery(t)&&t===i?r:void 0}const creator=()=>({postcssPlugin:"postcss-media-minmax",AtRule:{media:e=>{if(!(e.params.includes("<")||e.params.includes(">")||e.params.includes("=")))return;const i=transform(n.parse(e.params,{preserveInvalidMediaQueries:!0,onParseError:()=>{throw e.error(`Unable to parse media query "${e.params}"`)}}));e.params!==i&&(e.params=i)},"custom-media":e=>{if(!(e.params.includes("<")||e.params.includes(">")||e.params.includes("=")))return;const a=n.parseCustomMedia(e.params,{preserveInvalidMediaQueries:!0,onParseError:()=>{throw e.error(`Unable to parse media query "${e.params}"`)}});if(!a||!a.mediaQueryList)return;const t=a.mediaQueryList.map((e=>e.toString())).join(","),r=transform(a.mediaQueryList);t!==r&&(e.params=`${i.stringify(...a.name)} ${r}`)}}});creator.postcss=!0,module.exports=creator;