import { checkQuote } from "../util/check-quote.js";
import { formatLinkAsAutolink } from "../util/format-link-as-autolink.js";
link.peek = linkPeek;
function link(n, r, i, a) {
	let o = checkQuote(i), s = o === "\"" ? "Quote" : "Apostrophe", c = i.createTracker(a), l, u;
	if (formatLinkAsAutolink(n, i)) {
		let e = i.stack;
		i.stack = [], l = i.enter("autolink");
		let t = c.move("<");
		return t += c.move(i.containerPhrasing(n, {
			before: t,
			after: ">",
			...c.current()
		})), t += c.move(">"), l(), i.stack = e, t;
	}
	l = i.enter("link"), u = i.enter("label");
	let d = c.move("[");
	return d += c.move(i.containerPhrasing(n, {
		before: d,
		after: "](",
		...c.current()
	})), d += c.move("]("), u(), !n.url && n.title || /[\0- \u007F]/.test(n.url) ? (u = i.enter("destinationLiteral"), d += c.move("<"), d += c.move(i.safe(n.url, {
		before: d,
		after: ">",
		...c.current()
	})), d += c.move(">")) : (u = i.enter("destinationRaw"), d += c.move(i.safe(n.url, {
		before: d,
		after: n.title ? " " : ")",
		...c.current()
	}))), u(), n.title && (u = i.enter(`title${s}`), d += c.move(" " + o), d += c.move(i.safe(n.title, {
		before: d,
		after: o,
		...c.current()
	})), d += c.move(o), u()), d += c.move(")"), l(), d;
}
function linkPeek(e, n, r) {
	return formatLinkAsAutolink(e, r) ? "<" : "[";
}
export { link };
