import { NEWLINE_REGEXP as e, escapeRegExp as t } from "../utils/regexp-utils.js";
import { Position as n, Range as r, init_main as i } from "../../../vscode-languageserver-types/lib/esm/main.js";
import { URI as a } from "../../../vscode-uri/lib/esm/index.js";
//#region ../../node_modules/langium/lib/documentation/jsdoc.js
i();
function o(e, t, r) {
	let i, a;
	typeof e == "string" ? (a = t, i = r) : (a = e.range.start, i = t), a ||= n.create(0, 0);
	let o = c(e), s = w(i);
	return _({
		index: 0,
		tokens: d({
			lines: o,
			position: a,
			options: s
		}),
		position: a
	});
}
function s(e, t) {
	let n = w(t), r = c(e);
	if (r.length === 0) return !1;
	let i = r[0], a = r[r.length - 1], o = n.start, s = n.end;
	return !!o?.exec(i) && !!s?.exec(a);
}
function c(t) {
	let n = "";
	return n = typeof t == "string" ? t : t.text, n.split(e);
}
var l = /\s*(@([\p{L}][\p{L}\p{N}]*)?)/uy, u = /\{(@[\p{L}][\p{L}\p{N}]*)(\s*)([^\r\n}]+)?\}/gu;
function d(e) {
	let t = [], i = e.position.line, a = e.position.character;
	for (let o = 0; o < e.lines.length; o++) {
		let s = o === 0, c = o === e.lines.length - 1, d = e.lines[o], p = 0;
		if (s && e.options.start) {
			let t = e.options.start?.exec(d);
			t && (p = t.index + t[0].length);
		} else {
			let t = e.options.line?.exec(d);
			t && (p = t.index + t[0].length);
		}
		if (c) {
			let t = e.options.end?.exec(d);
			t && (d = d.substring(0, t.index));
		}
		if (d = d.substring(0, g(d)), h(d, p) >= d.length) {
			if (t.length > 0) {
				let e = n.create(i, a);
				t.push({
					type: "break",
					content: "",
					range: r.create(e, e)
				});
			}
		} else {
			l.lastIndex = p;
			let e = l.exec(d);
			if (e) {
				let o = e[0], s = e[1], c = n.create(i, a + p), l = n.create(i, a + p + o.length);
				t.push({
					type: "tag",
					content: s,
					range: r.create(c, l)
				}), p += o.length, p = h(d, p);
			}
			if (p < d.length) {
				let e = d.substring(p), n = Array.from(e.matchAll(u));
				t.push(...f(n, e, i, a + p));
			}
		}
		i++, a = 0;
	}
	return t.length > 0 && t[t.length - 1].type === "break" ? t.slice(0, -1) : t;
}
function f(e, t, i, a) {
	let o = [];
	if (e.length === 0) {
		let e = n.create(i, a), s = n.create(i, a + t.length);
		o.push({
			type: "text",
			content: t,
			range: r.create(e, s)
		});
	} else {
		let s = 0;
		for (let c of e) {
			let e = c.index, l = t.substring(s, e);
			l.length > 0 && o.push({
				type: "text",
				content: t.substring(s, e),
				range: r.create(n.create(i, s + a), n.create(i, e + a))
			});
			let u = l.length + 1, d = c[1];
			if (o.push({
				type: "inline-tag",
				content: d,
				range: r.create(n.create(i, s + u + a), n.create(i, s + u + d.length + a))
			}), u += d.length, c.length === 4) {
				u += c[2].length;
				let e = c[3];
				o.push({
					type: "text",
					content: e,
					range: r.create(n.create(i, s + u + a), n.create(i, s + u + e.length + a))
				});
			} else o.push({
				type: "text",
				content: "",
				range: r.create(n.create(i, s + u + a), n.create(i, s + u + a))
			});
			s = e + c[0].length;
		}
		let c = t.substring(s);
		c.length > 0 && o.push({
			type: "text",
			content: c,
			range: r.create(n.create(i, s + a), n.create(i, s + a + c.length))
		});
	}
	return o;
}
var p = /\S/, m = /\s*$/;
function h(e, t) {
	let n = e.substring(t).match(p);
	return n ? t + n.index : e.length;
}
function g(e) {
	let t = e.match(m);
	if (t && typeof t.index == "number") return t.index;
}
function _(e) {
	let t = n.create(e.position.line, e.position.character);
	if (e.tokens.length === 0) return new E([], r.create(t, t));
	let i = [];
	for (; e.index < e.tokens.length;) {
		let t = v(e, i[i.length - 1]);
		t && i.push(t);
	}
	let a = i[0]?.range.start ?? t, o = i[i.length - 1]?.range.end ?? t;
	return new E(i, r.create(a, o));
}
function v(e, t) {
	let n = e.tokens[e.index];
	if (n.type === "tag") return S(e, !1);
	if (n.type === "text" || n.type === "inline-tag") return b(e);
	y(n, t), e.index++;
}
function y(e, t) {
	if (t) {
		let n = new j("", e.range);
		"inlines" in t ? t.inlines.push(n) : t.content.inlines.push(n);
	}
}
function b(e) {
	let t = e.tokens[e.index], n = t, i = t, a = [];
	for (; t && t.type !== "break" && t.type !== "tag";) a.push(x(e)), i = t, t = e.tokens[e.index];
	return new A(a, r.create(n.range.start, i.range.end));
}
function x(e) {
	return e.tokens[e.index].type === "inline-tag" ? S(e, !0) : C(e);
}
function S(e, t) {
	let n = e.tokens[e.index++], i = n.content.substring(1);
	if (e.tokens[e.index]?.type === "text") if (t) {
		let a = C(e);
		return new D(i, new A([a], a.range), t, r.create(n.range.start, a.range.end));
	} else {
		let a = b(e);
		return new D(i, a, t, r.create(n.range.start, a.range.end));
	}
	else {
		let e = n.range;
		return new D(i, new A([], e), t, e);
	}
}
function C(e) {
	let t = e.tokens[e.index++];
	return new j(t.content, t.range);
}
function w(e) {
	if (!e) return w({
		start: "/**",
		end: "*/",
		line: "*"
	});
	let { start: t, end: n, line: r } = e;
	return {
		start: T(t, !0),
		end: T(n, !1),
		line: T(r, !0)
	};
}
function T(e, n) {
	if (typeof e == "string" || typeof e == "object") {
		let r = typeof e == "string" ? t(e) : e.source;
		return RegExp(n ? `^\\s*${r}` : `\\s*${r}\\s*$`);
	} else return e;
}
var E = class {
	constructor(e, t) {
		this.elements = e, this.range = t;
	}
	getTag(e) {
		return this.getAllTags().find((t) => t.name === e);
	}
	getTags(e) {
		return this.getAllTags().filter((t) => t.name === e);
	}
	getAllTags() {
		return this.elements.filter((e) => "name" in e);
	}
	toString() {
		let e = "";
		for (let t of this.elements) if (e.length === 0) e = t.toString();
		else {
			let n = t.toString();
			e += M(e) + n;
		}
		return e.trim();
	}
	toMarkdown(e) {
		let t = "";
		for (let n of this.elements) if (t.length === 0) t = n.toMarkdown(e);
		else {
			let r = n.toMarkdown(e);
			t += M(t) + r;
		}
		return t.trim();
	}
}, D = class {
	constructor(e, t, n, r) {
		this.name = e, this.content = t, this.inline = n, this.range = r;
	}
	toString() {
		let e = `@${this.name}`, t = this.content.toString();
		return this.content.inlines.length === 1 ? e = `${e} ${t}` : this.content.inlines.length > 1 && (e = `${e}\n${t}`), this.inline ? `{${e}}` : e;
	}
	toMarkdown(e) {
		return e?.renderTag?.(this) ?? this.toMarkdownDefault(e);
	}
	toMarkdownDefault(e) {
		let t = this.content.toMarkdown(e);
		if (this.inline) {
			let n = O(this.name, t, e ?? {});
			if (typeof n == "string") return n;
		}
		let n = "";
		e?.tag === "italic" || e?.tag === void 0 ? n = "*" : e?.tag === "bold" ? n = "**" : e?.tag === "bold-italic" && (n = "***");
		let r = `${n}@${this.name}${n}`;
		return this.content.inlines.length === 1 ? r = `${r} — ${t}` : this.content.inlines.length > 1 && (r = `${r}\n${t}`), this.inline ? `{${r}}` : r;
	}
};
function O(e, t, n) {
	if (e === "linkplain" || e === "linkcode" || e === "link") {
		let r = t.indexOf(" "), i = t;
		if (r > 0) {
			let e = h(t, r);
			i = t.substring(e), t = t.substring(0, r);
		}
		return (e === "linkcode" || e === "link" && n.link === "code") && (i = `\`${i}\``), n.renderLink?.(t, i) ?? k(t, i);
	}
}
function k(e, t) {
	try {
		return a.parse(e, !0), `[${t}](${e})`;
	} catch {
		return e;
	}
}
var A = class {
	constructor(e, t) {
		this.inlines = e, this.range = t;
	}
	toString() {
		let e = "";
		for (let t = 0; t < this.inlines.length; t++) {
			let n = this.inlines[t], r = this.inlines[t + 1];
			e += n.toString(), r && r.range.start.line > n.range.start.line && (e += "\n");
		}
		return e;
	}
	toMarkdown(e) {
		let t = "";
		for (let n = 0; n < this.inlines.length; n++) {
			let r = this.inlines[n], i = this.inlines[n + 1];
			t += r.toMarkdown(e), i && i.range.start.line > r.range.start.line && (t += "\n");
		}
		return t;
	}
}, j = class {
	constructor(e, t) {
		this.text = e, this.range = t;
	}
	toString() {
		return this.text;
	}
	toMarkdown() {
		return this.text;
	}
};
function M(e) {
	return e.endsWith("\n") ? "\n" : "\n\n";
}
//#endregion
export { s as isJSDoc, o as parseJSDoc };
