import{stringify as e,isTokenEOF as n,TokenType as t,isToken as o,mirrorVariant as s,isTokenOpenParen as i,isTokenOpenCurly as r,isTokenOpenSquare as a,isTokenFunction as c,isTokenWhitespace as u,isTokenComment as l,mirrorVariantType as d,ParseError as h,isTokenWhiteSpaceOrComment as m,isTokenCloseParen as p,isTokenComma as f}from"@csstools/css-tokenizer";var k;function walkerIndexGenerator(e){let n,t=e.slice(),o=-1;return(e,s,i)=>{if(e===n&&e.length===o&&e[i]===s){o=e.length;const n=i+1;return n=e.length)?(n=e,o=e.length,-1):(t=e.slice(),n=e,o=e.length,r)}}!function(e){e.Function="function",e.SimpleBlock="simple-block",e.Whitespace="whitespace",e.Comment="comment",e.Token="token"}(k||(k={}));const N=512;function consumeComponentValue(e,n,t=0,o=0){const s=n[t];if(i(s)||r(s)||a(s)){const s=consumeSimpleBlock(e,n,t,o);return{advance:s.advance,node:s.node}}if(c(s)){const s=consumeFunction(e,n,t,o);return{advance:s.advance,node:s.node}}if(u(s)){const o=consumeWhitespace(e,n,t);return{advance:o.advance,node:o.node}}if(l(s)){const o=consumeComment(e,n,t);return{advance:o.advance,node:o.node}}return{advance:1,node:new TokenNode(s)}}class ContainerNodeBaseClass{value=[];indexOf(e){return this.value.indexOf(e)}at(e){if("number"==typeof e)return e<0&&(e=this.value.length+e),this.value[e]}forEach(e,n){if(0===this.value.length)return;const t=walkerIndexGenerator(this.value);let o=0;for(;o!1!==e(n,t)&&((!("walk"in n.node)||!("number"==typeof t&&this.value[t]===n.node||this.value.includes(n.node))||!1!==n.node.walk(e,n.state))&&void 0),n)}}class FunctionNode extends ContainerNodeBaseClass{type=k.Function;name;endToken;constructor(e,n,t){super(),this.name=e,this.endToken=n,this.value=t}getName(){return this.name[4].value}normalize(){n(this.endToken)&&(this.endToken=[t.CloseParen,")",-1,-1,void 0])}tokens(){return n(this.endToken)?[this.name,...this.value.flatMap(e=>e.tokens())]:[this.name,...this.value.flatMap(e=>e.tokens()),this.endToken]}toString(){const n=this.value.map(n=>o(n)?e(n):n.toString()).join("");return e(this.name)+n+e(this.endToken)}toJSON(){return{type:this.type,name:this.getName(),tokens:this.tokens(),value:this.value.map(e=>e.toJSON())}}isFunctionNode(){return FunctionNode.isFunctionNode(this)}static isFunctionNode(e){return!!e&&(e instanceof FunctionNode&&e.type===k.Function)}}function consumeFunction(e,t,o,s){if(s>=N)throw new Error("Maximum nesting depth of 512 exceeded, reduce the complexity of your stylesheet.");const i=[];let r=o+1;for(;;){const a=t[r];if(!a||n(a))return e.onParseError(new h("Unexpected EOF while consuming a function.",t[o][2],t[t.length-1][3],["5.4.9. Consume a function","Unexpected EOF"])),{advance:t.length-o,node:new FunctionNode(t[o],a,i)};if(p(a))return{advance:r-o+1,node:new FunctionNode(t[o],a,i)};if(m(a)){const n=consumeAllCommentsAndWhitespace(e,t,r);r+=n.advance;for(let e=0;ee.tokens())]:[this.startToken,...this.value.flatMap(e=>e.tokens()),this.endToken]}toString(){const n=this.value.map(n=>o(n)?e(n):n.toString()).join("");return e(this.startToken)+n+e(this.endToken)}toJSON(){return{type:this.type,startToken:this.startToken,tokens:this.tokens(),value:this.value.map(e=>e.toJSON())}}isSimpleBlockNode(){return SimpleBlockNode.isSimpleBlockNode(this)}static isSimpleBlockNode(e){return!!e&&(e instanceof SimpleBlockNode&&e.type===k.SimpleBlock)}}function consumeSimpleBlock(e,t,o,s){if(s>=N)throw new Error("Maximum nesting depth of 512 exceeded, reduce the complexity of your stylesheet.");const i=d(t[o][0]);if(!i)throw new Error("Failed to parse, a mirror variant must exist for all block open tokens.");const r=[];let a=o+1;for(;;){const c=t[a];if(!c||n(c))return e.onParseError(new h("Unexpected EOF while consuming a simple block.",t[o][2],t[t.length-1][3],["5.4.8. Consume a simple block","Unexpected EOF"])),{advance:t.length-o,node:new SimpleBlockNode(t[o],c,r)};if(c[0]===i)return{advance:a-o+1,node:new SimpleBlockNode(t[o],c,r)};if(m(c)){const n=consumeAllCommentsAndWhitespace(e,t,a);a+=n.advance;for(let e=0;e{})},i=[...e];if(0===i.length)return;n(i[i.length-1])||i.push([t.EOF,"",i[i.length-1][2],i[i.length-1][3],void 0]);const r=consumeComponentValue(s,i);if(n(i[Math.min(r.advance,i.length-1)]))return r.node;s.onParseError(new h("Expected EOF after parsing a component value.",e[0][2],e[e.length-1][3],["5.3.9. Parse a component value","Expected EOF"]))}function parseListOfComponentValues(e,o){const s={onParseError:o?.onParseError??(()=>{})},i=[...e];if(0===i.length)return[];n(i[i.length-1])||i.push([t.EOF,"",i[i.length-1][2],i[i.length-1][3],void 0]);const r=[];let a=0;for(;;){if(!i[a]||n(i[a]))return r;const e=consumeComponentValue(s,i,a,0);r.push(e.node),a+=e.advance}}function parseCommaSeparatedListOfComponentValues(e,o){const s={onParseError:o?.onParseError??(()=>{})},i=[...e];if(0===e.length)return[];n(i[i.length-1])||i.push([t.EOF,"",i[i.length-1][2],i[i.length-1][3],void 0]);const r=[];let a=[],c=0;for(;;){if(!i[c]||n(i[c]))return r.push(a),r;if(f(i[c])){r.push(a),a=[],c++;continue}const e=consumeComponentValue(s,i,c,0);a.push(e.node),c+=e.advance}}function gatherNodeAncestry(e){const n=new Map;return e.walk(e=>{Array.isArray(e.node)?e.node.forEach(t=>{n.set(t,e.parent)}):n.set(e.node,e.parent)}),n}function forEach(e,n,t){if(0===e.length)return;const o=walkerIndexGenerator(e);let s=0;for(;s!1!==n(t,o)&&((!("walk"in t.node)||!("number"==typeof o&&e[o]===t.node||e.includes(t.node))||!1!==t.node.walk(n,t.state))&&void 0),t)}function replaceComponentValues(e,n){for(let t=0;t{if("number"!=typeof t)return;const o=n(e.node);o&&(Array.isArray(o)?e.parent.value.splice(t,1,...o):e.parent.value.splice(t,1,o))})}return e}function stringify(n){return n.map(n=>n.map(n=>e(n.tokens())).join("")).join(",")}function isSimpleBlockNode(e){return SimpleBlockNode.isSimpleBlockNode(e)}function isFunctionNode(e){return FunctionNode.isFunctionNode(e)}function isWhitespaceNode(e){return WhitespaceNode.isWhitespaceNode(e)}function isCommentNode(e){return CommentNode.isCommentNode(e)}function isWhiteSpaceOrCommentNode(e){return isWhitespaceNode(e)||isCommentNode(e)}function isTokenNode(e){return TokenNode.isTokenNode(e)}function sourceIndices(e){if(Array.isArray(e)){const n=e[0];if(!n)return[0,0];const t=e[e.length-1]||n;return[sourceIndices(n)[0],sourceIndices(t)[1]]}const n=e.tokens(),t=n[0],o=n[n.length-1];return t&&o?[t[2],o[3]]:[0,0]}export{CommentNode,k as ComponentValueType,ContainerNodeBaseClass,FunctionNode,SimpleBlockNode,TokenNode,WhitespaceNode,forEach,gatherNodeAncestry,isCommentNode,isFunctionNode,isSimpleBlockNode,isTokenNode,isWhiteSpaceOrCommentNode,isWhitespaceNode,parseCommaSeparatedListOfComponentValues,parseComponentValue,parseListOfComponentValues,replaceComponentValues,sourceIndices,stringify,walk,walkerIndexGenerator};