import { n as e, r as t } from "./esm-NIYEaYkc.js";
import { t as n } from "./esm-CuVa5T98.js";
import { r, t as i } from "./browser-KWU9rWZT.js";
//#region ../../node_modules/@gmod/bam/esm/chunk.js
var a = class {
	constructor(e, t, n, r) {
		this.minv = e, this.maxv = t, this.bin = n, this._fetchedSize = r;
	}
	toUniqueString() {
		return `${this.minv.toString()}..${this.maxv.toString()} (bin ${this.bin}, fetchedSize ${this.fetchedSize()})`;
	}
	toString() {
		return this.toUniqueString();
	}
	compareTo(e) {
		return this.minv.compareTo(e.minv) || this.maxv.compareTo(e.maxv) || this.bin - e.bin;
	}
	fetchedSize() {
		return this._fetchedSize === void 0 ? this.maxv.blockPosition + 65536 - this.minv.blockPosition : this._fetchedSize;
	}
}, o = 65536, s = o * o;
function c(e, t = 0) {
	let n = e[t] | e[t + 1] << 8 | e[t + 2] << 16 | e[t + 3] << 24;
	return ((e[t + 4] | e[t + 5] << 8 | e[t + 6] << 16 | e[t + 7] << 24) >>> 0) * s + (n >>> 0);
}
//#endregion
//#region ../../node_modules/@gmod/bam/esm/util.js
function l(e = {}) {
	return "aborted" in e ? { signal: e } : e;
}
function u(e, t) {
	let n = e.length;
	if (n === 0) return e;
	let r;
	if (t) {
		let i = t.blockPosition, a = t.dataPosition;
		r = [];
		for (let t = 0; t < n; t++) {
			let n = e[t], o = n.maxv;
			(o.blockPosition - i || o.dataPosition - a) > 0 && r.push(n);
		}
		if (r.length === 0) return r;
	} else r = e;
	r.sort((e, t) => {
		let n = e.minv.blockPosition - t.minv.blockPosition;
		return n === 0 ? e.minv.dataPosition - t.minv.dataPosition : n;
	});
	let i = [], a = r[0];
	i.push(a);
	let o = a.minv.blockPosition, s = a.maxv.blockPosition;
	for (let e = 1; e < r.length; e++) {
		let t = r[e], n = t.minv.blockPosition, c = t.maxv.blockPosition;
		if (n - s < 65e3 && c - o < 5e6) {
			let e = t.maxv, n = a.maxv;
			(c - s || e.dataPosition - n.dataPosition) > 0 && (a.maxv = e, s = c);
		} else i.push(t), a = t, o = n, s = c;
	}
	return i;
}
function d(e, t) {
	return { lineCount: c(e, t) };
}
function f(e, t) {
	return e ? e.compareTo(t) > 0 ? t : e : t;
}
function p(e, t = (e) => e) {
	let n = 0, r = 0, i = [], a = {};
	for (let o = 0; o < e.length; o += 1) if (!e[o]) {
		if (r < o) {
			let s = "";
			for (let t = r; t < o; t++) s += String.fromCharCode(e[t]);
			s = t(s), i[n] = s, a[s] = n;
		}
		r = o + 1, n += 1;
	}
	return {
		refNameToId: a,
		refIdToName: i
	};
}
function m(e) {
	let t = 0;
	for (let n of e) t += n.length;
	let n = new Uint8Array(t), r = 0;
	for (let t of e) n.set(t, r), r += t.length;
	return n;
}
function h(e, t, n) {
	return !!((e & t) !== t || e & n);
}
function g(e, t) {
	return t === "*" ? e === void 0 : `${e}` != `${t}`;
}
function _(e) {
	if (!e) return "";
	let { flagInclude: t = 0, flagExclude: n = 0, tagFilter: r } = e;
	return `:f${t}x${n}${r ? `:${r.tag}=${r.value ?? "*"}` : ""}`;
}
//#endregion
//#region ../../node_modules/@gmod/bam/esm/indexFile.js
var v = class {
	constructor({ filehandle: e, renameRefSeq: t = (e) => e }) {
		this.filehandle = e, this.renameRefSeq = t;
	}
	async estimatedBytesForRegions(e, t) {
		let n = u((await Promise.all(e.map((e) => this.blocksForRange(e.refId, e.start, e.end, t)))).flat()), r = 0;
		for (let e of n) r += e.fetchedSize();
		return r;
	}
}, y = class {
	constructor(e, t) {
		this.blockPosition = e, this.dataPosition = t;
	}
	toString() {
		return `${this.blockPosition}:${this.dataPosition}`;
	}
	compareTo(e) {
		return this.blockPosition - e.blockPosition || this.dataPosition - e.dataPosition;
	}
};
function b(e, t = 0, n = !1) {
	if (n) throw Error("big-endian virtual file offsets not implemented");
	return new y(e[t + 7] * 1099511627776 + e[t + 6] * 4294967296 + e[t + 5] * 16777216 + e[t + 4] * 65536 + e[t + 3] * 256 + e[t + 2], e[t + 1] << 8 | e[t]);
}
//#endregion
//#region ../../node_modules/@gmod/bam/esm/bai.js
var x = 21578050;
function S(e, t) {
	return e - e % t;
}
function C(e, t) {
	return e - e % t + t;
}
function w(e, t) {
	return --t, [
		[0, 0],
		[1 + (e >> 26), 1 + (t >> 26)],
		[9 + (e >> 23), 9 + (t >> 23)],
		[73 + (e >> 20), 73 + (t >> 20)],
		[585 + (e >> 17), 585 + (t >> 17)],
		[4681 + (e >> 14), 4681 + (t >> 14)]
	];
}
var T = class extends v {
	async lineCount(e, t) {
		return (await this.parse(t)).indices(e)?.stats?.lineCount || 0;
	}
	async _parse(e) {
		let t = await this.filehandle.readFile(), r = new DataView(t.buffer);
		if (r.getUint32(0, !0) !== x) throw Error("Not a BAI file");
		let i = r.getInt32(4, !0), o = ((1 << 18) - 1) / 7, s = 8, c, l = [];
		for (let e = 0; e < i; e++) {
			l.push(s);
			let e = r.getInt32(s, !0);
			s += 4;
			for (let t = 0; t < e; t += 1) {
				let e = r.getUint32(s, !0);
				if (s += 4, e === o + 1) s += 4, s += 32;
				else if (e > o + 1) throw Error("bai index contains too many bins, please use CSI");
				else {
					let e = r.getInt32(s, !0);
					s += 4;
					for (let t = 0; t < e; t++) s += 8, s += 8;
				}
			}
			let n = r.getInt32(s, !0);
			s += 4;
			let i = Array(n);
			for (let e = 0; e < n; e++) {
				let n = b(t, s);
				s += 8, c = f(c, n), i[e] = n;
			}
		}
		let u = new n({ maxSize: 5 });
		function p(e) {
			let n = l[e];
			if (n === void 0) return;
			let i = r.getInt32(n, !0), s;
			n += 4;
			let u = {};
			for (let e = 0; e < i; e += 1) {
				let e = r.getUint32(n, !0);
				if (n += 4, e === o + 1) n += 4, s = d(t, n + 16), n += 32;
				else if (e > o + 1) throw Error("bai index contains too many bins, please use CSI");
				else {
					let i = r.getInt32(n, !0);
					n += 4;
					let o = Array(i);
					for (let r = 0; r < i; r++) {
						let i = b(t, n);
						n += 8;
						let s = b(t, n);
						n += 8, c = f(c, i), o[r] = new a(i, s, e);
					}
					u[e] = o;
				}
			}
			let p = r.getInt32(n, !0);
			n += 4;
			let m = Array(p);
			for (let e = 0; e < p; e++) {
				let r = b(t, n);
				n += 8, c = f(c, r), m[e] = r;
			}
			return {
				binIndex: u,
				linearIndex: m,
				stats: s
			};
		}
		return {
			bai: !0,
			firstDataLine: c,
			maxBlockSize: 65536,
			indices: (e) => {
				if (!u.has(e)) {
					let t = p(e);
					return t && u.set(e, t), t;
				}
				return u.get(e);
			},
			refCount: i
		};
	}
	async indexCov(e, t, n, r) {
		let i = 16384, a = t !== void 0, o = (await this.parse(r)).indices(e);
		if (!o) return [];
		let { linearIndex: s, stats: c } = o;
		if (s.length === 0) return [];
		let l = n === void 0 ? (s.length - 1) * i : C(n, i), u = t === void 0 ? 0 : S(t, i), d = a ? Array((l - u) / i) : Array(s.length - 1), f = s[s.length - 1].blockPosition;
		if (l > (s.length - 1) * i) throw Error("query outside of range of linear index");
		let p = s[u / i].blockPosition;
		for (let e = u / i, t = 0; e < l / i; e++, t++) d[t] = {
			score: s[e + 1].blockPosition - p,
			start: e * i,
			end: e * i + i
		}, p = s[e + 1].blockPosition;
		return d.map((e) => ({
			...e,
			score: e.score * (c?.lineCount || 0) / f
		}));
	}
	async blocksForRange(e, t, n, r = {}) {
		t < 0 && (t = 0);
		let i = await this.parse(r);
		if (!i) return [];
		let a = i.indices(e);
		if (!a) return [];
		let o = w(t, n), s = [], { binIndex: c } = a;
		for (let [e, t] of o) for (let n = e; n <= t; n++) {
			let e = c[n];
			if (e) for (let t = 0, n = e.length; t < n; t++) s.push(e[t]);
		}
		let { linearIndex: l } = a, d = l.length, f = l[Math.min(t >> 14, d - 1)];
		return u(s, f);
	}
	async parse(e = {}) {
		return this.setupP ||= this._parse(e).catch((e) => {
			throw this.setupP = void 0, e;
		}), this.setupP;
	}
	async hasRefSeq(e, t = {}) {
		return !!(await this.parse(t)).indices(e)?.binIndex;
	}
}, E = [
	0,
	1996959894,
	3993919788,
	2567524794,
	124634137,
	1886057615,
	3915621685,
	2657392035,
	249268274,
	2044508324,
	3772115230,
	2547177864,
	162941995,
	2125561021,
	3887607047,
	2428444049,
	498536548,
	1789927666,
	4089016648,
	2227061214,
	450548861,
	1843258603,
	4107580753,
	2211677639,
	325883990,
	1684777152,
	4251122042,
	2321926636,
	335633487,
	1661365465,
	4195302755,
	2366115317,
	997073096,
	1281953886,
	3579855332,
	2724688242,
	1006888145,
	1258607687,
	3524101629,
	2768942443,
	901097722,
	1119000684,
	3686517206,
	2898065728,
	853044451,
	1172266101,
	3705015759,
	2882616665,
	651767980,
	1373503546,
	3369554304,
	3218104598,
	565507253,
	1454621731,
	3485111705,
	3099436303,
	671266974,
	1594198024,
	3322730930,
	2970347812,
	795835527,
	1483230225,
	3244367275,
	3060149565,
	1994146192,
	31158534,
	2563907772,
	4023717930,
	1907459465,
	112637215,
	2680153253,
	3904427059,
	2013776290,
	251722036,
	2517215374,
	3775830040,
	2137656763,
	141376813,
	2439277719,
	3865271297,
	1802195444,
	476864866,
	2238001368,
	4066508878,
	1812370925,
	453092731,
	2181625025,
	4111451223,
	1706088902,
	314042704,
	2344532202,
	4240017532,
	1658658271,
	366619977,
	2362670323,
	4224994405,
	1303535960,
	984961486,
	2747007092,
	3569037538,
	1256170817,
	1037604311,
	2765210733,
	3554079995,
	1131014506,
	879679996,
	2909243462,
	3663771856,
	1141124467,
	855842277,
	2852801631,
	3708648649,
	1342533948,
	654459306,
	3188396048,
	3373015174,
	1466479909,
	544179635,
	3110523913,
	3462522015,
	1591671054,
	702138776,
	2966460450,
	3352799412,
	1504918807,
	783551873,
	3082640443,
	3233442989,
	3988292384,
	2596254646,
	62317068,
	1957810842,
	3939845945,
	2647816111,
	81470997,
	1943803523,
	3814918930,
	2489596804,
	225274430,
	2053790376,
	3826175755,
	2466906013,
	167816743,
	2097651377,
	4027552580,
	2265490386,
	503444072,
	1762050814,
	4150417245,
	2154129355,
	426522225,
	1852507879,
	4275313526,
	2312317920,
	282753626,
	1742555852,
	4189708143,
	2394877945,
	397917763,
	1622183637,
	3604390888,
	2714866558,
	953729732,
	1340076626,
	3518719985,
	2797360999,
	1068828381,
	1219638859,
	3624741850,
	2936675148,
	906185462,
	1090812512,
	3747672003,
	2825379669,
	829329135,
	1181335161,
	3412177804,
	3160834842,
	628085408,
	1382605366,
	3423369109,
	3138078467,
	570562233,
	1426400815,
	3317316542,
	2998733608,
	733239954,
	1555261956,
	3268935591,
	3050360625,
	752459403,
	1541320221,
	2607071920,
	3965973030,
	1969922972,
	40735498,
	2617837225,
	3943577151,
	1913087877,
	83908371,
	2512341634,
	3803740692,
	2075208622,
	213261112,
	2463272603,
	3855990285,
	2094854071,
	198958881,
	2262029012,
	4057260610,
	1759359992,
	534414190,
	2176718541,
	4139329115,
	1873836001,
	414664567,
	2282248934,
	4279200368,
	1711684554,
	285281116,
	2405801727,
	4167216745,
	1634467795,
	376229701,
	2685067896,
	3608007406,
	1308918612,
	956543938,
	2808555105,
	3495958263,
	1231636301,
	1047427035,
	2932959818,
	3654703836,
	1088359270,
	936918e3,
	2847714899,
	3736837829,
	1202900863,
	817233897,
	3183342108,
	3401237130,
	1404277552,
	615818150,
	3134207493,
	3453421203,
	1423857449,
	601450431,
	3009837614,
	3294710456,
	1567103746,
	711928724,
	3020668471,
	3272380065,
	1510334235,
	755167117
];
typeof Int32Array < "u" && (E = new Int32Array(E));
var D = (e, t) => {
	let n = t === 0 ? 0 : ~~t ^ -1;
	for (let t = 0; t < e.length; t++) n = E[(n ^ e[t]) & 255] ^ n >>> 8;
	return n ^ -1;
}, O = 21582659, k = 38359875;
function A(e, t) {
	return e * 2 ** t;
}
function j(e, t) {
	return Math.floor(e / 2 ** t);
}
var M = class extends v {
	constructor() {
		super(...arguments), this.maxBinNumber = 0, this.depth = 0, this.minShift = 0;
	}
	async lineCount(e, t) {
		return (await this.parse(t)).indices(e)?.stats?.lineCount || 0;
	}
	async indexCov() {
		return [];
	}
	parseAuxData(e, t) {
		let n = new DataView(e.buffer), r = n.getUint32(t, !0), i = r & 65536 ? "zero-based-half-open" : "1-based-closed", a = {
			0: "generic",
			1: "SAM",
			2: "VCF"
		}[r & 15];
		if (!a) throw Error(`invalid Tabix preset format flags ${r}`);
		let o = {
			ref: n.getInt32(t + 4, !0),
			start: n.getInt32(t + 8, !0),
			end: n.getInt32(t + 12, !0)
		}, s = n.getInt32(t + 16, !0), c = s ? String.fromCharCode(s) : "", l = n.getInt32(t + 20, !0), u = n.getInt32(t + 24, !0);
		return {
			columnNumbers: o,
			coordinateType: i,
			metaValue: s,
			metaChar: c,
			skipLines: l,
			format: a,
			formatFlags: r,
			...p(e.subarray(t + 28, t + 28 + u), this.renameRefSeq)
		};
	}
	async _parse(t) {
		let r = await e(await this.filehandle.readFile(t)), i = new DataView(r.buffer), o, s = i.getUint32(0, !0);
		if (s === O) o = 1;
		else if (s === k) o = 2;
		else throw Error(`Not a CSI file ${s}`);
		this.minShift = i.getInt32(4, !0), this.depth = i.getInt32(8, !0), this.maxBinNumber = ((1 << (this.depth + 1) * 3) - 1) / 7;
		let c = this.maxBinNumber, l = i.getInt32(12, !0), u = l >= 30 ? this.parseAuxData(r, 16) : void 0, p = i.getInt32(16 + l, !0), m = 16 + l + 4, h, g = [];
		for (let e = 0; e < p; e++) {
			g.push(m);
			let e = i.getInt32(m, !0);
			m += 4;
			for (let t = 0; t < e; t++) {
				let e = i.getUint32(m, !0);
				if (m += 4, e > this.maxBinNumber) m += 44;
				else {
					m += 8;
					let e = i.getInt32(m, !0);
					m += 4;
					for (let t = 0; t < e; t += 1) {
						let e = b(r, m);
						m += 8, m += 8, h = f(h, e);
					}
				}
			}
		}
		let _ = new n({ maxSize: 5 });
		function v(e) {
			let t = g[e];
			if (t === void 0) return;
			let n = i.getInt32(t, !0);
			t += 4;
			let o = {}, s;
			for (let e = 0; e < n; e++) {
				let e = i.getUint32(t, !0);
				if (t += 4, e > c) s = d(r, t + 28), t += 44;
				else {
					h = f(h, b(r, t)), t += 8;
					let n = i.getInt32(t, !0);
					t += 4;
					let s = Array(n);
					for (let i = 0; i < n; i += 1) {
						let n = b(r, t);
						t += 8;
						let o = b(r, t);
						t += 8, s[i] = new a(n, o, e);
					}
					o[e] = s;
				}
			}
			return {
				binIndex: o,
				stats: s
			};
		}
		return {
			csiVersion: o,
			firstDataLine: h,
			indices: (e) => {
				if (!_.has(e)) {
					let t = v(e);
					return t && _.set(e, t), t;
				}
				return _.get(e);
			},
			refCount: p,
			csi: !0,
			maxBlockSize: 65536,
			...u
		};
	}
	async blocksForRange(e, t, n, r = {}) {
		t < 0 && (t = 0);
		let i = (await this.parse(r)).indices(e);
		if (!i) return [];
		let a = this.reg2bins(t, n);
		if (a.length === 0) return [];
		let o = [], { binIndex: s } = i;
		for (let [e, t] of a) for (let n = e; n <= t; n++) {
			let e = s[n];
			if (e) for (let t = 0, n = e.length; t < n; t++) o.push(e[t]);
		}
		return u(o, new y(0, 0));
	}
	reg2bins(e, t) {
		--e, e < 1 && (e = 1), t > 2 ** 50 && (t = 2 ** 34), --t;
		let n = 0, r = 0, i = this.minShift + this.depth * 3, a = [];
		for (; n <= this.depth; i -= 3, r += A(1, n * 3), n += 1) {
			let n = r + j(e, i), o = r + j(t, i);
			if (o - n + a.length > this.maxBinNumber) throw Error(`query ${e}-${t} is too large for current binning scheme (shift ${this.minShift}, depth ${this.depth}), try a smaller query or a coarser index binning scheme`);
			a.push([n, o]);
		}
		return a;
	}
	async parse(e = {}) {
		return this.setupP ||= this._parse(e).catch((e) => {
			throw this.setupP = void 0, e;
		}), this.setupP;
	}
	async hasRefSeq(e, t = {}) {
		return !!(await this.parse(t)).indices(e)?.binIndex;
	}
}, N = class {
	read() {
		throw Error("never called");
	}
	stat() {
		throw Error("never called");
	}
	readFile() {
		throw Error("never called");
	}
	close() {
		throw Error("never called");
	}
}, P = {
	BAM_FPAIRED: 1,
	BAM_FPROPER_PAIR: 2,
	BAM_FUNMAP: 4,
	BAM_FMUNMAP: 8,
	BAM_FREVERSE: 16,
	BAM_FMREVERSE: 32,
	BAM_FREAD1: 64,
	BAM_FREAD2: 128,
	BAM_FSECONDARY: 256,
	BAM_FQCFAIL: 512,
	BAM_FDUP: 1024,
	BAM_FSUPPLEMENTARY: 2048
}, F = "=ACMGRSVTWYHKDBN".split(""), I = /* @__PURE__ */ "F F .F R .R F .R R .F2F1.F2R1.R2F1.R2R1.F1F2.F1R2.R1F2.R1R2.F2F1.F2R1.R2F1.R2R1.F F .R F .F R .R R .F1F2.R1F2.F1R2.R1R2.F2F1.R2F1.F2R1.R2R1.F1F2.R1F2.F1R2.R1R2".split("."), L = [
	77,
	73,
	68,
	78,
	83,
	72,
	80,
	61,
	88,
	63,
	63,
	63,
	63,
	63,
	63,
	63
], R = 461, z = class {
	constructor(e) {
		this.bytes = e.bytes, this.fileOffset = e.fileOffset, this._dataView = new DataView(this.bytes.byteArray.buffer);
	}
	get byteArray() {
		return this.bytes.byteArray;
	}
	get flags() {
		return this._cachedFlags === void 0 && (this._cachedFlags = (this._dataView.getInt32(this.bytes.start + 16, !0) & 4294901760) >> 16), this._cachedFlags;
	}
	get ref_id() {
		return this._cachedRefId === void 0 && (this._cachedRefId = this._dataView.getInt32(this.bytes.start + 4, !0)), this._cachedRefId;
	}
	get start() {
		return this._cachedStart === void 0 && (this._cachedStart = this._dataView.getInt32(this.bytes.start + 8, !0)), this._cachedStart;
	}
	get end() {
		return this._cachedEnd === void 0 && (this._cachedEnd = this.start + this.length_on_ref), this._cachedEnd;
	}
	get mq() {
		let e = (this.bin_mq_nl & 65280) >> 8;
		return e === 255 ? void 0 : e;
	}
	get score() {
		return this.mq;
	}
	get qual() {
		if (this.isSegmentUnmapped()) return null;
		{
			let e = this.b0 + this.read_name_length + this.num_cigar_bytes + this.num_seq_bytes;
			return this.byteArray.subarray(e, e + this.seq_length);
		}
	}
	get strand() {
		return this.isReverseComplemented() ? -1 : 1;
	}
	get b0() {
		return this.bytes.start + 36;
	}
	get tagsStart() {
		return this._cachedTagsStart === void 0 && (this._cachedTagsStart = this.b0 + this.read_name_length + this.num_cigar_bytes + this.num_seq_bytes + this.seq_length), this._cachedTagsStart;
	}
	get name() {
		let e = this.read_name_length - 1, t = this.b0, n = this.byteArray, r = Array(e);
		for (let i = 0; i < e; i++) r[i] = n[t + i];
		return String.fromCharCode(...r);
	}
	get NUMERIC_MD() {
		if (this._cachedNUMERIC_MD === void 0) {
			let e = this.getTagRaw("MD");
			this._cachedNUMERIC_MD = e instanceof Uint8Array ? e : null;
		}
		return this._cachedNUMERIC_MD === null ? void 0 : this._cachedNUMERIC_MD;
	}
	get tags() {
		return this._cachedTags === void 0 && (this._cachedTags = this._computeTags()), this._cachedTags;
	}
	getTag(e) {
		return this._cachedTags === void 0 ? this._findTag(e, !1) : this._cachedTags[e];
	}
	getTagRaw(e) {
		return this._findTag(e, !0);
	}
	_findTag(e, t) {
		let n = e.charCodeAt(0), r = e.charCodeAt(1), i = this.tagsStart, a = this.bytes.end, o = this.byteArray;
		for (; i < a;) {
			let e = o[i], s = o[i + 1], c = o[i + 2];
			i += 3;
			let l = e === n && s === r;
			switch (c) {
				case 65:
					if (l) return String.fromCharCode(o[i]);
					i += 1;
					break;
				case 105:
					if (l) return this._dataView.getInt32(i, !0);
					i += 4;
					break;
				case 73:
					if (l) return this._dataView.getUint32(i, !0);
					i += 4;
					break;
				case 99:
					if (l) return this._dataView.getInt8(i);
					i += 1;
					break;
				case 67:
					if (l) return this._dataView.getUint8(i);
					i += 1;
					break;
				case 115:
					if (l) return this._dataView.getInt16(i, !0);
					i += 2;
					break;
				case 83:
					if (l) return this._dataView.getUint16(i, !0);
					i += 2;
					break;
				case 102:
					if (l) return this._dataView.getFloat32(i, !0);
					i += 4;
					break;
				case 90:
				case 72:
					if (l) {
						let e = i;
						for (; i < a && o[i] !== 0;) i++;
						if (t) return o.subarray(e, i);
						let n = [];
						for (let t = e; t < i; t++) n.push(String.fromCharCode(o[t]));
						return n.join("");
					}
					for (; i <= a && o[i++] !== 0;);
					break;
				case 66: {
					let e = o[i++], t = this._dataView.getInt32(i, !0);
					i += 4;
					let n = o.byteOffset + i;
					if (l) {
						if (e === 105) {
							if (n % 4 == 0) return new Int32Array(o.buffer, n, t);
							let e = Array(t);
							for (let n = 0; n < t; n++) e[n] = this._dataView.getInt32(i + n * 4, !0);
							return e;
						} else if (e === 73) {
							if (n % 4 == 0) return new Uint32Array(o.buffer, n, t);
							let e = Array(t);
							for (let n = 0; n < t; n++) e[n] = this._dataView.getUint32(i + n * 4, !0);
							return e;
						} else if (e === 115) {
							if (n % 2 == 0) return new Int16Array(o.buffer, n, t);
							let e = Array(t);
							for (let n = 0; n < t; n++) e[n] = this._dataView.getInt16(i + n * 2, !0);
							return e;
						} else if (e === 83) {
							if (n % 2 == 0) return new Uint16Array(o.buffer, n, t);
							let e = Array(t);
							for (let n = 0; n < t; n++) e[n] = this._dataView.getUint16(i + n * 2, !0);
							return e;
						} else if (e === 99) return new Int8Array(o.buffer, n, t);
						else if (e === 67) return new Uint8Array(o.buffer, n, t);
						else if (e === 102) {
							if (n % 4 == 0) return new Float32Array(o.buffer, n, t);
							let e = Array(t);
							for (let n = 0; n < t; n++) e[n] = this._dataView.getFloat32(i + n * 4, !0);
							return e;
						}
					}
					e === 105 || e === 73 || e === 102 ? i += t << 2 : e === 115 || e === 83 ? i += t << 1 : (e === 99 || e === 67) && (i += t);
					break;
				}
			}
		}
	}
	_computeTags() {
		let e = this.tagsStart, t = this.bytes.end, n = this.byteArray, r = {};
		for (; e < t;) {
			let i = String.fromCharCode(n[e], n[e + 1]), a = n[e + 2];
			switch (e += 3, a) {
				case 65:
					r[i] = String.fromCharCode(n[e]), e += 1;
					break;
				case 105:
					r[i] = this._dataView.getInt32(e, !0), e += 4;
					break;
				case 73:
					r[i] = this._dataView.getUint32(e, !0), e += 4;
					break;
				case 99:
					r[i] = this._dataView.getInt8(e), e += 1;
					break;
				case 67:
					r[i] = this._dataView.getUint8(e), e += 1;
					break;
				case 115:
					r[i] = this._dataView.getInt16(e, !0), e += 2;
					break;
				case 83:
					r[i] = this._dataView.getUint16(e, !0), e += 2;
					break;
				case 102:
					r[i] = this._dataView.getFloat32(e, !0), e += 4;
					break;
				case 90:
				case 72: {
					let a = [];
					for (; e <= t;) {
						let t = n[e++];
						if (t !== 0) a.push(String.fromCharCode(t));
						else break;
					}
					r[i] = a.join("");
					break;
				}
				case 66: {
					let t = n[e++], a = this._dataView.getInt32(e, !0);
					e += 4;
					let o = n.byteOffset + e;
					if (t === 105) {
						if (o % 4 == 0) r[i] = new Int32Array(n.buffer, o, a);
						else {
							let t = Array(a);
							for (let n = 0; n < a; n++) t[n] = this._dataView.getInt32(e + n * 4, !0);
							r[i] = t;
						}
						e += a << 2;
					} else if (t === 73) {
						if (o % 4 == 0) r[i] = new Uint32Array(n.buffer, o, a);
						else {
							let t = Array(a);
							for (let n = 0; n < a; n++) t[n] = this._dataView.getUint32(e + n * 4, !0);
							r[i] = t;
						}
						e += a << 2;
					} else if (t === 115) {
						if (o % 2 == 0) r[i] = new Int16Array(n.buffer, o, a);
						else {
							let t = Array(a);
							for (let n = 0; n < a; n++) t[n] = this._dataView.getInt16(e + n * 2, !0);
							r[i] = t;
						}
						e += a << 1;
					} else if (t === 83) {
						if (o % 2 == 0) r[i] = new Uint16Array(n.buffer, o, a);
						else {
							let t = Array(a);
							for (let n = 0; n < a; n++) t[n] = this._dataView.getUint16(e + n * 2, !0);
							r[i] = t;
						}
						e += a << 1;
					} else if (t === 99) r[i] = new Int8Array(n.buffer, o, a), e += a;
					else if (t === 67) r[i] = new Uint8Array(n.buffer, o, a), e += a;
					else if (t === 102) {
						if (o % 4 == 0) r[i] = new Float32Array(n.buffer, o, a);
						else {
							let t = Array(a);
							for (let n = 0; n < a; n++) t[n] = this._dataView.getFloat32(e + n * 4, !0);
							r[i] = t;
						}
						e += a << 2;
					}
					break;
				}
				default:
					console.error("Unknown BAM tag type", a);
					break;
			}
		}
		return r;
	}
	isPaired() {
		return !!(this.flags & P.BAM_FPAIRED);
	}
	isProperlyPaired() {
		return !!(this.flags & P.BAM_FPROPER_PAIR);
	}
	isSegmentUnmapped() {
		return !!(this.flags & P.BAM_FUNMAP);
	}
	isMateUnmapped() {
		return !!(this.flags & P.BAM_FMUNMAP);
	}
	isReverseComplemented() {
		return !!(this.flags & P.BAM_FREVERSE);
	}
	isMateReverseComplemented() {
		return !!(this.flags & P.BAM_FMREVERSE);
	}
	isRead1() {
		return !!(this.flags & P.BAM_FREAD1);
	}
	isRead2() {
		return !!(this.flags & P.BAM_FREAD2);
	}
	isSecondary() {
		return !!(this.flags & P.BAM_FSECONDARY);
	}
	isFailedQc() {
		return !!(this.flags & P.BAM_FQCFAIL);
	}
	isDuplicate() {
		return !!(this.flags & P.BAM_FDUP);
	}
	isSupplementary() {
		return !!(this.flags & P.BAM_FSUPPLEMENTARY);
	}
	get cigarAndLength() {
		return this._cachedCigarAndLength === void 0 && (this._cachedCigarAndLength = this._computeCigarAndLength()), this._cachedCigarAndLength;
	}
	_computeCigarAndLength() {
		if (this.isSegmentUnmapped()) return {
			length_on_ref: 0,
			NUMERIC_CIGAR: new Uint32Array()
		};
		let e = this.num_cigar_ops, t = this.b0 + this.read_name_length, n = this._dataView.getInt32(t, !0), r = n >> 4;
		if ((n & 15) == 4 && r === this.seq_length) {
			t += 4;
			let e = this._dataView.getInt32(t, !0), n = e >> 4;
			return (e & 15) != 3 && console.warn("CG tag with no N tag"), {
				NUMERIC_CIGAR: this.tags.CG,
				length_on_ref: n
			};
		}
		let i = this.byteArray.byteOffset + t;
		if (i % 4 == 0 && e > 50) {
			let t = new Uint32Array(this.byteArray.buffer, i, e), n = 0;
			for (let r = 0; r < e; ++r) {
				let e = t[r];
				n += (e >> 4) * (R >> (e & 15) & 1);
			}
			return {
				NUMERIC_CIGAR: t,
				length_on_ref: n
			};
		}
		let a = Array(e), o = 0;
		for (let n = 0; n < e; ++n) {
			let e = this._dataView.getInt32(t + n * 4, !0) | 0;
			a[n] = e, o += (e >> 4) * (R >> (e & 15) & 1);
		}
		return {
			NUMERIC_CIGAR: a,
			length_on_ref: o
		};
	}
	get length_on_ref() {
		return this.cigarAndLength.length_on_ref;
	}
	get NUMERIC_CIGAR() {
		return this.cigarAndLength.NUMERIC_CIGAR;
	}
	get CIGAR() {
		let e = this.NUMERIC_CIGAR, t = "";
		for (let n = 0, r = e.length; n < r; n++) {
			let r = e[n], i = r >> 4, a = L[r & 15];
			t += i + String.fromCharCode(a);
		}
		return t;
	}
	get num_cigar_ops() {
		return this.flag_nc & 65535;
	}
	get num_cigar_bytes() {
		return this.num_cigar_ops << 2;
	}
	get read_name_length() {
		return this.bin_mq_nl & 255;
	}
	get num_seq_bytes() {
		return this.seq_length + 1 >> 1;
	}
	get NUMERIC_SEQ() {
		let e = this.b0 + this.read_name_length + this.num_cigar_bytes;
		return this.byteArray.subarray(e, e + this.num_seq_bytes);
	}
	get seq() {
		let e = this.NUMERIC_SEQ, t = this.seq_length, n = Array(t), r = 0, i = t >> 1;
		for (let t = 0; t < i; ++t) {
			let i = e[t];
			n[r++] = F[(i & 240) >> 4], n[r++] = F[i & 15];
		}
		return r < t && (n[r] = F[(e[i] & 240) >> 4]), n.join("");
	}
	get pair_orientation() {
		let e = this.flags;
		if (!(e & 12 || this.ref_id !== this.next_refid)) return I[e >> 4 & 15 | (this.template_length > 0 ? 16 : 0)];
	}
	get bin_mq_nl() {
		return this._dataView.getInt32(this.bytes.start + 12, !0);
	}
	get flag_nc() {
		return this._dataView.getInt32(this.bytes.start + 16, !0);
	}
	get seq_length() {
		return this._dataView.getInt32(this.bytes.start + 20, !0);
	}
	get next_refid() {
		return this._dataView.getInt32(this.bytes.start + 24, !0);
	}
	get next_pos() {
		return this._dataView.getInt32(this.bytes.start + 28, !0);
	}
	get template_length() {
		return this._dataView.getInt32(this.bytes.start + 32, !0);
	}
	seqAt(e) {
		if (e < this.seq_length) {
			let t = e >> 1, n = this.byteArray[this.b0 + this.read_name_length + this.num_cigar_bytes + t];
			return e % 2 == 0 ? F[(n & 240) >> 4] : F[n & 15];
		} else return;
	}
	toJSON() {
		let e = {};
		for (let t of Object.keys(this)) t.startsWith("_") || t === "bytes" || (e[t] = this[t]);
		return e;
	}
};
//#endregion
//#region ../../node_modules/@gmod/bam/esm/sam.js
function B(e) {
	let t = e.split(/\r?\n/), n = [];
	for (let e of t) {
		let [t, ...r] = e.split(/\t/);
		t && n.push({
			tag: t.slice(1),
			data: r.map((e) => {
				let t = e.indexOf(":");
				return {
					tag: e.slice(0, t),
					value: e.slice(t + 1)
				};
			})
		});
	}
	return n;
}
var V = 65536, H = class {
	constructor({ bamFilehandle: e, bamPath: t, bamUrl: a, baiPath: o, baiFilehandle: s, baiUrl: c, csiPath: l, csiFilehandle: u, csiUrl: d, htsget: f, renameRefSeqs: p = (e) => e, recordClass: m }) {
		if (this.htsget = !1, this.chunkFeatureCache = new n({ maxSize: 100 }), this.renameRefSeq = p, this.RecordClass = m ?? z, e) this.bam = e;
		else if (t) this.bam = new i(t);
		else if (a) this.bam = new r(a);
		else if (f) this.htsget = !0, this.bam = new N();
		else throw Error("unable to initialize bam");
		if (u) this.index = new M({ filehandle: u });
		else if (l) this.index = new M({ filehandle: new i(l) });
		else if (d) this.index = new M({ filehandle: new r(d) });
		else if (s) this.index = new T({ filehandle: s });
		else if (o) this.index = new T({ filehandle: new i(o) });
		else if (c) this.index = new T({ filehandle: new r(c) });
		else if (t) this.index = new T({ filehandle: new i(`${t}.bai`) });
		else if (a) this.index = new T({ filehandle: new r(`${a}.bai`) });
		else if (f) this.htsget = !0;
		else throw Error("unable to infer index format");
	}
	async getHeaderPre(t) {
		let n = l(t);
		if (!this.index) return;
		let r = await this.index.parse(n), i = r.firstDataLine === void 0 ? void 0 : r.firstDataLine.blockPosition + V, a = await e(i === void 0 ? await this.bam.readFile() : await this.bam.read(i, 0)), o = new DataView(a.buffer);
		if (o.getInt32(0, !0) !== 21840194) throw Error("Not a BAM file");
		let s = o.getInt32(4, !0);
		this.header = new TextDecoder("utf8").decode(a.subarray(8, 8 + s));
		let c = s + 8;
		for (let t = 0; t < 5; t++) {
			if (this._hasEnoughRefSeqData(a, c)) {
				let { chrToIndex: e, indexToChr: t } = this._parseRefSeqs(a, c);
				return this.chrToIndex = e, this.indexToChr = t, B(this.header);
			}
			if (i === void 0) throw Error(`Insufficient data for reference sequences in ${a.length} bytes`);
			i *= 2, a = await e(await this.bam.read(i, 0));
		}
		throw Error("Insufficient data for reference sequences after 5 retries");
	}
	getHeader(e) {
		return this.headerP ||= this.getHeaderPre(e).catch((e) => {
			throw this.headerP = void 0, e;
		}), this.headerP;
	}
	async getHeaderText(e = {}) {
		return await this.getHeader(e), this.header;
	}
	_hasEnoughRefSeqData(e, t) {
		if (t + 4 > e.length) return !1;
		let n = new DataView(e.buffer), r = n.getInt32(t, !0), i = t + 4;
		for (let t = 0; t < r; t += 1) {
			if (i + 8 > e.length) return !1;
			let t = n.getInt32(i, !0);
			i = i + 8 + t;
		}
		return !0;
	}
	_parseRefSeqs(e, t) {
		let n = new DataView(e.buffer), r = n.getInt32(t, !0), i = t + 4, a = {}, o = [], s = new TextDecoder("utf8");
		for (let t = 0; t < r; t += 1) {
			let r = n.getInt32(i, !0), c = this.renameRefSeq(s.decode(e.subarray(i + 4, i + 4 + r - 1))), l = n.getInt32(i + r + 4, !0);
			a[c] = t, o.push({
				refName: c,
				length: l
			}), i = i + 8 + r;
		}
		return {
			chrToIndex: a,
			indexToChr: o
		};
	}
	async getRecordsForRange(e, t, n, r) {
		await this.getHeader(r);
		let i = this.chrToIndex?.[e];
		if (i === void 0 || !this.index) return [];
		let a = await this.index.blocksForRange(i, t - 1, n, r);
		return this._fetchChunkFeaturesDirect(a, i, t, n, r);
	}
	chunkCacheKey(e, t) {
		let { minv: n, maxv: r } = e;
		return `${n.blockPosition}:${n.dataPosition}-${r.blockPosition}:${r.dataPosition}${_(t)}`;
	}
	blocksOverlap(e, t, n, r) {
		return e <= r && t >= n;
	}
	evictOverlappingChunks(e, t) {
		for (let [n, r] of this.chunkFeatureCache) this.blocksOverlap(e, t, r.minBlock, r.maxBlock) && this.chunkFeatureCache.delete(n);
	}
	async _fetchChunkFeaturesDirect(e, t, n, r, i = {}) {
		let { viewAsPairs: a, filterBy: o } = i, { flagInclude: s = 0, flagExclude: c = 0, tagFilter: l } = o || {}, u = [];
		for (let a = 0, d = e.length; a < d; a++) {
			let d = e[a], f = this.chunkCacheKey(d, o), p = d.minv.blockPosition, m = d.maxv.blockPosition, _, v = this.chunkFeatureCache.get(f);
			if (v) _ = v.features;
			else {
				this.evictOverlappingChunks(p, m);
				let { data: e, cpositions: t, dpositions: n } = await this._readChunk({
					chunk: d,
					opts: i
				}), r = await this.readBamFeatures(e, t, n, d);
				if (o) {
					_ = [];
					for (let e = 0, t = r.length; e < t; e++) {
						let t = r[e];
						h(t.flags, s, c) || l && g(t.tags[l.tag], l.value) || _.push(t);
					}
				} else _ = r;
				this.chunkFeatureCache.set(f, {
					minBlock: p,
					maxBlock: m,
					features: _
				});
			}
			let y = !1;
			for (let e = 0, i = _.length; e < i; e++) {
				let i = _[e];
				if (i.ref_id === t) if (i.start >= r) {
					y = !0;
					break;
				} else i.end >= n && u.push(i);
			}
			if (y) break;
		}
		if (a) {
			let e = await this.fetchPairs(t, u, i);
			for (let t = 0, n = e.length; t < n; t++) u.push(e[t]);
		}
		return u;
	}
	async fetchPairs(e, t, n) {
		let { pairAcrossChr: r, maxInsertSize: i = 2e5 } = n, a = {}, o = {};
		for (let e = 0, n = t.length; e < n; e++) {
			let n = t[e], r = n.name;
			a[r] = (a[r] || 0) + 1, o[n.fileOffset] = 1;
		}
		let s = [];
		for (let o = 0, c = t.length; o < c; o++) {
			let c = t[o], l = c.name;
			this.index && a[l] === 1 && (r || c.next_refid === e && Math.abs(c.start - c.next_pos) < i) && s.push(this.index.blocksForRange(c.next_refid, c.next_pos, c.next_pos + 1, n));
		}
		let c = /* @__PURE__ */ new Map(), l = await Promise.all(s);
		for (let e = 0, t = l.length; e < t; e++) {
			let t = l[e];
			for (let e = 0, n = t.length; e < n; e++) {
				let n = t[e], r = n.toString();
				c.has(r) || c.set(r, n);
			}
		}
		return (await Promise.all([...c.values()].map(async (e) => {
			let { data: t, cpositions: r, dpositions: i, chunk: s } = await this._readChunk({
				chunk: e,
				opts: n
			}), c = [], l = await this.readBamFeatures(t, r, i, s);
			for (let e = 0, t = l.length; e < t; e++) {
				let t = l[e];
				a[t.name] === 1 && !o[t.fileOffset] && c.push(t);
			}
			return c;
		}))).flat();
	}
	async _readChunk({ chunk: e, opts: n }) {
		let { buffer: r, cpositions: i, dpositions: a } = await t(await this.bam.read(e.fetchedSize(), e.minv.blockPosition, n), e);
		return {
			data: r,
			cpositions: i,
			dpositions: a,
			chunk: e
		};
	}
	async readBamFeatures(e, t, n, r) {
		let i = 0, a = [], o = 0, s = new DataView(e.buffer), c = n.length > 0, l = t.length > 0;
		for (; i + 4 < e.length;) {
			let u = s.getInt32(i, !0), d = i + 4 + u - 1;
			if (c) {
				for (; i + r.minv.dataPosition >= n[o++];);
				o--;
			}
			if (d < e.length) {
				let s = new this.RecordClass({
					bytes: {
						byteArray: e,
						start: i,
						end: d
					},
					fileOffset: l ? t[o] * 256 + (i - n[o]) + r.minv.dataPosition + 1 : D(e.subarray(i, d)) >>> 0
				});
				a.push(s);
			}
			i = d + 1;
		}
		return a;
	}
	async hasRefSeq(e) {
		let t = this.chrToIndex?.[e];
		return t === void 0 ? !1 : this.index?.hasRefSeq(t);
	}
	async lineCount(e) {
		let t = this.chrToIndex?.[e];
		return t === void 0 || !this.index ? 0 : this.index.lineCount(t);
	}
	async indexCov(e, t, n) {
		if (!this.index) return [];
		await this.index.parse();
		let r = this.chrToIndex?.[e];
		return r === void 0 ? [] : this.index.indexCov(r, t, n);
	}
	async blocksForRange(e, t, n, r) {
		if (!this.index) return [];
		await this.index.parse();
		let i = this.chrToIndex?.[e];
		return i === void 0 ? [] : this.index.blocksForRange(i, t, n, r);
	}
	clearFeatureCache() {
		this.chunkFeatureCache.clear();
	}
	async estimatedBytesForRegions(e, t) {
		if (!this.index) return 0;
		if (await this.getHeader(t), !this.chrToIndex) throw Error("Header not yet parsed");
		let n = e.flatMap((e) => {
			let t = this.chrToIndex[e.refName];
			return t === void 0 ? [] : [{
				refId: t,
				start: e.start,
				end: e.end
			}];
		});
		return this.index.estimatedBytesForRegions(n, t);
	}
};
//#endregion
//#region ../../node_modules/@gmod/bam/esm/htsget.js
async function U(t, n) {
	let r = await Promise.all(t.map(async (e) => {
		let { url: t, headers: r } = e;
		if (t.startsWith("data:")) {
			let e = await fetch(t);
			if (!e.ok) throw Error("failed to decode base64");
			let n = await e.arrayBuffer();
			return new Uint8Array(n);
		} else {
			let { referer: e, ...i } = r, a = await fetch(t, {
				...n,
				headers: {
					...n?.headers,
					...i
				}
			});
			if (!a.ok) throw Error(`HTTP ${a.status} fetching ${t}: ${await a.text()}`);
			return new Uint8Array(await a.arrayBuffer());
		}
	}));
	return m(await Promise.all(r.map((t) => e(t))));
}
var W = class extends H {
	constructor(e) {
		super({
			htsget: !0,
			recordClass: e.recordClass
		}), this.baseUrl = e.baseUrl, this.trackId = e.trackId;
	}
	async getRecordsForRange(e, t, n, r) {
		let i = `${`${this.baseUrl}/${this.trackId}`}?referenceName=${e}&start=${t}&end=${n}&format=BAM`, a = this.chrToIndex?.[e];
		if (a === void 0) return [];
		let o = await fetch(i, { ...r });
		if (!o.ok) throw Error(`HTTP ${o.status} fetching ${i}: ${await o.text()}`);
		let s = await U((await o.json()).htsget.urls.slice(1), r), c = await this.readBamFeatures(s, [], [], {
			minv: {
				dataPosition: 0,
				blockPosition: 0
			},
			maxv: {
				dataPosition: 0,
				blockPosition: 0
			}
		}), l = [];
		for (let e = 0, r = c.length; e < r; e++) {
			let r = c[e];
			if (r.ref_id === a) {
				if (r.start >= n) break;
				r.end >= t && l.push(r);
			}
		}
		return l;
	}
	async getHeader(e = {}) {
		let t = `${this.baseUrl}/${this.trackId}?referenceName=na&class=header`, n = await fetch(t, e);
		if (!n.ok) throw Error(`HTTP ${n.status} fetching ${t}: ${await n.text()}`);
		let r = await U((await n.json()).htsget.urls, e), i = new DataView(r.buffer);
		if (i.getInt32(0, !0) !== 21840194) throw Error("Not a BAM file");
		let a = i.getInt32(4, !0), o = B(new TextDecoder("utf8").decode(r.subarray(8, 8 + a))), s = [], c = {}, l = o.filter((e) => e.tag === "SQ");
		for (let [e, t] of l.entries()) {
			let n = "", r = 0;
			for (let e of t.data) e.tag === "SN" ? n = e.value : e.tag === "LN" && (r = +e.value);
			c[n] = e, s[e] = {
				refName: n,
				length: r
			};
		}
		return this.chrToIndex = c, this.indexToChr = s, o;
	}
};
//#endregion
export { T as BAI, H as BamFile, z as BamRecord, M as CSI, W as HtsgetFile };
