import{ResolverOptions,IntermediateSearchResults,SearchResults,EnrichedSearchResults,HighlightOptions}from"./type.js";import Index from"./index.js";import Document from"./document.js";import WorkerIndex from"./worker.js";import default_resolver from"./resolve/default.js";import"./resolve/handler.js";import"./resolve/or.js";import"./resolve/and.js";import"./resolve/xor.js";import"./resolve/not.js";import{apply_enrich}from"./document/search.js";import{highlight_fields}from"./document/highlight.js";export default function Resolver(a,b){if(!this||this.constructor!==Resolver)return new Resolver(a,b);let c,d,e,f,g,h,i=0;if(a&&a.index){const c=a;if(b=c.index,i=c.boost||0,d=c.query){e=c.field||c.pluck,f=c.highlight;const d=c.resolve,g=c.async||c.queue;c.resolve=!1,c.index=null,a=g?b.searchAsync(c):b.search(c),c.resolve=d,c.index=b,a=a.result||a}else a=[]}if(a&&a.then){const b=this;a=a.then(function(a){b.promises[0]=b.result=a.result||a,b.execute()}),c=[a],a=[],g=new Promise(function(a){h=a})}this.index=b||null,this.result=a||[],this.boostval=i,this.promises=c||[],this.await=g||null,this.return=h||null,this.highlight=f||null,this.query=d||"",this.field=e||""}Resolver.prototype.limit=function(a){if(this.await){const b=this;this.promises.push(function(){return b.limit(a).result})}else if(this.result.length){const b=[];for(let c,d=0;d<this.result.length;d++)if(c=this.result[d])if(!(c.length<=a)){b[d]=c.slice(0,a);break}else if(b[d]=c,a-=c.length,!a)break;this.result=b}return this},Resolver.prototype.offset=function(a){if(this.await){const b=this;this.promises.push(function(){return b.offset(a).result})}else if(this.result.length){const b=[];for(let c,d=0;d<this.result.length;d++)(c=this.result[d])&&(c.length<=a?a-=c.length:(b[d]=c.slice(a),a=0));this.result=b}return this},Resolver.prototype.boost=function(a){if(this.await){const b=this;this.promises.push(function(){return b.boost(a).result})}else this.boostval+=a;return this},Resolver.prototype.execute=function(a){let b=this.result,c=this.await;this.await=null;for(let d,e=0;e<this.promises.length;e++)if(d=this.promises[e])if("function"==typeof d)b=d(),this.promises[e]=b=b.result||b,e--;else if(d._fn)b=d._fn(),this.promises[e]=b=b.result||b,e--;else if(d.then)return this.await=c;const d=this.return;return this.promises=[],this.return=null,a||d(b),b},Resolver.prototype.resolve=function(a,b,c,d,e){let f=this.await?this.execute(!0):this.result;if(f.then){const g=this;return f.then(function(){return g.resolve(a,b,c,d,e)})}return f.length&&("object"==typeof a?(d=a.highlight||this.highlight,c=!!d||a.enrich,b=a.offset,a=a.limit):(d=d||this.highlight,c=!!d||c),f=e?c?apply_enrich.call(this.index,f):f:default_resolver.call(this.index,f,a||100,b,c)),this.finalize(f,d)},Resolver.prototype.finalize=function(a,b){if(a.then){const c=this;return a.then(function(a){return c.finalize(a,b)})}!1,b&&a.length&&this.query&&(a=highlight_fields(this.query,a,this.index.index,this.field,b));const c=this.return;return this.index=this.result=this.promises=this.await=this.return=null,this.highlight=null,this.query=this.field="",c&&c(a),a};