!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).MiniSearch=e()}(this,(function(){"use strict";var t=function(){return t=Object.assign||function(t){for(var e,r=1,n=arguments.length;r=t.length&&(t=void 0),{value:t&&t[n++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}function r(t,e){var r="function"==typeof Symbol&&t[Symbol.iterator];if(!r)return t;var n,i,o=r.call(t),u=[];try{for(;(void 0===e||e-- >0)&&!(n=o.next()).done;)u.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(r=o.return)&&r.call(o)}finally{if(i)throw i.error}}return u}"function"==typeof SuppressedError&&SuppressedError;var n,i="KEYS",o="VALUES",u="",a=function(){function t(t,e){var r=t._tree,n=Array.from(r.keys());this.set=t,this._type=e,this._path=n.length>0?[{node:r,keys:n}]:[]}return t.prototype.next=function(){var t=this.dive();return this.backtrack(),t},t.prototype.dive=function(){if(0===this._path.length)return{done:!0,value:void 0};var t=s(this._path),e=t.node,r=t.keys;if(s(r)===u)return{done:!1,value:this.result()};var n=e.get(s(r));return this._path.push({node:n,keys:Array.from(n.keys())}),this.dive()},t.prototype.backtrack=function(){if(0!==this._path.length){var t=s(this._path).keys;t.pop(),t.length>0||(this._path.pop(),this.backtrack())}},t.prototype.key=function(){return this.set._prefix+this._path.map((function(t){var e=t.keys;return s(e)})).filter((function(t){return t!==u})).join("")},t.prototype.value=function(){return s(this._path).node.get(u)},t.prototype.result=function(){switch(this._type){case o:return this.value();case i:return this.key();default:return[this.key(),this.value()]}},t.prototype[Symbol.iterator]=function(){return this},t}(),s=function(t){return t[t.length-1]},l=function(t,r,n,i,o,a,s,c){var h,d,f=a*s;try{t:for(var y=e(t.keys()),v=y.next();!v.done;v=y.next()){var p=v.value;if(p===u){var m=o[f-1];m<=n&&i.set(c,[t.get(p),m])}else{for(var _=a,g=0;gn)continue t}l(t.get(p),r,n,i,o,_,s,c+p)}}}catch(t){h={error:t}}finally{try{v&&!v.done&&(d=y.return)&&d.call(y)}finally{if(h)throw h.error}}},c=function(){function t(t,e){void 0===t&&(t=new Map),void 0===e&&(e=""),this._size=void 0,this._tree=t,this._prefix=e}return t.prototype.atPrefix=function(n){var i,o;if(!n.startsWith(this._prefix))throw new Error("Mismatched prefix");var a=r(h(this._tree,n.slice(this._prefix.length)),2),s=a[0],l=a[1];if(void 0===s){var c=r(m(l),2),d=c[0],f=c[1];try{for(var y=e(d.keys()),v=y.next();!v.done;v=y.next()){var p=v.value;if(p!==u&&p.startsWith(f)){var _=new Map;return _.set(p.slice(f.length),d.get(p)),new t(_,n)}}}catch(t){i={error:t}}finally{try{v&&!v.done&&(o=y.return)&&o.call(y)}finally{if(i)throw i.error}}}return new t(s,n)},t.prototype.clear=function(){this._size=void 0,this._tree.clear()},t.prototype.delete=function(t){return this._size=void 0,y(this._tree,t)},t.prototype.entries=function(){return new a(this,"ENTRIES")},t.prototype.forEach=function(t){var n,i;try{for(var o=e(this),u=o.next();!u.done;u=o.next()){var a=r(u.value,2);t(a[0],a[1],this)}}catch(t){n={error:t}}finally{try{u&&!u.done&&(i=o.return)&&i.call(o)}finally{if(n)throw n.error}}},t.prototype.fuzzyGet=function(t,e){return function(t,e,r){var n=new Map;if(void 0===e)return n;for(var i=e.length+1,o=i+r,u=new Uint8Array(o*i).fill(r+1),a=0;a0)throw new Error("Expected documents to be present. Omit the argument to remove all documents.");this._index=new c,this._documentCount=0,this._documentIds=new Map,this._idToShortId=new Map,this._fieldLength=new Map,this._avgFieldLength=[],this._storedFields=new Map,this._nextId=0}},n.prototype.discard=function(t){var e=this,r=this._idToShortId.get(t);if(null==r)throw new Error("MiniSearch: cannot discard document with ID ".concat(t,": it is not in the index"));this._idToShortId.delete(t),this._documentIds.delete(r),this._storedFields.delete(r),(this._fieldLength.get(r)||[]).forEach((function(t,n){e.removeFieldLength(r,n,e._documentCount,t)})),this._fieldLength.delete(r),this._documentCount-=1,this._dirtCount+=1,this.maybeAutoVacuum()},n.prototype.maybeAutoVacuum=function(){if(!1!==this._options.autoVacuum){var t=this._options.autoVacuum,e=t.minDirtFactor,r=t.minDirtCount,n=t.batchSize,i=t.batchWait;this.conditionalVacuum({batchSize:n,batchWait:i},{minDirtCount:r,minDirtFactor:e})}},n.prototype.discardAll=function(t){var r,n,i=this._options.autoVacuum;try{this._options.autoVacuum=!1;try{for(var o=e(t),u=o.next();!u.done;u=o.next()){var a=u.value;this.discard(a)}}catch(t){r={error:t}}finally{try{u&&!u.done&&(n=o.return)&&n.call(o)}finally{if(r)throw r.error}}}finally{this._options.autoVacuum=i}this.maybeAutoVacuum()},n.prototype.replace=function(t){var e=this._options,r=e.idField,n=(0,e.extractField)(t,r);this.discard(n),this.add(t)},n.prototype.vacuum=function(t){return void 0===t&&(t={}),this.conditionalVacuum(t)},n.prototype.conditionalVacuum=function(t,e){var r=this;return this._currentVacuum?(this._enqueuedVacuumConditions=this._enqueuedVacuumConditions&&e,null!=this._enqueuedVacuum||(this._enqueuedVacuum=this._currentVacuum.then((function(){var e=r._enqueuedVacuumConditions;return r._enqueuedVacuumConditions=S,r.performVacuuming(t,e)}))),this._enqueuedVacuum):!1===this.vacuumConditionsMet(e)?Promise.resolve():(this._currentVacuum=this.performVacuuming(t),this._currentVacuum)},n.prototype.performVacuuming=function(t,n){return function(t,e,r,n){return new(r||(r=Promise))((function(i,o){function u(t){try{s(n.next(t))}catch(t){o(t)}}function a(t){try{s(n.throw(t))}catch(t){o(t)}}function s(t){var e;t.done?i(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(u,a)}s((n=n.apply(t,e||[])).next())}))}(this,void 0,void 0,(function(){var i,o,u,a,s,l,c,h,d,f,y,v,p,m,_,g,F,x,w,b,A,C,E,S,D;return function(t,e){var r,n,i,o,u={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(a){return function(s){return function(a){if(r)throw new TypeError("Generator is already executing.");for(;o&&(o=0,a[0]&&(u=0)),u;)try{if(r=1,n&&(i=2&a[0]?n.return:a[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,a[1])).done)return i;switch(n=0,i&&(a=[2&a[0],i.value]),a[0]){case 0:case 1:i=a;break;case 4:return u.label++,{value:a[1],done:!1};case 5:u.label++,n=a[1],a=[0];continue;case 7:a=u.ops.pop(),u.trys.pop();continue;default:if(!((i=(i=u.trys).length>0&&i[i.length-1])||6!==a[0]&&2!==a[0])){u=0;continue}if(3===a[0]&&(!i||a[1]>i[0]&&a[1]=e&&this.dirtFactor>=r},Object.defineProperty(n.prototype,"isVacuuming",{get:function(){return null!=this._currentVacuum},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"dirtCount",{get:function(){return this._dirtCount},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"dirtFactor",{get:function(){return this._dirtCount/(1+this._documentCount+this._dirtCount)},enumerable:!1,configurable:!0}),n.prototype.has=function(t){return this._idToShortId.has(t)},n.prototype.getStoredFields=function(t){var e=this._idToShortId.get(t);if(null!=e)return this._storedFields.get(e)},n.prototype.search=function(t,i){var o,u;void 0===i&&(i={});var a=this.executeQuery(t,i),s=[];try{for(var l=e(a),c=l.next();!c.done;c=l.next()){var h=r(c.value,2),d=h[0],f=h[1],y=f.score,v=f.terms,p=f.match,m=v.length||1,_={id:this._documentIds.get(d),score:y*m,terms:Object.keys(p),queryTerms:v,match:p};Object.assign(_,this._storedFields.get(d)),(null==i.filter||i.filter(_))&&s.push(_)}}catch(t){o={error:t}}finally{try{c&&!c.done&&(u=l.return)&&u.call(l)}finally{if(o)throw o.error}}return t===n.wildcard&&null==i.boostDocument&&null==this._options.searchOptions.boostDocument||s.sort(M),s},n.prototype.autoSuggest=function(n,i){var o,u,a,s;void 0===i&&(i={}),i=t(t({},this._options.autoSuggestOptions),i);var l=new Map;try{for(var c=e(this.search(n,i)),h=c.next();!h.done;h=c.next()){var d=h.value,f=d.score,y=(x=d.terms).join(" ");null!=(g=l.get(y))?(g.score+=f,g.count+=1):l.set(y,{score:f,terms:x,count:1})}}catch(t){o={error:t}}finally{try{h&&!h.done&&(u=c.return)&&u.call(c)}finally{if(o)throw o.error}}var v=[];try{for(var p=e(l),m=p.next();!m.done;m=p.next()){var _=r(m.value,2),g=_[0],F=_[1],x=(f=F.score,F.terms),w=F.count;v.push({suggestion:g,terms:x,score:f/w})}}catch(t){a={error:t}}finally{try{m&&!m.done&&(s=p.return)&&s.call(p)}finally{if(a)throw a.error}}return v.sort(M),v},Object.defineProperty(n.prototype,"documentCount",{get:function(){return this._documentCount},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"termCount",{get:function(){return this._index.size},enumerable:!1,configurable:!0}),n.loadJSON=function(t,e){if(null==e)throw new Error("MiniSearch: loadJSON should be given the same options used when serializing the index");return this.loadJS(JSON.parse(t),e)},n.getDefault=function(t){if(A.hasOwnProperty(t))return F(A,t);throw new Error('MiniSearch: unknown option "'.concat(t,'"'))},n.loadJS=function(t,i){var o,u,a,s,l,h,d=t.index,f=t.documentCount,y=t.nextId,v=t.documentIds,p=t.fieldIds,m=t.fieldLength,_=t.averageFieldLength,g=t.storedFields,F=t.dirtCount,x=t.serializationVersion;if(1!==x&&2!==x)throw new Error("MiniSearch: cannot deserialize an index created with an incompatible version");var w=new n(i);w._documentCount=f,w._nextId=y,w._documentIds=V(v),w._idToShortId=new Map,w._fieldIds=p,w._fieldLength=V(m),w._avgFieldLength=_,w._storedFields=V(g),w._dirtCount=F||0,w._index=new c;try{for(var b=e(w._documentIds),A=b.next();!A.done;A=b.next()){var C=r(A.value,2),E=C[0],z=C[1];w._idToShortId.set(z,E)}}catch(t){o={error:t}}finally{try{A&&!A.done&&(u=b.return)&&u.call(b)}finally{if(o)throw o.error}}try{for(var S=e(d),D=S.next();!D.done;D=S.next()){var I=r(D.value,2),k=I[0],M=I[1],O=new Map;try{for(var T=(l=void 0,e(Object.keys(M))),L=T.next();!L.done;L=T.next()){var j=L.value,B=M[j];1===x&&(B=B.ds),O.set(parseInt(j,10),V(B))}}catch(t){l={error:t}}finally{try{L&&!L.done&&(h=T.return)&&h.call(T)}finally{if(l)throw l.error}}w._index.set(k,O)}}catch(t){a={error:t}}finally{try{D&&!D.done&&(s=S.return)&&s.call(S)}finally{if(a)throw a.error}}return w},n.prototype.executeQuery=function(e,r){var i=this;if(void 0===r&&(r={}),e===n.wildcard)return this.executeWildcardQuery(r);if("string"!=typeof e){var o=t(t(t({},r),e),{queries:void 0}),u=e.queries.map((function(t){return i.executeQuery(t,o)}));return this.combineResults(u,o.combineWith)}var a=this._options,s=a.tokenize,l=a.processTerm,c=a.searchOptions,h=t(t({tokenize:s,processTerm:l},c),r),d=h.tokenize,f=h.processTerm,y=d(e).flatMap((function(t){return f(t)})).filter((function(t){return!!t})).map(b(h)).map((function(t){return i.executeQuerySpec(t,h)}));return this.combineResults(y,h.combineWith)},n.prototype.executeQuerySpec=function(n,i){var o,u,a,s,l,c,h=t(t({},this._options.searchOptions),i),d=(h.fields||this._options.fields).reduce((function(e,r){var n;return t(t({},e),((n={})[r]=F(h.boost,r)||1,n))}),{}),f=h.boostDocument,y=h.weights,v=h.maxFuzzy,p=h.bm25,m=t(t({},C.weights),y),_=m.fuzzy,g=m.prefix,x=this._index.get(n.term),w=this.termResults(n.term,n.term,1,x,d,f,p);if(n.prefix&&(l=this._index.atPrefix(n.term)),n.fuzzy){var b=!0===n.fuzzy?.2:n.fuzzy,A=b<1?Math.min(v,Math.round(n.term.length*b)):b;A&&(c=this._index.fuzzyGet(n.term,A))}if(l)try{for(var E=e(l),z=E.next();!z.done;z=E.next()){var S=r(z.value,2),D=S[0],I=S[1];if(V=D.length-n.term.length){null==c||c.delete(D);var k=g*D.length/(D.length+.3*V);this.termResults(n.term,D,k,I,d,f,p,w)}}}catch(t){o={error:t}}finally{try{z&&!z.done&&(u=E.return)&&u.call(E)}finally{if(o)throw o.error}}if(c)try{for(var M=e(c.keys()),O=M.next();!O.done;O=M.next()){D=O.value;var V,T=r(c.get(D),2),L=T[0];(V=T[1])&&(k=_*D.length/(D.length+V),this.termResults(n.term,D,k,L,d,f,p,w))}}catch(t){a={error:t}}finally{try{O&&!O.done&&(s=M.return)&&s.call(M)}finally{if(a)throw a.error}}return w},n.prototype.executeWildcardQuery=function(n){var i,o,u=new Map,a=t(t({},this._options.searchOptions),n);try{for(var s=e(this._documentIds),l=s.next();!l.done;l=s.next()){var c=r(l.value,2),h=c[0],d=c[1],f=a.boostDocument?a.boostDocument(d,"",this._storedFields.get(h)):1;u.set(h,{score:f,terms:[],match:{}})}}catch(t){i={error:t}}finally{try{l&&!l.done&&(o=s.return)&&o.call(s)}finally{if(i)throw i.error}}return u},n.prototype.combineResults=function(t,e){if(void 0===e&&(e=_),0===t.length)return new Map;var r=e.toLowerCase();return t.reduce(x[r])||new Map},n.prototype.toJSON=function(){var t,n,i,o,u=[];try{for(var a=e(this._index),s=a.next();!s.done;s=a.next()){var l=r(s.value,2),c=l[0],h=l[1],d={};try{for(var f=(i=void 0,e(h)),y=f.next();!y.done;y=f.next()){var v=r(y.value,2),p=v[0],m=v[1];d[p]=Object.fromEntries(m)}}catch(t){i={error:t}}finally{try{y&&!y.done&&(o=f.return)&&o.call(f)}finally{if(i)throw i.error}}u.push([c,d])}}catch(n){t={error:n}}finally{try{s&&!s.done&&(n=a.return)&&n.call(a)}finally{if(t)throw t.error}}return{documentCount:this._documentCount,nextId:this._nextId,documentIds:Object.fromEntries(this._documentIds),fieldIds:this._fieldIds,fieldLength:Object.fromEntries(this._fieldLength),averageFieldLength:this._avgFieldLength,storedFields:Object.fromEntries(this._storedFields),dirtCount:this._dirtCount,index:u,serializationVersion:2}},n.prototype.termResults=function(t,r,n,i,o,u,a,s){var l,c,h,d,f;if(void 0===s&&(s=new Map),null==i)return s;try{for(var y=e(Object.keys(o)),v=y.next();!v.done;v=y.next()){var p=v.value,m=o[p],_=this._fieldIds[p],g=i.get(_);if(null!=g){var x=g.size,b=this._avgFieldLength[_];try{for(var A=(h=void 0,e(g.keys())),C=A.next();!C.done;C=A.next()){var E=C.value;if(this._documentIds.has(E)){var z=u?u(this._documentIds.get(E),r,this._storedFields.get(E)):1;if(z){var S=g.get(E),D=this._fieldLength.get(E)[_],k=n*m*z*w(S,x,this._documentCount,D,b,a),M=s.get(E);if(M){M.score+=k,I(M.terms,t);var O=F(M.match,r);O?O.push(p):M.match[r]=[p]}else s.set(E,{score:k,terms:[t],match:(f={},f[r]=[p],f)})}}else this.removeTerm(_,E,r),x-=1}}catch(t){h={error:t}}finally{try{C&&!C.done&&(d=A.return)&&d.call(A)}finally{if(h)throw h.error}}}}}catch(t){l={error:t}}finally{try{v&&!v.done&&(c=y.return)&&c.call(y)}finally{if(l)throw l.error}}return s},n.prototype.addTerm=function(t,e,r){var n=this._index.fetch(r,O),i=n.get(t);if(null==i)(i=new Map).set(e,1),n.set(t,i);else{var o=i.get(e);i.set(e,(o||0)+1)}},n.prototype.removeTerm=function(t,e,r){if(this._index.has(r)){var n=this._index.fetch(r,O),i=n.get(t);null==i||null==i.get(e)?this.warnDocumentChanged(e,t,r):i.get(e)<=1?i.size<=1?n.delete(t):i.delete(e):i.set(e,i.get(e)-1),0===this._index.get(r).size&&this._index.delete(r)}else this.warnDocumentChanged(e,t,r)},n.prototype.warnDocumentChanged=function(t,r,n){var i,o;try{for(var u=e(Object.keys(this._fieldIds)),a=u.next();!a.done;a=u.next()){var s=a.value;if(this._fieldIds[s]===r)return void this._options.logger("warn","MiniSearch: document with ID ".concat(this._documentIds.get(t),' has changed before removal: term "').concat(n,'" was not present in field "').concat(s,'". Removing a document after it has changed can corrupt the index!'),"version_conflict")}}catch(t){i={error:t}}finally{try{a&&!a.done&&(o=u.return)&&o.call(u)}finally{if(i)throw i.error}}},n.prototype.addDocumentId=function(t){var e=this._nextId;return this._idToShortId.set(t,e),this._documentIds.set(e,t),this._documentCount+=1,this._nextId+=1,e},n.prototype.addFields=function(t){for(var e=0;e