import{I as T}from"./main-BPWpXae6.js";import{B as x}from"./index-P3rx-5SH.js";import{u as g,i as b}from"./rpcWorker-CVTx5FIM.js";import{r as F}from"./rxjs-Dgg09m9e.js";class N extends x.BaseFeatureDataAdapter{constructor(){super(...arguments),this.intervalTrees={}}async getNames(){const{header:e,columnNames:t}=await this.loadData();if(t.length)return t;const s=e.split(/\n|\r\n|\r/).filter(i=>!!i).at(-1);return s!=null&&s.includes("	")?s.slice(1).split("	").map(i=>i.trim()):void 0}async loadFeatureIntervalTreeHelper(e){var t;const{features:c}=await this.loadData(),s=c[e];if(!s)return;const i=((t=await this.getNames())===null||t===void 0?void 0:t.slice(3))||[],n=new T;for(let a=0;a<s.length;a++){const r=s[a],[h,d,f,...o]=r.split("	");for(let l=0;l<o.length;l++){const u=`${this.id}-${h}-${a}-${l}`,p=+d,m=+f,v=+o[l],w=i[l]||`col${l}`;v&&n.insert([p,m],new g.SimpleFeature({id:u,data:{refName:h,start:p,end:m,score:v,source:w}}))}}return n}async getRefNames(e={}){const{features:t}=await this.loadData(e);return Object.keys(t)}async loadDataP(e={}){const t=this.pluginManager,c=this.getConf("bedGraphLocation"),s=await g.fetchAndMaybeUnzip(b.openLocation(c,t),e);if(s.length>536870888)throw new Error("Data exceeds maximum string length (512MB)");const n=new TextDecoder("utf8",{fatal:!0}).decode(s).split(/\n|\r\n|\r/).filter(o=>!!o),a=[];let r=0;for(;r<n.length&&n[r].startsWith("#");r++)a.push(n[r]);const h=a.join(`
`),d={};for(;r<n.length;r++){const o=n[r],l=o.indexOf("	"),u=o.slice(0,l);d[u]||(d[u]=[]),d[u].push(o)}const f=this.getConf("columnNames");return{header:h,features:d,columnNames:f}}async loadFeatureIntervalTree(e){return this.intervalTrees[e]||(this.intervalTrees[e]=this.loadFeatureIntervalTreeHelper(e).catch(t=>{throw this.intervalTrees[e]=void 0,t})),this.intervalTrees[e]}async loadData(e={}){return this.bedFeatures||(this.bedFeatures=this.loadDataP(e).catch(t=>{throw this.bedFeatures=void 0,t})),this.bedFeatures}getFeatures(e,t={}){return F.ObservableCreate(async c=>{const{start:s,end:i,refName:n}=e,a=await this.loadFeatureIntervalTree(n);a==null||a.search([s,i]).forEach(r=>{c.next(r)}),c.complete()})}freeResources(){}}export{N as default};
