import{Scanner as e}from"./scanner.mjs";const t=123,r={" ":1,"&&":2,"||":3,"|":4};function n(e){let r=null,n=null;return e.eat(t),e.skipWs(),r=e.scanNumber(e),e.skipWs(),44===e.charCode()?(e.pos++,e.skipWs(),125!==e.charCode()&&(n=e.scanNumber(e),e.skipWs())):n=r,e.eat(125),{min:Number(r),max:n?Number(n):0}}function a(e,r){const o=function(e){let r=null,a=!1;switch(e.charCode()){case 42:e.pos++,r={min:0,max:0};break;case 43:e.pos++,r={min:1,max:0};break;case 63:e.pos++,r={min:0,max:1};break;case 35:e.pos++,a=!0,e.charCode()===t?r=n(e):63===e.charCode()?(e.pos++,r={min:0,max:0}):r={min:1,max:0};break;case t:r=n(e);break;default:return null}return{type:"Multiplier",comma:a,min:r.min,max:r.max,term:null}}(e);return null!==o?(o.term=r,35===e.charCode()&&43===e.charCodeAt(e.pos-1)?a(e,o):o):r}function o(e){const t=e.peek();return""===t?null:a(e,{type:"Token",value:t})}function s(e){let t,r=null;if(e.eat(60),t=e.scanWord(),"boolean-expr"===t){e.eat(91);const t=p(e,93);return e.eat(93),e.eat(62),a(e,{type:"Boolean",term:1===t.terms.length?t.terms[0]:t})}return 40===e.charCode()&&41===e.nextCharCode()&&(e.pos+=2,t+="()"),91===e.charCodeAt(e.findWsEnd(e.pos))&&(e.skipWs(),r=function(e){let t=null,r=null,n=1;return e.eat(91),45===e.charCode()&&(e.peek(),n=-1),-1==n&&8734===e.charCode()?e.peek():(t=n*Number(e.scanNumber(e)),e.isNameCharCode()&&(t+=e.scanWord())),e.skipWs(),e.eat(44),e.skipWs(),8734===e.charCode()?e.peek():(n=1,45===e.charCode()&&(e.peek(),n=-1),r=n*Number(e.scanNumber(e)),e.isNameCharCode()&&(r+=e.scanWord())),e.eat(93),{type:"Range",min:t,max:r}}(e)),e.eat(62),a(e,{type:"Type",name:t,opts:r})}function c(e,t){function n(e,t){return{type:"Group",terms:e,combinator:t,disallowEmpty:!1,explicit:!1}}let a;for(t=Object.keys(t).sort((e,t)=>r[e]-r[t]);t.length>0;){a=t.shift();let r=0,o=0;for(;r<e.length;r++){const t=e[r];"Combinator"===t.type&&(t.value===a?(-1===o&&(o=r-1),e.splice(r,1),r--):(-1!==o&&r-o>1&&(e.splice(o,r-o,n(e.slice(o,r),a)),r=o+1),o=-1))}-1!==o&&t.length&&e.splice(o,r-o,n(e.slice(o,r),a))}return a}function p(e,t){const r=Object.create(null),n=[];let a,o=null,s=e.pos;for(;e.charCode()!==t&&(a=u(e,t));)"Spaces"!==a.type&&("Combinator"===a.type?(null!==o&&"Combinator"!==o.type||(e.pos=s,e.error("Unexpected combinator")),r[a.value]=!0):null!==o&&"Combinator"!==o.type&&(r[" "]=!0,n.push({type:"Combinator",value:" "})),n.push(a),o=a,s=e.pos);return null!==o&&"Combinator"===o.type&&(e.pos-=s,e.error("Unexpected combinator")),{type:"Group",terms:n,combinator:c(n,r)||" ",disallowEmpty:!1,explicit:!1}}function u(e,r){let n=e.charCode();switch(n){case 93:break;case 91:return a(e,function(e,t){let r;return e.eat(91),r=p(e,t),e.eat(93),r.explicit=!0,33===e.charCode()&&(e.pos++,r.disallowEmpty=!0),r}(e,r));case 60:return 39===e.nextCharCode()?function(e){let t;return e.eat(60),e.eat(39),t=e.scanWord(),e.eat(39),e.eat(62),a(e,{type:"Property",name:t})}(e):s(e);case 124:return{type:"Combinator",value:e.substringToPos(e.pos+(124===e.nextCharCode()?2:1))};case 38:return e.pos++,e.eat(38),{type:"Combinator",value:"&&"};case 44:return e.pos++,{type:"Comma"};case 39:return a(e,{type:"String",value:e.scanString()});case 32:case 9:case 10:case 13:case 12:return{type:"Spaces",value:e.scanSpaces()};case 64:return n=e.nextCharCode(),e.isNameCharCode(n)?(e.pos++,{type:"AtKeyword",name:e.scanWord()}):o(e);case 42:case 43:case 63:case 35:case 33:break;case t:if(n=e.nextCharCode(),n<48||n>57)return o(e);break;default:return e.isNameCharCode(n)?function(e){const t=e.scanWord();return 40===e.charCode()?(e.pos++,{type:"Function",name:t}):a(e,{type:"Keyword",name:t})}(e):o(e)}}function i(t){const r=new e(t),n=p(r);return r.pos!==t.length&&r.error("Unexpected input"),1===n.terms.length&&"Group"===n.terms[0].type?n.terms[0]:n}export{i as parse};