import{ar as m,at as $}from"./rpcWorker-CVTx5FIM.js";import{B as T,a as I,p as k,b as A}from"./BamAdapter-BR_9DsQb.js";import"./crc32-Cb79amSz.js";import"./AbortablePromiseCache-xs5eyOH3.js";import"./index-P3rx-5SH.js";import"./QuickLRU-Cm0slBPC.js";import"./rxjs-Dgg09m9e.js";async function w(u,t){const a=await Promise.all(u.map(async e=>{const{url:r,headers:s}=e;if(r.startsWith("data:"))return m.Buffer.from(r.split(",")[1],"base64");{const{referer:h,...i}=s,n=await fetch(r,{...t,headers:{...t==null?void 0:t.headers,...i}});if(!n.ok)throw new Error(`HTTP ${n.status} fetching ${r}: ${await n.text()}`);return m.Buffer.from(await n.arrayBuffer())}}));return m.Buffer.concat(await Promise.all(a.map(e=>$(e))))}class B extends T{constructor(t){super({htsget:!0}),this.baseUrl=t.baseUrl,this.trackId=t.trackId}async*streamRecordsForRange(t,a,e,r){var s;const i=`${`${this.baseUrl}/${this.trackId}`}?referenceName=${t}&start=${a}&end=${e}&format=BAM`,n=(s=this.chrToIndex)===null||s===void 0?void 0:s[t];if(n===void 0)yield[];else{const o=await fetch(i,{...r});if(!o.ok)throw new Error(`HTTP ${o.status} fetching ${i}: ${await o.text()}`);const c=await o.json(),l=await w(c.htsget.urls.slice(1),r);yield*this._fetchChunkFeatures([{buffer:l,_fetchedSize:void 0,bin:0,compareTo(){return 0},toUniqueString(){return`${t}_${a}_${e}`},fetchedSize(){return 0},minv:{dataPosition:0,blockPosition:0,compareTo:()=>0},maxv:{dataPosition:Number.MAX_SAFE_INTEGER,blockPosition:0,compareTo:()=>0},toString(){return`${t}_${a}_${e}`}}],n,a,e,r)}}async _readChunk({chunk:t}){if(!t.buffer)throw new Error("expected chunk.buffer in htsget");return{data:t.buffer,cpositions:[],dpositions:[],chunk:t}}async getHeader(t={}){const a=`${this.baseUrl}/${this.trackId}?referenceName=na&class=header`,e=await fetch(a,t);if(!e.ok)throw new Error(`HTTP ${e.status} fetching ${a}: ${await e.text()}`);const r=await e.json(),s=await w(r.htsget.urls,t);if(s.readInt32LE(0)!==I)throw new Error("Not a BAM file");const h=s.readInt32LE(4),i=s.toString("utf8",8,8+h),n=k(i),o=[],c={},l=n.filter(f=>f.tag==="SQ");for(const[f,b]of l.entries()){let g="",p=0;for(const d of b.data)d.tag==="SN"?g=d.value:d.tag==="LN"&&(p=+d.value);c[g]=f,o[f]={refName:g,length:p}}return this.chrToIndex=c,this.indexToChr=o,n}}class N extends A{async configurePre(){const t=this.getConf("htsgetBase"),a=this.getConf("htsgetTrackId"),e=new B({baseUrl:t,trackId:a}),r=this.getConf("sequenceAdapter");if(r&&this.getSubAdapter){const s=await this.getSubAdapter(r);return{bam:e,sequenceAdapter:s.dataAdapter}}return{bam:e}}}export{N as default};
