import e from"browserslist";import{isTokenDimension as t,isTokenNumber as r,NumberType as n,isTokenString as o,TokenType as s,isTokenDelim as i,tokenize as a}from"@csstools/css-tokenizer";import{isWhitespaceNode as u,isCommentNode as c,isTokenNode as l,parseCommaSeparatedListOfComponentValues as f,isFunctionNode as d}from"@csstools/css-parser-algorithms";function died(e){return"string"==typeof e}function matchBeforeDateCondition(e,t,r){const n=new Date;n.setUTCFullYear(e),n.setUTCMonth(t),n.setUTCDate(r);return(new Date).getTime()<n.getTime()||`Died because ${e}-${t}-${r} is in the past`}function matchBrowserslistCondition(t,r){const n=e(t);for(let e=0;e<n.length;e++){const t=n[e];if(r.has(t))return!0}return`Died because the browsers matching "${t}" do not have any overlap with your project browserslist`}function matchIfCondition(e){const r=e.a,n=e.b;if(!(r[0]!==n[0]||t(r)&&t(n)&&r[4].unit.toLowerCase()!==n[4].unit.toLowerCase())){switch(e.operator){case"<":if(r[4].value>=n[4].value)return`Died because A (${r[1]}) is no longer less than B (${n[1]})`;break;case">":if(r[4].value<=n[4].value)return`Died because A (${r[1]}) is no longer greater than B (${n[1]})`;break;case"=":if(r[4].value!==n[4].value)return`Died because A (${r[1]}) is no longer equal to B (${n[1]})`}return!0}}function matchNotCondition(e){const r=e.a,n=e.b;if(!(r[0]!==n[0]||t(r)&&t(n)&&r[4].unit.toLowerCase()!==n[4].unit.toLowerCase())){switch(e.operator){case"<":if(r[4].value<n[4].value)return`Died because A (${r[1]}) is less than B (${n[1]})`;break;case">":if(r[4].value>n[4].value)return`Died because A (${r[1]}) is greater than B (${n[1]})`;break;case"=":if(r[4].value===n[4].value)return`Died because A (${r[1]}) equals B (${n[1]})`}return!0}}function parseBeforeDateCondition(e){const t=e.value.filter(e=>!u(e)&&!c(e));if(t.length>3)return!1;const o=t[0],s=t[1],i=t[2];if(!o||!s||!i)return!1;if(!l(o)||!l(s)||!l(i))return!1;const a=o.value,f=s.value,d=i.value;return!(!r(a)||a[4].type!==n.Integer)&&(!(!r(f)||f[4].type!==n.Integer)&&(!(!r(d)||d[4].type!==n.Integer)&&{year:a[4].value,month:f[4].value,day:d[4].value}))}function parseBrowserslistCondition(e){const t=e.value.filter(e=>!u(e)&&!c(e));if(t.length>1)return!1;const r=t[0];if(!r)return!1;if(!l(r))return!1;const n=r.value;return!!o(n)&&n[4].value}const h=[s.Ident,s.Number,s.Percentage,s.Dimension],m=["<",">","="];function parseIfCondition(e){const t=e.value.filter(e=>!u(e)&&!c(e));if(t.length>3)return!1;const r=t[0],n=t[1],o=t[2];if(!r||!n||!o)return!1;if(!l(r)||!l(n)||!l(o))return!1;const s=r.value,a=n.value,f=o.value;return!!i(a)&&(!!m.includes(a[4].value)&&(!!h.includes(s[0])&&(!!h.includes(f[0])&&{a:s,b:f,operator:a[4].value})))}function parseNotCondition(e){const t=parseIfCondition(e);return!!t&&{a:t.a,b:t.b,operator:t.operator}}const creator=()=>{const t=new Set(e());return{postcssPlugin:"postcss-todo-or-die",Once(e,{result:r}){const n=[];e.walkAtRules(e=>{if("todo-or-die"===e.name.toLowerCase()){if(!e.params.trim())throw e.error("Rule must have valid params");n.push(e)}});for(const e of n){const errorHandler=t=>{throw e.error(t.message)},n=a({css:e.params},{onParseError:errorHandler}),o=f(n,{onParseError:errorHandler});if(!o.length)return void e.warn(r,"Rule must have some valid params.");for(let n=0;n<o.length;n++){const s=o[n].filter(e=>!u(e)&&!c(e));if(1!==s.length)return void e.warn(r,"Conditions must be split by commas when adding multiple in a list.");if(!d(s[0]))return void e.warn(r,"Conditions must be one of the supported functions.");switch(s[0].name[4].value.toLowerCase()){case"if":{const t=parseIfCondition(s[0]);if(!t)return void e.warn(r,"Incorrect arguments in `if()` function.");const n=matchIfCondition(t);if(died(n))throw e.error(n);break}case"not":{const t=parseNotCondition(s[0]);if(!t)return void e.warn(r,"Incorrect arguments in `not()` function.");const n=matchNotCondition(t);if(died(n))throw e.error(n);break}case"browserslist":{const n=parseBrowserslistCondition(s[0]);if(!n)return void e.warn(r,"Incorrect arguments in `browserslist()` function.");const o=matchBrowserslistCondition(n,t);if(died(o))throw e.error(o);break}case"before-date":{const t=parseBeforeDateCondition(s[0]);if(!t)return void e.warn(r,"Incorrect arguments in `before-date()` function.");const n=matchBeforeDateCondition(t.year,t.month,t.day);if(died(n))throw e.error(n);break}}}e.nodes?e.replaceWith(e.nodes):e.remove()}}}};creator.postcss=!0;export{creator as default,creator as"module.exports"};
