import { ok } from "../../devlop/lib/default.js";
import { normalizeIdentifier } from "../../micromark-util-normalize-identifier/index.js";
footnoteReference.peek = footnoteReferencePeek;
function enterFootnoteCallString() {
	this.buffer();
}
function enterFootnoteCall(e) {
	this.enter({
		type: "footnoteReference",
		identifier: "",
		label: ""
	}, e);
}
function enterFootnoteDefinitionLabelString() {
	this.buffer();
}
function enterFootnoteDefinition(e) {
	this.enter({
		type: "footnoteDefinition",
		identifier: "",
		label: "",
		children: []
	}, e);
}
function exitFootnoteCallString(e) {
	let l = this.resume(), u = this.stack[this.stack.length - 1];
	u.type, u.identifier = normalizeIdentifier(this.sliceSerialize(e)).toLowerCase(), u.label = l;
}
function exitFootnoteCall(e) {
	this.exit(e);
}
function exitFootnoteDefinitionLabelString(e) {
	let l = this.resume(), u = this.stack[this.stack.length - 1];
	u.type, u.identifier = normalizeIdentifier(this.sliceSerialize(e)).toLowerCase(), u.label = l;
}
function exitFootnoteDefinition(e) {
	this.exit(e);
}
function footnoteReferencePeek() {
	return "[";
}
function footnoteReference(e, c, l, u) {
	let d = l.createTracker(u), f = d.move("[^"), p = l.enter("footnoteReference"), m = l.enter("reference");
	return f += d.move(l.safe(l.associationId(e), {
		after: "]",
		before: f
	})), m(), p(), f += d.move("]"), f;
}
function gfmFootnoteFromMarkdown() {
	return {
		enter: {
			gfmFootnoteCallString: enterFootnoteCallString,
			gfmFootnoteCall: enterFootnoteCall,
			gfmFootnoteDefinitionLabelString: enterFootnoteDefinitionLabelString,
			gfmFootnoteDefinition: enterFootnoteDefinition
		},
		exit: {
			gfmFootnoteCallString: exitFootnoteCallString,
			gfmFootnoteCall: exitFootnoteCall,
			gfmFootnoteDefinitionLabelString: exitFootnoteDefinitionLabelString,
			gfmFootnoteDefinition: exitFootnoteDefinition
		}
	};
}
function gfmFootnoteToMarkdown(e) {
	let c = !1;
	return e && e.firstLineBlank && (c = !0), {
		handlers: {
			footnoteDefinition: l,
			footnoteReference
		},
		unsafe: [{
			character: "[",
			inConstruct: [
				"label",
				"phrasing",
				"reference"
			]
		}]
	};
	function l(e, l, u, d) {
		let f = u.createTracker(d), p = f.move("[^"), m = u.enter("footnoteDefinition"), h = u.enter("label");
		return p += f.move(u.safe(u.associationId(e), {
			before: p,
			after: "]"
		})), h(), p += f.move("]:"), e.children && e.children.length > 0 && (f.shift(4), p += f.move((c ? "\n" : " ") + u.indentLines(u.containerFlow(e, f.current()), c ? mapAll : mapExceptFirst))), m(), p;
	}
}
function mapExceptFirst(e, c, l) {
	return c === 0 ? e : mapAll(e, c, l);
}
function mapAll(e, c, l) {
	return (l ? "" : "    ") + e;
}
export { gfmFootnoteFromMarkdown, gfmFootnoteToMarkdown };
