import e from"@csstools/postcss-progressive-custom-properties";import{tokenize as o,isTokenIdent as r,TokenType as t,isTokenHash as s}from"@csstools/css-tokenizer";import{color as l,SyntaxFlag as a}from"@csstools/css-color-parser";import{hasFallback as n,hasSupportsAtRuleAncestor as i}from"@csstools/utilities";import{replaceComponentValues as c,parseCommaSeparatedListOfComponentValues as v,isFunctionNode as u,isWhiteSpaceOrCommentNode as p,isTokenNode as d,WhitespaceNode as h,TokenNode as f,stringify as g}from"@csstools/css-parser-algorithms";const b=/\balpha\(/i,m=/^alpha$/i,w=new Set(["srgb","srgb-linear","display-p3","display-p3-linear","a98-rgb","prophoto-rgb","rec2020","xyz","xyz-d50","xyz-d65"]),basePlugin=e=>({postcssPlugin:"postcss-color-function",Declaration(C){const x=C.value;if(!b.test(x))return;if(n(C))return;if(i(C,b))return;const y=o({css:x}),R=c(v(y),e=>{if(!u(e)||!m.test(e.getName()))return;let o=!1;for(let n=0;n<e.value.length;n++){let i=e.value[n];for(;p(i);)n++,i=e.value[n];for(d(i)&&r(i.value)&&"from"===i.value[4].value.toLowerCase()&&(o=!0,n++,i=e.value[n]);p(i);)n++,i=e.value[n];if(!o)return;const c=i;if(u(c)){const o=c.getName().toLowerCase();if("var"===o)return;if("color"===o){let o="";for(let e=0;e<c.value.length;e++){const t=c.value[e];if(d(t)&&r(t.value)&&w.has(t.value[4].value.toLowerCase())){if(o)return;o=t.value[4].value}}if(!o)return;e.name[1]="color(",e.name[4].value="color";let s=["r","g","b"];return"xyz"!==o&&"xyz-d50"!==o&&"xyz-d65"!==o||(s=["x","y","z"]),void e.value.splice(n+1,0,new h([[t.Whitespace," ",-1,-1,void 0]]),new f([t.Ident,o,-1,-1,{value:o}]),new h([[t.Whitespace," ",-1,-1,void 0]]),new f([t.Ident,s[0],-1,-1,{value:s[0]}]),new h([[t.Whitespace," ",-1,-1,void 0]]),new f([t.Ident,s[1],-1,-1,{value:s[1]}]),new h([[t.Whitespace," ",-1,-1,void 0]]),new f([t.Ident,s[2],-1,-1,{value:s[2]}]))}switch(o){case"rgb":case"rgba":return void convertToRelativeColor(e,n+1,"rgb",["r","g","b"]);case"hsl":case"hsla":return void convertToRelativeColor(e,n+1,"hsl",["h","s","l"]);case"hwb":return void convertToRelativeColor(e,n+1,"hwb",["h","w","b"]);case"lab":return void convertToRelativeColor(e,n+1,"lab",["l","a","b"]);case"lch":return void convertToRelativeColor(e,n+1,"lch",["l","c","h"]);case"oklab":return void convertToRelativeColor(e,n+1,"oklab",["l","a","b"]);case"oklch":return void convertToRelativeColor(e,n+1,"oklch",["l","c","h"])}}if(d(c)&&(s(c.value)||r(c.value))){const o=l(c);if(!o)return;if(!o.syntaxFlags.has(a.Hex)&&!o.syntaxFlags.has(a.ColorKeyword))return;return void convertToRelativeColor(e,n+1,"rgb",["r","g","b"])}}}),T=g(R);T!==x&&(C.cloneBefore({value:T}),e?.preserve||C.remove())}});function convertToRelativeColor(e,o,r,s){e.name[1]=r+"(",e.name[4].value=r,e.value.splice(o,0,new h([[t.Whitespace," ",-1,-1,void 0]]),new f([t.Ident,s[0],-1,-1,{value:s[0]}]),new h([[t.Whitespace," ",-1,-1,void 0]]),new f([t.Ident,s[1],-1,-1,{value:s[1]}]),new h([[t.Whitespace," ",-1,-1,void 0]]),new f([t.Ident,s[2],-1,-1,{value:s[2]}]))}basePlugin.postcss=!0;const postcssPlugin=o=>{const r=Object.assign({preserve:!1,enableProgressiveCustomProperties:!0},o);return r.enableProgressiveCustomProperties&&r.preserve?{postcssPlugin:"postcss-color-function",plugins:[e(),basePlugin(r)]}:basePlugin(r)};postcssPlugin.postcss=!0;export{postcssPlugin as default,postcssPlugin as"module.exports"};
