import { isString } from "../strings.js";
import { isClipboardTextFieldNode, isCurrencyAmountNode, isIconNode, isLinkNode, isNextLinkNode, isTextNode } from "./nodes.js";
import { toReactNodes } from "./toReactNodes.js";
import { ensureArray } from "@lightsparkdev/core";
//#region src/utils/toReactNodes/setReactNodesTypography.tsx
function isNonTypographicReactNode(node) {
	return typeof node === "undefined" || node === null || isIconNode(node);
}
function setReactNodesTypography(nodesArg, typography, replaceExistingTypography = true) {
	const nodes = ensureArray(nodesArg);
	const typographyMap = {
		link: typography.link || typography.default,
		text: typography.text || typography.default,
		nextLink: typography.nextLink || typography.default,
		currencyAmount: typography.currencyAmount || typography.default,
		clipboardTextField: typography.clipboardTextField || typography.default
	};
	return nodes.map((node) => {
		if (isNonTypographicReactNode(node)) return node;
		else if (isLinkNode(node) && typographyMap.link) return { link: {
			...node.link,
			typography: replaceExistingTypography ? typographyMap.link : node.link.typography || typographyMap.link
		} };
		else if (isNextLinkNode(node) && typographyMap.nextLink) return { nextLink: {
			...node.nextLink,
			typography: replaceExistingTypography ? typographyMap.nextLink : node.nextLink.typography || typographyMap.nextLink
		} };
		else if (isCurrencyAmountNode(node) && typographyMap.currencyAmount) return { currencyAmount: {
			...node.currencyAmount,
			typography: typographyMap.currencyAmount
		} };
		else if (isClipboardTextFieldNode(node) && typographyMap.clipboardTextField) return { clipboardTextField: {
			...node.clipboardTextField,
			typography: typographyMap.clipboardTextField
		} };
		else if ((isTextNode(node) || isString(node)) && typographyMap.text) return setTextNodeTypography(node, typographyMap.text, replaceExistingTypography);
		return node;
	});
}
function setTextNodeTypography(node, typography, replaceExistingTypography = true) {
	if (isString(node)) return {
		text: node,
		typography
	};
	const useExistingTypography = !replaceExistingTypography && "typography" in node ? node.typography : void 0;
	return {
		...node,
		typography: useExistingTypography || typography
	};
}
function toReactNodesWithTypographyMap(nodesArg, nodesTypographyMap, replaceExistingTypography = true) {
	if (!nodesArg) return null;
	return toReactNodes(setReactNodesTypography(nodesArg, nodesTypographyMap, replaceExistingTypography));
}
function setDefaultReactNodesTypography(nodesArg, setDefaultReactNodesTypographyArg) {
	return setReactNodesTypography(nodesArg, setDefaultReactNodesTypographyArg, false);
}
//#endregion
export { isNonTypographicReactNode, setDefaultReactNodesTypography, setReactNodesTypography, toReactNodesWithTypographyMap };
