import{createTokenizer}from"./FormatterWorker.js";export const CSSParserStates={Initial:"Initial",Selector:"Selector",Style:"Style",PropertyName:"PropertyName",PropertyValue:"PropertyValue",AtRule:"AtRule"};let Rule,Chunk;export function parseCSS(e,t){const a=e.split("\n");let n,r,s=[],l=0,S=CSSParserStates.Initial;const o=new Set;let u=[];function i(e){u=u.concat(e.chunk)}function m(e,a,m,c){const C=a?new Set(a.split(" ")):o;switch(S){case CSSParserStates.Initial:C.has("qualifier")||C.has("builtin")||C.has("tag")?(n={selectorText:e,lineNumber:g,columnNumber:m,properties:[]},S=CSSParserStates.Selector):C.has("def")&&(n={atRule:e,lineNumber:g,columnNumber:m},S=CSSParserStates.AtRule);break;case CSSParserStates.Selector:"{"===e&&C===o?(n.selectorText=n.selectorText.trim(),n.styleRange=p(g,c),S=CSSParserStates.Style):n.selectorText+=e;break;case CSSParserStates.AtRule:";"!==e&&"{"!==e||C!==o?n.atRule+=e:(n.atRule=n.atRule.trim(),s.push(n),S=CSSParserStates.Initial);break;case CSSParserStates.Style:if(C.has("meta")||C.has("property"))r={name:e,value:"",range:p(g,m),nameRange:p(g,m)},S=CSSParserStates.PropertyName;else if("}"===e&&C===o)n.styleRange.endLine=g,n.styleRange.endColumn=m,s.push(n),S=CSSParserStates.Initial;else if(C.has("comment")){if("/*"!==e.substring(0,2)||"*/"!==e.substring(e.length-2))break;const t=e.substring(2,e.length-2);if(u=[],parseCSS("a{\n"+t+"}",i),1===u.length&&1===u[0].properties.length){const e=u[0].properties[0];e.disabled=!0,e.range=p(g,m),e.range.endColumn=c;const t=g-1,a=m+2;e.nameRange.startLine+=t,e.nameRange.startColumn+=a,e.nameRange.endLine+=t,e.nameRange.endColumn+=a,e.valueRange.startLine+=t,e.valueRange.startColumn+=a,e.valueRange.endLine+=t,e.valueRange.endColumn+=a,n.properties.push(e)}}break;case CSSParserStates.PropertyName:":"===e&&C===o?(r.name=r.name,r.nameRange.endLine=g,r.nameRange.endColumn=m,r.valueRange=p(g,c),S=CSSParserStates.PropertyValue):C.has("property")&&(r.name+=e);break;case CSSParserStates.PropertyValue:";"!==e&&"}"!==e||C!==o?C.has("comment")||(r.value+=e):(r.value=r.value,r.valueRange.endLine=g,r.valueRange.endColumn=m,r.range.endLine=g,r.range.endColumn=";"===e?c:m,n.properties.push(r),"}"===e?(n.styleRange.endLine=g,n.styleRange.endColumn=m,s.push(n),S=CSSParserStates.Initial):S=CSSParserStates.Style);break;default:console.assert(!1,"Unknown CSS parser state.")}l+=c-m,l>1e5&&(t({chunk:s,isLastChunk:!1}),s=[],l=0)}const c=createTokenizer("text/css");let g;for(g=0;g<a.length;++g){const e=a[g];c(e,m),m("\n",null,e.length,e.length+1)}function p(e,t){return{startLine:e,startColumn:t,endLine:e,endColumn:t}}t({chunk:s,isLastChunk:!0})}