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