import { t as e } from "./esm-NIYEaYkc.js";
import { t } from "./browser-KWU9rWZT.js";
//#region node_modules/@gmod/indexedfasta/esm/indexedFasta.js
function n(e, t, n, r) {
	return e + t * Math.floor(r / n) + r % n;
}
async function r(e, t = {}) {
	let n = new TextDecoder("utf8").decode(await e.readFile(t)), r = [], i = [], a = [], o = [], s = [], c = {}, l = 0, u = n.length, d = 0;
	for (; l < u;) {
		let e = n.indexOf("\n", l);
		e === -1 && (e = u);
		let t = n.slice(l, e);
		if (t.endsWith("\r") && (t = t.slice(0, -1)), t = t.trim(), l = e + 1, t.length === 0) continue;
		let f = t.indexOf("	"), p = t.indexOf("	", f + 1), m = t.indexOf("	", p + 1), h = t.indexOf("	", m + 1), g = t.slice(0, f);
		if (g.startsWith(">")) throw Error("found > in sequence name, might have supplied FASTA file for the FASTA index");
		r.push(g), a.push(+t.slice(f + 1, p)), i.push(+t.slice(p + 1, m)), o.push(+t.slice(m + 1, h)), s.push(+t.slice(h + 1)), c[g] = d, d++;
	}
	return {
		names: r,
		nameToIndex: c,
		offsets: i,
		lengths: a,
		lineLengths: o,
		lineBytes: s
	};
}
var i = class {
	constructor({ fasta: e, fai: n, path: r, faiPath: i }) {
		if (e) this.fasta = e;
		else if (r) this.fasta = new t(r);
		else throw Error("Need to pass filehandle for fasta or path to localfile");
		if (n) this.fai = n;
		else if (i) this.fai = new t(i);
		else if (r) this.fai = new t(`${r}.fai`);
		else throw Error("Need to pass filehandle for  or path to localfile");
	}
	async _getIndexes(e) {
		return this.indexes ||= r(this.fai, e).catch((e) => {
			throw this.indexes = void 0, e;
		}), this.indexes;
	}
	async getSequenceNames(e) {
		return (await this._getIndexes(e)).names;
	}
	async getSequenceSizes(e) {
		let t = await this._getIndexes(e);
		if (!t.sizesCache) {
			let e = {};
			for (let n = 0; n < t.names.length; n++) e[t.names[n]] = t.lengths[n];
			t.sizesCache = e;
		}
		return t.sizesCache;
	}
	async getSequenceSize(e, t) {
		let n = await this._getIndexes(t), r = n.nameToIndex[e];
		return r === void 0 ? void 0 : n.lengths[r];
	}
	async hasReferenceSequence(e, t) {
		return (await this._getIndexes(t)).nameToIndex[e] !== void 0;
	}
	async getResiduesByName(e, t, n, r) {
		let i = await this._getIndexes(r), a = i.nameToIndex[e];
		if (a !== void 0) return this._fetchFromIndex(i.offsets[a], i.lineBytes[a], i.lineLengths[a], i.lengths[a], t, n, r);
	}
	async getSequence(e, t, n, r) {
		return this.getResiduesByName(e, t, n, r);
	}
	async _fetchFromIndex(e, t, r, i, a = 0, o, s) {
		let c = o;
		if (a < 0) throw TypeError("regionStart cannot be less than 0");
		if ((c === void 0 || c > i) && (c = i), a >= c) return "";
		let l = n(e, t, r, a), u = n(e, t, r, c) - l, d = new TextDecoder("utf8").decode(await this.fasta.read(u, l, s)).replace(/\s+/g, "");
		if (/[^\x20-\x7e]/.test(d.slice(0, 1e3))) throw Error("Non-ASCII characters detected in sequence. The file may be gzip compressed. Use BgzipIndexedFasta for bgzip files, or decompress the file.");
		return d;
	}
}, a = class extends i {
	constructor({ fasta: n, path: r, fai: i, faiPath: a, gzi: o, gziPath: s }) {
		super({
			fasta: n,
			path: r,
			fai: i,
			faiPath: a
		}), n && o ? this.fasta = new e({
			filehandle: n,
			gziFilehandle: o
		}) : r && s && (this.fasta = new e({
			filehandle: new t(r),
			gziFilehandle: new t(s)
		}));
	}
};
//#endregion
//#region node_modules/@gmod/indexedfasta/esm/index.js
function o(e) {
	return e.split(">").filter((e) => /\S/.test(e)).map((e) => {
		let [t, ...n] = e.split("\n"), [r, ...i] = t.split(" "), a = n.join("").replace(/\s/g, "");
		return {
			id: r,
			description: i.join(" "),
			sequence: a
		};
	});
}
var s = class {
	constructor({ fasta: e, path: n }) {
		if (e) this.fasta = e;
		else if (n) this.fasta = new t(n);
		else throw Error("Need to pass fasta or path");
		this.data = this.fasta.readFile().then((e) => o(new TextDecoder("utf8").decode(e)));
	}
	async fetch(e, t, n) {
		let r = (await this.data).find((t) => t.id === e), i = n - t;
		if (!r) throw Error(`no sequence with id ${e} exists`);
		return r.sequence.slice(t, i);
	}
	async getSequenceNames() {
		return (await this.data).map((e) => e.id);
	}
};
//#endregion
export { a as BgzipIndexedFasta, s as FetchableSmallFasta, i as IndexedFasta, o as parseSmallFasta };
