import{DocumentSearchOptions,DocumentSearchResults,EnrichedDocumentSearchResults,MergedDocumentSearchResults,MergedDocumentSearchEntry,EnrichedSearchResults,SearchResults,IntermediateSearchResults}from"../type.js";import{create_object,is_array,is_object,is_string,inherit}from"../common.js";import{intersect,intersect_union}from"../intersect.js";import Document from"../document.js";import Index from"../index.js";import WorkerIndex from"../worker.js";import Resolver from"../resolver.js";import tick from"../profiler.js";import{highlight_fields}from"./highlight.js";Document.prototype.search=function(a,b,c,d){!1,c||(!b&&is_object(a)?(c=a,a=""):is_object(b)&&(c=b,b=0));let e,f,g,h,i,j,k,l,m,n,o=[],p=[],q=0,r=!0;if(c){if(is_array(c)&&(c={index:c}),a=c.query||a,e=c.pluck,g=c.merge,i=c.boost,k=e||c.field||(k=c.index)&&(k.index?null:k),l=this.tag&&c.tag,h=c.suggest,r=!1!==c.resolve,j=c.cache,!1,n=r&&this.store&&c.highlight,f=!!n||r&&this.store&&c.enrich,b=c.limit||b,m=c.offset||0,b||(b=r?100:0),l&&(!this.db||!d)){!1,l.constructor!==Array&&(l=[l]);let c=[];for(let a,b=0;b<l.length;b++)if(a=l[b],a.field&&a.tag){const b=a.tag;if(b.constructor===Array)for(let d=0;d<b.length;d++)c.push(a.field,b[d]);else c.push(a.field,b)}else{const b=Object.keys(a);for(let d,e,f=0;f<b.length;f++)if(d=b[f],e=a[d],e.constructor===Array)for(let a=0;a<e.length;a++)c.push(d,e[a]);else c.push(d,e)}if(l=c,!a){let a=[];if(c.length)for(let d=0;d<c.length;d+=2){let e;if(this.db){const g=this.index.get(c[d]);if(!g){continue}!1,a.push(e=g.db.tag(c[d+1],b,m,f))}else!1,e=get_tag.call(this,c[d],c[d+1],b,m,f);o.push(r?{field:c[d],tag:c[d+1],result:e}:[e])}if(a.length){const b=this;return Promise.all(a).then(function(a){for(let b=0;b<a.length;b++)r?o[b].result=a[b]:o[b]=a[b];return r?o:new Resolver(1<o.length?intersect(o,1,0,0,h,i):o[0],b)})}return r?o:new Resolver(1<o.length?intersect(o,1,0,0,h,i):o[0],this)}}r||e||(k=k||this.field,k&&(is_string(k)?e=k:(is_array(k)&&1===k.length&&(k=k[0]),e=k.field||k.index))),k&&k.constructor!==Array&&(k=[k])}k||(k=this.field);let s,t=(this.worker||this.db)&&!d&&[];for(let e,g,u,v=0;v<k.length;v++){if(g=k[v],this.db&&this.tag&&!this.tree[v])continue;let i;if(is_string(g)||(i=g,g=i.field,a=i.query||a,b=inherit(i.limit,b),m=inherit(i.offset,m),h=inherit(i.suggest,h),n=r&&this.store&&inherit(i.highlight,n),f=!!n||r&&this.store&&inherit(i.enrich,f),j=inherit(i.cache,j)),d)e=d[v];else{const d=i||c||{},f=d.enrich,h=this.index.get(g);if(l&&(this.db&&(d.tag=l,s=h.db.support_tag_search,d.field=k),!s&&f&&(d.enrich=!1)),e=j?h.searchCache(a,b,d):h.search(a,b,d),f&&(d.enrich=f),t){t[v]=e;continue}}if(e=e.result||e,u=e&&e.length,l&&u){const a=[];let b=0;if(!(this.db&&d))for(let c,d,e=0;e<l.length;e+=2){if(!1,c=this.tag.get(l[e]),!c)if(!1,h)continue;else return r?o:new Resolver(o,this);if(c=c&&c.get(l[e+1]),d=c&&c.length,d)b++,a.push(c);else if(!h)return r?o:new Resolver(o,this)}else if(!s)for(let c=k.length;c<d.length;c++){let e=d[c],f=e&&e.length;if(f)b++,a.push(e);else if(!h)return r?o:new Resolver(o,this)}if(b){if(!1,e=intersect_union(e,a,r),u=e.length,!u&&!h)return r?e:new Resolver(e,this);b--}}if(u)p[q]=g,o.push(e),q++;else if(1===k.length)return r?o:new Resolver(o,this)}if(t){if(this.db&&l&&l.length&&!s)for(let a=0;a<l.length;a+=2){const c=this.index.get(l[a]);if(!c)if(!1,h)continue;else return r?o:new Resolver(o,this);!1,t.push(c.db.tag(l[a+1],b,m,!1))}const d=this;return Promise.all(t).then(function(e){return c&&(c.resolve=r),e.length&&(e=d.search(a,b,c,e)),e})}if(!q)return r?o:new Resolver(o,this);if(e&&(!f||!this.store))return o=o[0],r?o:new Resolver(o,this);t=[];for(let g,h=0;h<p.length;h++){if(g=o[h],f&&g.length&&"undefined"==typeof g[0].doc&&(this.db?(!1,t.push(g=this.index.get(this.field[0]).db.enrich(g))):g=apply_enrich.call(this,g)),e)return r?n?highlight_fields(a,g,this.index,e,n):g:new Resolver(g,this);o[h]={field:p[h],result:g}}if(f&&!0&&this.db&&t.length){const b=this;return Promise.all(t).then(function(c){for(let a=0;a<c.length;a++)o[a].result=c[a];return n&&(o=highlight_fields(a,o,b.index,e,n)),g?merge_fields(o):o})}return n&&(o=highlight_fields(a,o,this.index,e,n)),g?merge_fields(o):o};function merge_fields(a){const b=[],c=create_object(),d=create_object();for(let e,f,g,h,k,l,m,n=0;n<a.length;n++){e=a[n],f=e.field,g=e.result;for(let a=0;a<g.length;a++)k=g[a],"object"==typeof k?h=k.id:k={id:h=k},l=c[h],l?l.push(f):(k.field=c[h]=[f],b.push(k)),(m=k.highlight)&&(l=d[h],!l&&(d[h]=l={},k.highlight=l),l[f]=m)}return b}function get_tag(a,b,c,d,e){let f=this.tag.get(a);if(!f)return[];if(f=f.get(b),!f)return[];let g=f.length-d;return 0<g&&((c&&g>c||d)&&(f=f.slice(d,d+c)),e&&(f=apply_enrich.call(this,f))),f}export function apply_enrich(a){if(!this||!this.store)return a;if(this.db)return this.index.get(this.field[0]).db.enrich(a);const b=Array(a.length);for(let c,d=0;d<a.length;d++)c=a[d],b[d]={id:c,doc:this.store.get(c)};return b}