import{parse as e}from"../definition-syntax/parse.mjs";const t={type:"Match"},n={type:"Mismatch"},r={type:"DisallowEmpty"};function a(e,r,a){return r===t&&a===n||e===t&&r===t&&a===t?e:("If"===e.type&&e.else===n&&r===t&&(r=e.then,e=e.match),{type:"If",match:e,then:r,else:a})}function o(e){return e.length>2&&40===e.charCodeAt(e.length-2)&&41===e.charCodeAt(e.length-1)}function l(e){return"Keyword"===e.type||"AtKeyword"===e.type||"Function"===e.type||"Type"===e.type&&o(e.name)}function y(e,t=" ",n=!1){return{type:"Group",terms:e,combinator:t,disallowEmpty:!1,explicit:n}}function p(e,t,n=new Set){if(!n.has(e))switch(n.add(e),e.type){case"If":e.match=p(e.match,t,n),e.then=p(e.then,t,n),e.else=p(e.else,t,n);break;case"Type":return t[e.name]||e}return e}function m(e,r,y){switch(e){case" ":{let e=t;for(let t=r.length-1;t>=0;t--){e=a(r[t],e,n)}return e}case"|":{let e=n,y=null;for(let n=r.length-1;n>=0;n--){let p=r[n];if(l(p)&&(null===y&&n>0&&l(r[n-1])&&(y=Object.create(null),e=a({type:"Enum",map:y},t,e)),null!==y)){const e=(o(p.name)?p.name.slice(0,-1):p.name).toLowerCase();if(e in y==!1){y[e]=p;continue}}y=null,e=a(p,t,e)}return e}case"&&":{if(r.length>5)return{type:"MatchOnce",terms:r,all:!0};let o=n;for(let n=r.length-1;n>=0;n--){const l=r[n];let y;y=r.length>1?m(e,r.filter(function(e){return e!==l}),!1):t,o=a(l,y,o)}return o}case"||":{if(r.length>5)return{type:"MatchOnce",terms:r,all:!1};let o=y?t:n;for(let n=r.length-1;n>=0;n--){const l=r[n];let y;y=r.length>1?m(e,r.filter(function(e){return e!==l}),!0):t,o=a(l,y,o)}return o}}}function u(e){if("function"==typeof e)return{type:"Generic",fn:e};switch(e.type){case"Group":{let t=m(e.combinator,e.terms.map(u),!1);return e.disallowEmpty&&(t=a(t,r,n)),t}case"Multiplier":return function(e){let o=t,l=u(e.term);if(0===e.max)l=a(l,r,n),o=a(l,null,n),o.then=a(t,t,o),e.comma&&(o.then.else=a({type:"Comma",syntax:e},o,n));else for(let r=e.min||1;r<=e.max;r++)e.comma&&o!==t&&(o=a({type:"Comma",syntax:e},o,n)),o=a(l,a(t,t,o),n);if(0===e.min)o=a(t,t,o);else for(let r=0;r<e.min-1;r++)e.comma&&o!==t&&(o=a({type:"Comma",syntax:e},o,n)),o=a(l,o,n);return o}(e);case"Boolean":{const t=u(e.term),n=u(y([y([{type:"Keyword",name:"not"},{type:"Type",name:"!boolean-group"}]),y([{type:"Type",name:"!boolean-group"},y([{type:"Multiplier",comma:!1,min:0,max:0,term:y([{type:"Keyword",name:"and"},{type:"Type",name:"!boolean-group"}])},{type:"Multiplier",comma:!1,min:0,max:0,term:y([{type:"Keyword",name:"or"},{type:"Type",name:"!boolean-group"}])}],"|")])],"|")),r=u(y([{type:"Type",name:"!term"},y([{type:"Token",value:"("},{type:"Type",name:"!self"},{type:"Token",value:")"}]),{type:"Type",name:"general-enclosed"}],"|"));return p(r,{"!term":t,"!self":n}),p(n,{"!boolean-group":r}),n}case"Type":case"Property":return{type:e.type,name:e.name,syntax:e};case"Keyword":return{type:e.type,name:e.name.toLowerCase(),syntax:e};case"AtKeyword":return{type:e.type,name:"@"+e.name.toLowerCase(),syntax:e};case"Function":return{type:e.type,name:e.name.toLowerCase()+"(",syntax:e};case"String":return 3===e.value.length?{type:"Token",value:e.value.charAt(1),syntax:e}:{type:e.type,value:e.value.substr(1,e.value.length-2).replace(/\\'/g,"'"),syntax:e};case"Token":return{type:e.type,value:e.value,syntax:e};case"Comma":return{type:e.type,syntax:e};default:throw new Error("Unknown node type:",e.type)}}function c(t,n){return"string"==typeof t&&(t=e(t)),{type:"MatchGraph",match:u(t),syntax:n||null,source:t}}export{r as DISALLOW_EMPTY,t as MATCH,n as MISMATCH,c as buildMatchGraph};