/*! © 2026 Yahoo, Inc. UDS v0.0.0-development */
import { Node, SyntaxKind } from "ts-morph";
//#region ../css/dist/purger/optimized/ast/jsx.mjs
/*! © 2026 Yahoo, Inc. UDS CSS v0.0.0-development */
/**
* Check if an expression is using React.forwardRef
*/
const isForwardRefExpression = (node) => {
	if (!Node.isCallExpression(node)) return false;
	const expression = node.getExpression();
	if (Node.isIdentifier(expression) && expression.getText() === "forwardRef") return true;
	if (Node.isPropertyAccessExpression(expression) && expression.getText() === "React.forwardRef") return true;
	return false;
};
/**
* Check if a node is a JSX component (PascalCase name)
*/
const isJsxComponent = (node) => {
	let name;
	if (Node.isVariableDeclaration(node) || Node.isFunctionDeclaration(node) || Node.isClassDeclaration(node)) name = node.getName();
	else if (Node.isFunctionExpression(node) || Node.isArrowFunction(node)) name = node.getFirstAncestorByKind(SyntaxKind.VariableDeclaration)?.getName();
	return name ? /^[A-Z]/.test(name) : false;
};
/**
* Get the function declaration from a component, unwrapping forwardRef if needed
*/
const getReactFunctionDeclaration = (declaration) => {
	if (!isJsxComponent(declaration)) return null;
	if (Node.isFunctionDeclaration(declaration)) return declaration;
	if (Node.isVariableDeclaration(declaration)) {
		const initializer = declaration.getInitializer();
		if (!initializer) return null;
		if (isForwardRefExpression(initializer)) {
			const [arg] = initializer.getArguments();
			if (Node.isFunctionDeclaration(arg) || Node.isFunctionExpression(arg) || Node.isArrowFunction(arg)) return arg;
		} else if (Node.isFunctionDeclaration(initializer) || Node.isFunctionExpression(initializer) || Node.isArrowFunction(initializer)) return initializer;
	}
	return null;
};
/**
* Get the parent component that contains a JSX reference.
* Returns information about the component including spread rest identifier.
*/
const getParentComponentInfo = (reference) => {
	const block = reference.getParentWhile((parent) => !getReactFunctionDeclaration(parent));
	if (!block) return;
	const blockParent = block.getParent();
	if (!blockParent) return;
	const reactComponent = getReactFunctionDeclaration(blockParent);
	if (!reactComponent) return;
	let componentName;
	const variableDeclaration = reactComponent.getFirstAncestorByKind(SyntaxKind.VariableDeclaration);
	if (variableDeclaration) componentName = variableDeclaration.getName();
	else if (Node.isFunctionDeclaration(reactComponent)) componentName = reactComponent.getName();
	if (!componentName) return;
	let spreadRestIdentifier;
	let paramIdentifier;
	reactComponent.getParameters().forEach((parameter) => {
		const objectBindingPattern = parameter.getChildrenOfKind(SyntaxKind.ObjectBindingPattern)[0];
		const identifier = parameter.getChildrenOfKind(SyntaxKind.Identifier)[0];
		if (objectBindingPattern) {
			const dotDotToken = objectBindingPattern.getFirstDescendantByKind(SyntaxKind.DotDotDotToken);
			if (dotDotToken) spreadRestIdentifier = dotDotToken.getParent()?.getFirstChildByKindOrThrow(SyntaxKind.Identifier).getText();
		} else if (identifier) paramIdentifier = identifier.getText();
	});
	return {
		componentName,
		spreadRestIdentifier,
		paramIdentifier
	};
};
//#endregion
export { getParentComponentInfo };
