import { checkQuote as e } from "../util/check-quote.js";
import { formatLinkAsAutolink as t } from "../util/format-link-as-autolink.js";
//#region ../../node_modules/mdast-util-to-markdown/lib/handle/link.js
n.peek = r;
function n(n, r, i, a) {
	let o = e(i), s = o === "\"" ? "Quote" : "Apostrophe", c = i.createTracker(a), l, u;
	if (t(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 r(e, n, r) {
	return t(e, r) ? "<" : "[";
}
//#endregion
export { n as link };
