import { ok } from "../../devlop/lib/default.js";
import { handle } from "../../mdast-util-to-markdown/lib/handle/index.js";
function gfmTaskListItemFromMarkdown() {
	return { exit: {
		taskListCheckValueChecked: exitCheck,
		taskListCheckValueUnchecked: exitCheck,
		paragraph: exitParagraphWithTaskListItem
	} };
}
function gfmTaskListItemToMarkdown() {
	return {
		unsafe: [{
			atBreak: !0,
			character: "-",
			after: "[:|-]"
		}],
		handlers: { listItem: listItemWithTaskListItem }
	};
}
function exitCheck(e) {
	let t = this.stack[this.stack.length - 2];
	t.type, t.checked = e.type === "taskListCheckValueChecked";
}
function exitParagraphWithTaskListItem(e) {
	let t = this.stack[this.stack.length - 2];
	if (t && t.type === "listItem" && typeof t.checked == "boolean") {
		let e = this.stack[this.stack.length - 1];
		e.type;
		let n = e.children[0];
		if (n && n.type === "text") {
			let r = t.children, i = -1, a;
			for (; ++i < r.length;) {
				let e = r[i];
				if (e.type === "paragraph") {
					a = e;
					break;
				}
			}
			a === e && (n.value = n.value.slice(1), n.value.length === 0 ? e.children.shift() : e.position && n.position && typeof n.position.start.offset == "number" && (n.position.start.column++, n.position.start.offset++, e.position.start = Object.assign({}, n.position.start)));
		}
	}
	this.exit(e);
}
function listItemWithTaskListItem(e, n, r, i) {
	let a = e.children[0], o = typeof e.checked == "boolean" && a && a.type === "paragraph", s = "[" + (e.checked ? "x" : " ") + "] ", c = r.createTracker(i);
	o && c.move(s);
	let l = handle.listItem(e, n, r, {
		...i,
		...c.current()
	});
	return o && (l = l.replace(/^(?:[*+-]|\d+\.)([\r\n]| {1,3})/, u)), l;
	function u(e) {
		return e + s;
	}
}
export { gfmTaskListItemFromMarkdown, gfmTaskListItemToMarkdown };
