import s from"@csstools/postcss-progressive-custom-properties";import{tokenize as t}from"@csstools/css-tokenizer";import{color as e,SyntaxFlag as o,serializeHSL as r,serializeRGB as a}from"@csstools/css-color-parser";import{replaceComponentValues as n,parseCommaSeparatedListOfComponentValues as c,isFunctionNode as l,stringify as p}from"@csstools/css-parser-algorithms";function hasFallback(s){const t=s.parent;if(!t)return!1;const e=s.prop.toLowerCase(),o=t.index(s);for(let s=0;s<o;s++){const o=t.nodes[s];if("decl"===o.type&&o.prop.toLowerCase()===e)return!0}return!1}const i=/(?:rgb|hsl)a?\(/i;function hasSupportsAtRuleAncestor(s){let t=s.parent;for(;t;)if("atrule"===t.type){if("supports"===t.name.toLowerCase()&&i.test(t.params))return!0;t=t.parent}else t=t.parent;return!1}const u=/^(?:rgb|hsl)a?$/i,basePlugin=s=>({postcssPlugin:"postcss-color-functional-notation",Declaration:f=>{const g=f.value;if(!i.test(g))return;if(hasFallback(f))return;if(hasSupportsAtRuleAncestor(f))return;const m=n(c(t({css:g})),(s=>{if(!l(s)||!u.test(s.getName()))return;const t=e(s);return!t||t.syntaxFlags.has(o.Experimental)||t.syntaxFlags.has(o.HasNoneKeywords)||t.syntaxFlags.has(o.RelativeColorSyntax)||(t.syntaxFlags.has(o.LegacyRGB)||t.syntaxFlags.has(o.LegacyHSL))&&!t.syntaxFlags.has(o.HasPercentageAlpha)?void 0:"hsl"===t.colorNotation?r(t):a(t)})),h=p(m);h!==g&&(f.cloneBefore({value:h}),s?.preserve||f.remove())}});basePlugin.postcss=!0;const postcssPlugin=t=>{const e=Object.assign({preserve:!1,enableProgressiveCustomProperties:!0},t);return e.enableProgressiveCustomProperties&&e.preserve?{postcssPlugin:"postcss-color-functional-notation",plugins:[s(),basePlugin(e)]}:basePlugin(e)};postcssPlugin.postcss=!0;export{postcssPlugin as default};
