var B=Object.defineProperty;var E=(n,t,s)=>t in n?B(n,t,{enumerable:!0,configurable:!0,writable:!0,value:s}):n[t]=s;var C=(n,t,s)=>E(n,typeof t!="symbol"?t+"":t,s);import{ar as b,c as F,i as I,as as A}from"./rpcWorker-CVTx5FIM.js";import{B as O}from"./index-P3rx-5SH.js";const g=65536,k=10;function T(n,t){const s=new Set;return n.filter(e=>{const o=t(e);return s.has(o)?!1:s.add(o)})}class q{constructor(t,s,e=20){C(this,"ixxFile");C(this,"ixFile");C(this,"maxResults");this.ixxFile=t,this.ixFile=s,this.maxResults=e}async search(t,s){let e=[];const i=t.split(" ")[0].toLowerCase(),r=await this._getBuffer(i,s);if(!r)return[];let{end:c,buffer:d}=r,l=!1;for(;!l;){const u=d.toString(),p=u.slice(0,u.lastIndexOf(`
`)).split(`
`).filter(a=>!!a),y=[];for(const a of p){const m=a.split(" ")[0],h=m.startsWith(i);m.slice(0,i.length)>i&&(l=!0),h&&y.push(a)}const x=y.flatMap(a=>{const[m,...h]=a.split(" ");return h.map(w=>[m,w.split(",")[0]])});if(e.length+x.length<this.maxResults&&!l){const a=await this.ixFile.read(b.Buffer.alloc(g),0,g,c,s);if(!a.bytesRead){e=e.concat(x);break}d=b.Buffer.concat([d,a.buffer]),c+=g}else if(e.length+x.length>=this.maxResults||l){e=e.concat(x);break}}return T(e,u=>u[1]).slice(0,this.maxResults)}async getIndex(t){return(await this.ixxFile.readFile({encoding:"utf8",...t})).split(`
`).filter(e=>!!e).map(e=>{const o=e.length-k,i=e.slice(0,o),r=e.slice(o),c=Number.parseInt(r,16);return[i,c]})}async _getBuffer(t,s){let e=0,o=65536;const i=await this.getIndex(s);for(const[d,l]of i)d.slice(0,t.length)<t&&(e=l,o=l+65536);const r=o-e;return r<0?void 0:{...await this.ixFile.read(b.Buffer.alloc(r),0,r,e,s),end:o}}}function L(n){try{return decodeURIComponent(n)}catch{return n}}function S(n,t,s=15){const e=n.toLowerCase().indexOf(t);return n.length<40?n:(Math.max(0,e-s)>0?"...":"")+n.slice(Math.max(0,e-s),e+t.length+s).trim()+(e+t.length<n.length?"...":"")}class U extends O.BaseAdapter{constructor(t,s,e){super(t,s,e);const o=F.readConfObject(t,"ixFilePath"),i=F.readConfObject(t,"ixxFilePath");if(!o)throw new Error("must provide out.ix");if(!i)throw new Error("must provide out.ixx");this.trixJs=new q(I.openLocation(i,e),I.openLocation(o,e),1500)}async searchIndex(t){const s=t.queryString.toLowerCase(),e=s.split(" "),i=(await this.trixJs.search(s)).filter(([,r])=>e.every(c=>L(r).toLowerCase().includes(c))).map(([r,c])=>{const d=JSON.parse(c.replaceAll("|",",")),[l,u,...p]=d.map(f=>L(f)),y=p.findIndex(f=>!!f),x=p.map(f=>f.toLowerCase()).findIndex(f=>f.includes(r.toLowerCase())),a=p[y],m=p[x],h=x!==-1?S(m,r):void 0,w=S(a,r),R=!h||w.toLowerCase()===h.toLowerCase()?w:`${w} (${h})`;return new A({locString:l,label:a,displayString:R,matchedObject:d.map(f=>decodeURIComponent(f)),trackId:u})});return t.searchType==="exact"?i.filter(r=>r.getLabel().toLowerCase()===t.queryString.toLowerCase()):i}freeResources(){}}export{U as default};
