import{List as e}from"../utils/List.mjs";import{SyntaxError as t}from"./SyntaxError.mjs";import{tokenize as n}from"../tokenizer/index.mjs";import{readSequence as o}from"./sequence.mjs";import{OffsetToLocation as r}from"../tokenizer/OffsetToLocation.mjs";import{TokenStream as s}from"../tokenizer/TokenStream.mjs";import{findWhiteSpaceStart as i,consumeNumber as c,cmpStr as a,cmpChar as u}from"../tokenizer/utils.mjs";import{Ident as l,Percentage as p,Number as f,Hash as m,Function as h,Url as d,WhiteSpace as k,Comment as g}from"../tokenizer/types.mjs";import x from"../tokenizer/names.mjs";const b=()=>{};function L(e){return function(){return this[e]()}}function S(e){const t=Object.create(null);for(const n of Object.keys(e)){const o=e[n],r=o.parse||o;r&&(t[n]=r)}return t}function y(y){let j="",C="<unknown>",w=!1,O=b,P=!1;const T=new r,E=Object.assign(new s,function(e){const t={context:Object.create(null),features:Object.assign(Object.create(null),e.features),scope:Object.assign(Object.create(null),e.scope),atrule:S(e.atrule),pseudo:S(e.pseudo),node:S(e.node)};for(const[n,o]of Object.entries(e.parseContext))switch(typeof o){case"function":t.context[n]=o;break;case"string":t.context[n]=L(o)}return{config:t,...t,...t.node}}(y||{}),{parseAtrulePrelude:!0,parseRulePrelude:!0,parseValue:!0,parseCustomProperty:!1,readSequence:o,consumeUntilBalanceEnd:()=>0,consumeUntilLeftCurlyBracket:e=>123===e?1:0,consumeUntilLeftCurlyBracketOrSemicolon:e=>123===e||59===e?1:0,consumeUntilExclamationMarkOrSemicolon:e=>33===e||59===e?1:0,consumeUntilSemicolonIncluded:e=>59===e?2:0,createList:()=>new e,createSingleNodeList:t=>(new e).appendData(t),getFirstListNode:e=>e&&e.first,getLastListNode:e=>e&&e.last,parseWithFallback(e,t){const n=this.tokenIndex;try{return e.call(this)}catch(e){if(P)throw e;this.skip(n-this.tokenIndex);const o=t.call(this);return P=!0,O(e,o),P=!1,o}},lookupNonWSType(e){let t;do{if(t=this.lookupType(e++),t!==k&&t!==g)return t}while(0!==t);return 0},charCodeAt:e=>e>=0&&e<j.length?j.charCodeAt(e):0,substring:(e,t)=>j.substring(e,t),substrToCursor(e){return this.source.substring(e,this.tokenStart)},cmpChar:(e,t)=>u(j,e,t),cmpStr:(e,t,n)=>a(j,e,t,n),consume(e){const t=this.tokenStart;return this.eat(e),this.substrToCursor(t)},consumeFunctionName(){const e=j.substring(this.tokenStart,this.tokenEnd-1);return this.eat(h),e},consumeNumber(e){const t=j.substring(this.tokenStart,c(j,this.tokenStart));return this.eat(e),t},eat(e){if(this.tokenType!==e){const t=x[e].slice(0,-6).replace(/-/g," ").replace(/^./,e=>e.toUpperCase());let n=`${/[[\](){}]/.test(t)?`"${t}"`:t} is expected`,o=this.tokenStart;switch(e){case l:this.tokenType===h||this.tokenType===d?(o=this.tokenEnd-1,n="Identifier is expected but function found"):n="Identifier is expected";break;case m:this.isDelim(35)&&(this.next(),o++,n="Name is expected");break;case p:this.tokenType===f&&(o=this.tokenEnd,n="Percent sign is expected")}this.error(n,o)}this.next()},eatIdent(e){this.tokenType===l&&!1!==this.lookupValue(0,e)||this.error(`Identifier "${e}" is expected`),this.next()},eatDelim(e){this.isDelim(e)||this.error(`Delim "${String.fromCharCode(e)}" is expected`),this.next()},getLocation:(e,t)=>w?T.getLocationRange(e,t,C):null,getLocationFromList(e){if(w){const t=this.getFirstListNode(e),n=this.getLastListNode(e);return T.getLocationRange(null!==t?t.loc.start.offset-T.startOffset:this.tokenStart,null!==n?n.loc.end.offset-T.startOffset:this.tokenStart,C)}return null},error(e,n){const o=void 0!==n&&n<j.length?T.getLocation(n):this.eof?T.getLocation(i(j,j.length-1)):T.getLocation(this.tokenStart);throw new t(e||"Unexpected input",j,o.offset,o.line,o.column,T.startLine,T.startColumn)}});return Object.assign(function(e,t){j=e,t=t||{},E.setSource(j,n),T.setSource(j,t.offset,t.line,t.column),C=t.filename||"<unknown>",w=Boolean(t.positions),O="function"==typeof t.onParseError?t.onParseError:b,P=!1,E.parseAtrulePrelude=!("parseAtrulePrelude"in t)||Boolean(t.parseAtrulePrelude),E.parseRulePrelude=!("parseRulePrelude"in t)||Boolean(t.parseRulePrelude),E.parseValue=!("parseValue"in t)||Boolean(t.parseValue),E.parseCustomProperty="parseCustomProperty"in t&&Boolean(t.parseCustomProperty);const{context:o="default",onComment:r}=t;if(o in E.context==!1)throw new Error("Unknown context `"+o+"`");"function"==typeof r&&E.forEachToken((e,t,n)=>{if(e===g){const e=E.getLocation(t,n),o=a(j,n-2,n,"*/")?j.slice(t+2,n-2):j.slice(t+2,n);r(o,e)}});const s=E.context[o].call(E,t);return E.eof||E.error(),s},{SyntaxError:t,config:E.config})}export{y as createParser};