/*! © 2026 Yahoo, Inc. UDS v0.0.0-development */
require("../../../../../_virtual/_rolldown/runtime.cjs");
let ts_morph = require("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 (!ts_morph.Node.isCallExpression(node)) return false;
	const expression = node.getExpression();
	if (ts_morph.Node.isIdentifier(expression) && expression.getText() === "forwardRef") return true;
	if (ts_morph.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 (ts_morph.Node.isVariableDeclaration(node) || ts_morph.Node.isFunctionDeclaration(node) || ts_morph.Node.isClassDeclaration(node)) name = node.getName();
	else if (ts_morph.Node.isFunctionExpression(node) || ts_morph.Node.isArrowFunction(node)) name = node.getFirstAncestorByKind(ts_morph.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 (ts_morph.Node.isFunctionDeclaration(declaration)) return declaration;
	if (ts_morph.Node.isVariableDeclaration(declaration)) {
		const initializer = declaration.getInitializer();
		if (!initializer) return null;
		if (isForwardRefExpression(initializer)) {
			const [arg] = initializer.getArguments();
			if (ts_morph.Node.isFunctionDeclaration(arg) || ts_morph.Node.isFunctionExpression(arg) || ts_morph.Node.isArrowFunction(arg)) return arg;
		} else if (ts_morph.Node.isFunctionDeclaration(initializer) || ts_morph.Node.isFunctionExpression(initializer) || ts_morph.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(ts_morph.SyntaxKind.VariableDeclaration);
	if (variableDeclaration) componentName = variableDeclaration.getName();
	else if (ts_morph.Node.isFunctionDeclaration(reactComponent)) componentName = reactComponent.getName();
	if (!componentName) return;
	let spreadRestIdentifier;
	let paramIdentifier;
	reactComponent.getParameters().forEach((parameter) => {
		const objectBindingPattern = parameter.getChildrenOfKind(ts_morph.SyntaxKind.ObjectBindingPattern)[0];
		const identifier = parameter.getChildrenOfKind(ts_morph.SyntaxKind.Identifier)[0];
		if (objectBindingPattern) {
			const dotDotToken = objectBindingPattern.getFirstDescendantByKind(ts_morph.SyntaxKind.DotDotDotToken);
			if (dotDotToken) spreadRestIdentifier = dotDotToken.getParent()?.getFirstChildByKindOrThrow(ts_morph.SyntaxKind.Identifier).getText();
		} else if (identifier) paramIdentifier = identifier.getText();
	});
	return {
		componentName,
		spreadRestIdentifier,
		paramIdentifier
	};
};
//#endregion
exports.getParentComponentInfo = getParentComponentInfo;
