import{ar as p,c as d,as as n}from"./rpcWorker-CVTx5FIM.js";import{B as l}from"./index-P3rx-5SH.js";import{c as f}from"./crc32-Cb79amSz.js";class w{constructor(t){this.url=t.url.endsWith("/")?t.url:`${t.url}/`}async readMeta(){const t=await this.loadFile("meta.json"),{compress:a,track_names:r}=t;return{hashHexCharacters:Math.ceil(t.hash_bits/4),compress:a,tracks:r}}async getHashHexCharacters(){return(await this.readMeta()).hashHexCharacters}async getCompress(){return(await this.readMeta()).compress}async getTrackNames(){return(await this.readMeta()).tracks}async get(t){return(await this.getBucket(t))[t]}async getBucket(t){const a=this.hash(t),r=await this.hexToDirPath(a);return this.loadFile(r)}async loadFile(t){const a=await fetch(`${this.url}${t}`);if(!a.ok)throw new Error(`HTTP ${a.status} ${a.statusText}`);return a.json()}async hexToDirPath(t){const a=await this.getHashHexCharacters();if(a){const r=await this.getCompress();for(;t.length<8;)t=`0${t}`;t=t.slice(8-a);const e=[];for(let s=0;s<t.length;s+=3)e.push(t.slice(s,s+3));return`${e.join("/")}.json${r?"z":""}`}return""}hash(t){return f(p.Buffer.from(t)).toString(16).toLowerCase().replace("-","n")}}class k extends l.BaseAdapter{constructor(t,a,r){super(t,a,r);const e=d.readConfObject(t,"namesIndexLocation"),{baseUri:s,uri:c}=e;this.httpMap=new w({url:s?new URL(c,s).href:c})}async loadIndexFile(t){return this.httpMap.getBucket(t)}async searchIndex(t){const{searchType:a,queryString:r}=t,e=this.tracksNames||await this.httpMap.getTrackNames(),s=r.toLowerCase(),c=await this.loadIndexFile(s);return c[s]?this.formatResults(c[s],e,a):[]}formatResults(t,a,r){return[...r==="exact"?[]:t.prefix.map(e=>new n({label:typeof e=="object"?e.name:e,matchedAttribute:"name",matchedObject:{result:e}})),...t.exact.map(e=>{const s=e[0],c=e[1],i=e[3],h=e[4],m=e[5],u=`${i||s}:${h}-${m}`;return new n({locString:u,label:s,matchedAttribute:"name",matchedObject:e,trackId:a[c]})})].filter(e=>e.getLabel()!=="too many matches")}freeResources(){}}export{k as default};
