import { __toESM } from "../../../_virtual/rolldown_runtime.js";
import { hastToReact } from "../../property-information/lib/hast-to-react.js";
import { find } from "../../property-information/lib/find.js";
import { html, svg } from "../../property-information/index.js";
import { stringify } from "../../comma-separated-tokens/index.js";
import { stringify as stringify$1 } from "../../space-separated-tokens/index.js";
import { ok } from "../../devlop/lib/default.js";
import { pointStart } from "../../unist-util-position/lib/index.js";
import { name } from "../../estree-util-is-identifier-name/lib/index.js";
import { whitespace } from "../../hast-util-whitespace/lib/index.js";
import { require_cjs } from "../../style-to-js/cjs/index.js";
import { VFileMessage } from "../../vfile-message/lib/index.js";
var import_cjs = /* @__PURE__ */ __toESM(require_cjs(), 1), own = {}.hasOwnProperty, emptyMap = /* @__PURE__ */ new Map(), cap = /[A-Z]/g, tableElements = new Set([
	"table",
	"tbody",
	"thead",
	"tfoot",
	"tr"
]), tableCellElement = new Set(["td", "th"]), docs = "https://github.com/syntax-tree/hast-util-to-jsx-runtime";
function toJsxRuntime(p, P) {
	if (!P || P.Fragment === void 0) throw TypeError("Expected `Fragment` in options");
	let F = P.filePath || void 0, R;
	if (P.development) {
		if (typeof P.jsxDEV != "function") throw TypeError("Expected `jsxDEV` in options when `development: true`");
		R = developmentCreate(F, P.jsxDEV);
	} else {
		if (typeof P.jsx != "function") throw TypeError("Expected `jsx` in production options");
		if (typeof P.jsxs != "function") throw TypeError("Expected `jsxs` in production options");
		R = productionCreate(F, P.jsx, P.jsxs);
	}
	let z = {
		Fragment: P.Fragment,
		ancestors: [],
		components: P.components || {},
		create: R,
		elementAttributeNameCase: P.elementAttributeNameCase || "react",
		evaluater: P.createEvaluater ? P.createEvaluater() : void 0,
		filePath: F,
		ignoreInvalidStyle: P.ignoreInvalidStyle || !1,
		passKeys: P.passKeys !== !1,
		passNode: P.passNode || !1,
		schema: P.space === "svg" ? svg : html,
		stylePropertyNameCase: P.stylePropertyNameCase || "dom",
		tableCellAlignToStyle: P.tableCellAlignToStyle !== !1
	}, B = one(z, p, void 0);
	return B && typeof B != "string" ? B : z.create(p, z.Fragment, { children: B || void 0 }, void 0);
}
function one(p, P, F) {
	if (P.type === "element") return element(p, P, F);
	if (P.type === "mdxFlowExpression" || P.type === "mdxTextExpression") return mdxExpression(p, P);
	if (P.type === "mdxJsxFlowElement" || P.type === "mdxJsxTextElement") return mdxJsxElement(p, P, F);
	if (P.type === "mdxjsEsm") return mdxEsm(p, P);
	if (P.type === "root") return root(p, P, F);
	if (P.type === "text") return text(p, P);
}
function element(p, P, F) {
	let I = p.schema, R = I;
	P.tagName.toLowerCase() === "svg" && I.space === "html" && (R = svg, p.schema = R), p.ancestors.push(P);
	let z = findComponentFromName(p, P.tagName, !1), B = createElementProps(p, P), V = createChildren(p, P);
	return tableElements.has(P.tagName) && (V = V.filter(function(p) {
		return typeof p == "string" ? !whitespace(p) : !0;
	})), addNode(p, B, z, P), addChildren(B, V), p.ancestors.pop(), p.schema = I, p.create(P, z, B, F);
}
function mdxExpression(p, P) {
	if (P.data && P.data.estree && p.evaluater) {
		let F = P.data.estree.body[0];
		return F.type, p.evaluater.evaluateExpression(F.expression);
	}
	crashEstree(p, P.position);
}
function mdxEsm(p, P) {
	if (P.data && P.data.estree && p.evaluater) return p.evaluater.evaluateProgram(P.data.estree);
	crashEstree(p, P.position);
}
function mdxJsxElement(p, P, F) {
	let I = p.schema, R = I;
	P.name === "svg" && I.space === "html" && (R = svg, p.schema = R), p.ancestors.push(P);
	let z = P.name === null ? p.Fragment : findComponentFromName(p, P.name, !0), B = createJsxElementProps(p, P), V = createChildren(p, P);
	return addNode(p, B, z, P), addChildren(B, V), p.ancestors.pop(), p.schema = I, p.create(P, z, B, F);
}
function root(p, P, F) {
	let I = {};
	return addChildren(I, createChildren(p, P)), p.create(P, p.Fragment, I, F);
}
function text(p, P) {
	return P.value;
}
function addNode(p, P, F, I) {
	typeof F != "string" && F !== p.Fragment && p.passNode && (P.node = I);
}
function addChildren(p, P) {
	if (P.length > 0) {
		let F = P.length > 1 ? P : P[0];
		F && (p.children = F);
	}
}
function productionCreate(p, P, F) {
	return I;
	function I(p, I, L, R) {
		let z = Array.isArray(L.children) ? F : P;
		return R ? z(I, L, R) : z(I, L);
	}
}
function developmentCreate(p, P) {
	return F;
	function F(F, I, L, R) {
		let z = Array.isArray(L.children), B = pointStart(F);
		return P(I, L, R, z, {
			columnNumber: B ? B.column - 1 : void 0,
			fileName: p,
			lineNumber: B ? B.line : void 0
		}, void 0);
	}
}
function createElementProps(p, P) {
	let F = {}, I, L;
	for (L in P.properties) if (L !== "children" && own.call(P.properties, L)) {
		let R = createProperty(p, L, P.properties[L]);
		if (R) {
			let [L, z] = R;
			p.tableCellAlignToStyle && L === "align" && typeof z == "string" && tableCellElement.has(P.tagName) ? I = z : F[L] = z;
		}
	}
	if (I) {
		let P = F.style ||= {};
		P[p.stylePropertyNameCase === "css" ? "text-align" : "textAlign"] = I;
	}
	return F;
}
function createJsxElementProps(p, P) {
	let F = {};
	for (let I of P.attributes) if (I.type === "mdxJsxExpressionAttribute") if (I.data && I.data.estree && p.evaluater) {
		let P = I.data.estree.body[0];
		P.type;
		let L = P.expression;
		L.type;
		let R = L.properties[0];
		R.type, Object.assign(F, p.evaluater.evaluateExpression(R.argument));
	} else crashEstree(p, P.position);
	else {
		let L = I.name, R;
		if (I.value && typeof I.value == "object") if (I.value.data && I.value.data.estree && p.evaluater) {
			let P = I.value.data.estree.body[0];
			P.type, R = p.evaluater.evaluateExpression(P.expression);
		} else crashEstree(p, P.position);
		else R = I.value === null ? !0 : I.value;
		F[L] = R;
	}
	return F;
}
function createChildren(p, P) {
	let F = [], I = -1, L = p.passKeys ? /* @__PURE__ */ new Map() : emptyMap;
	for (; ++I < P.children.length;) {
		let R = P.children[I], z;
		if (p.passKeys) {
			let p = R.type === "element" ? R.tagName : R.type === "mdxJsxFlowElement" || R.type === "mdxJsxTextElement" ? R.name : void 0;
			if (p) {
				let P = L.get(p) || 0;
				z = p + "-" + P, L.set(p, P + 1);
			}
		}
		let B = one(p, R, z);
		B !== void 0 && F.push(B);
	}
	return F;
}
function createProperty(p, I, L) {
	let B = find(p.schema, I);
	if (!(L == null || typeof L == "number" && Number.isNaN(L))) {
		if (Array.isArray(L) && (L = B.commaSeparated ? stringify(L) : stringify$1(L)), B.property === "style") {
			let P = typeof L == "object" ? L : parseStyle(p, String(L));
			return p.stylePropertyNameCase === "css" && (P = transformStylesToCssCasing(P)), ["style", P];
		}
		return [p.elementAttributeNameCase === "react" && B.space ? hastToReact[B.property] || B.property : B.attribute, L];
	}
}
function parseStyle(p, P) {
	try {
		return (0, import_cjs.default)(P, { reactCompat: !0 });
	} catch (P) {
		if (p.ignoreInvalidStyle) return {};
		let F = P, I = new VFileMessage("Cannot parse `style` attribute", {
			ancestors: p.ancestors,
			cause: F,
			ruleId: "style",
			source: "hast-util-to-jsx-runtime"
		});
		throw I.file = p.filePath || void 0, I.url = docs + "#cannot-parse-style-attribute", I;
	}
}
function findComponentFromName(p, P, F) {
	let I;
	if (!F) I = {
		type: "Literal",
		value: P
	};
	else if (P.includes(".")) {
		let p = P.split("."), F = -1, L;
		for (; ++F < p.length;) {
			let P = name(p[F]) ? {
				type: "Identifier",
				name: p[F]
			} : {
				type: "Literal",
				value: p[F]
			};
			L = L ? {
				type: "MemberExpression",
				object: L,
				property: P,
				computed: !!(F && P.type === "Literal"),
				optional: !1
			} : P;
		}
		I = L;
	} else I = name(P) && !/^[a-z]/.test(P) ? {
		type: "Identifier",
		name: P
	} : {
		type: "Literal",
		value: P
	};
	if (I.type === "Literal") {
		let P = I.value;
		return own.call(p.components, P) ? p.components[P] : P;
	}
	if (p.evaluater) return p.evaluater.evaluateExpression(I);
	crashEstree(p);
}
function crashEstree(p, P) {
	let F = new VFileMessage("Cannot handle MDX estrees without `createEvaluater`", {
		ancestors: p.ancestors,
		place: P,
		ruleId: "mdx-estree",
		source: "hast-util-to-jsx-runtime"
	});
	throw F.file = p.filePath || void 0, F.url = docs + "#cannot-handle-mdx-estrees-without-createevaluater", F;
}
function transformStylesToCssCasing(p) {
	let P = {}, F;
	for (F in p) own.call(p, F) && (P[transformStyleToCssCasing(F)] = p[F]);
	return P;
}
function transformStyleToCssCasing(p) {
	let P = p.replace(cap, toDash);
	return P.slice(0, 3) === "ms-" && (P = "-" + P), P;
}
function toDash(p) {
	return "-" + p.toLowerCase();
}
export { toJsxRuntime };
