import Resolver from"../resolver.js";import{ResolverOptions,SearchResults,EnrichedSearchResults,IntermediateSearchResults}from"../type.js";Resolver.prototype.handler=function(a,b,c){let d=c[0];if(d[0]&&d[0].query)return this[a].apply(this,d);if("and"===a||"not"===a){let a,b=this.result.length||this.await;if(!b&&!d.suggest)return 1<c.length&&(d=c[c.length-1]),a=d.resolve,a?this.await||this.result:this}const e=this;let f,g,h,j,k,l=[],m=0,n=0;for(let d,o=0;o<c.length;o++)if(d=c[o],d){let a;if(d.constructor===Resolver)a=d.await||d.result;else if(d.then||d.constructor===Array)a=d;else{m=d.limit||0,n=d.offset||0,h=d.suggest,g=d.resolve,j=d.highlight||this.highlight,f=(j||d.enrich)&&g;let b=d.queue,c=d.async||b,i=d.index,p=d.query;if(i?this.index||(this.index=i):i=this.index,p||d.tag){{const a=d.field||d.pluck;a&&(p&&(!this.query||j)&&(this.query=p,this.field=a,this.highlight=j),i=i.index.get(a))}if(b&&(k||this.await)){k=1;let a;const b=this.promises.length,f=new Promise(function(b){a=b});(function(d,g){f._fn=function(){g.index=null,g.resolve=!1;let f=c?d.searchAsync(g):d.search(g);return f.then?f.then(function(c){return e.promises[b]=c=c.result||c,a(c),c}):(f=f.result||f,a(f),f)}})(i,Object.assign({},d)),this.promises.push(f),l[o]=f;continue}else d.resolve=!1,d.index=null,a=c?i.searchAsync(d):i.search(d),d.resolve=g,d.index=i}else if(d.and)a=inner_call(d,"and",i);else if(d.or)a=inner_call(d,"or",i);else if(d.not)a=inner_call(d,"not",i);else if(d.xor)a=inner_call(d,"xor",i);else continue}a.await?(k=1,a=a.await):a.then?(k=1,a=a.then(function(a){return a.result||a})):a=a.result||a,l[o]=a}if(k&&!this.await&&(this.await=new Promise(function(a){e.return=a})),k){const a=Promise.all(l).then(function(c){for(let d=0;d<e.promises.length;d++)if(e.promises[d]===a){e.promises[d]=function(){return b.call(e,c,m,n,f,g,h,j)};break}e.execute()});this.promises.push(a)}else if(this.await)this.promises.push(function(){return b.call(e,l,m,n,f,g,h,j)});else return b.call(this,l,m,n,f,g,h,j);return g?this.await||this.result:this};function inner_call(a,b,c){const d=a[b],e=d[0]||d;e.index||(e.index=c);let f=new Resolver(e);return 1<d.length&&(f=f[b].apply(f,d.slice(1))),f}