/*! © 2026 Yahoo, Inc. UDS v0.0.0-development */
require("../../../_virtual/_rolldown/runtime.cjs");
let lodash_es = require("lodash-es");
let ts_morph = require("ts-morph");
//#region ../tailwind-v3/dist/utils/tsMorph.mjs
/*! © 2026 Yahoo, Inc. UDS Tailwind and Purger v0.0.0-development */
/** Determines if an expression is using React.forwardRef. */
function isForwardRefExpression(node) {
	if (ts_morph.Node.isCallExpression(node)) {
		const expression = node.getExpression();
		/**
		* forwardRef(() => <Component />)
		*/
		if (ts_morph.Node.isIdentifier(expression) && expression.getText() === "forwardRef") return true;
		/**
		* React.forwardRef(() => <Component />)
		*/
		if (ts_morph.Node.isPropertyAccessExpression(expression) && expression.getText() === "React.forwardRef") return true;
	}
	return false;
}
/** Determines if a node is a JSX component. */
function 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.charAt(0)) : false;
}
/** Returns a functional component declaration, unwrapping forwardRef if needed. */
function getReactFunctionDeclaration(declaration) {
	if (isJsxComponent(declaration)) {
		if (ts_morph.Node.isFunctionDeclaration(declaration)) return declaration;
		if (ts_morph.Node.isVariableDeclaration(declaration)) {
			const initializer = declaration.getInitializer();
			if (initializer) {
				if (isForwardRefExpression(initializer)) {
					const [initializerDeclaration] = initializer.getArguments();
					if (ts_morph.Node.isFunctionDeclaration(initializerDeclaration) || ts_morph.Node.isFunctionExpression(initializerDeclaration) || ts_morph.Node.isArrowFunction(initializerDeclaration)) return initializerDeclaration;
				} else if (ts_morph.Node.isFunctionDeclaration(initializer) || ts_morph.Node.isFunctionExpression(initializer) || ts_morph.Node.isArrowFunction(initializer)) return initializer;
			}
		}
	}
	return null;
}
/**
* Traces component references.
*
* This is similar to `findReferencesAsNodes` but returns JsxSelfClosingElement and JsxElement nodes.
* Note, this currently does not account for cases where the component is used as a prop or is renamed.
*/
function findReferencesAsJsxElements(identifer) {
	const jsxElements = [];
	for (const reference of identifer.findReferencesAsNodes()) {
		const node = reference.getFirstAncestor((node) => {
			return ts_morph.Node.isJsxOpeningElement(node) || ts_morph.Node.isJsxSelfClosingElement(node);
		});
		if (node) jsxElements.push(node);
	}
	return jsxElements;
}
/**
* Given a Jsx Reference, this function finds the parent of that reference,
* looks at the props, and returns the name used either for spread operator rest or
* the prop name if there's no spread operator
*/
const getParentSpreadOrParamIdentifier = (reference) => {
	const blockParent = reference.getParentWhile((parent) => {
		return !getReactFunctionDeclaration(parent);
	}).getParent();
	if (!blockParent) return;
	const reactComponent = getReactFunctionDeclaration(blockParent);
	if (reactComponent) {
		const parameters = reactComponent.getParameters();
		for (let i = 0; i < parameters.length; i++) {
			const parameter = parameters[i];
			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 !== void 0) return dotDotToken?.getParent()?.getFirstChildByKindOrThrow(ts_morph.SyntaxKind.Identifier).getText();
			} else if (identifier) return identifier.getText();
		}
	}
};
/**
* Get used props on a reference.
*/
const getUsedPropsInReference = (reference, variants) => {
	const attributes = reference.getAttributes();
	const res = [];
	attributes.forEach((attribute) => {
		if (ts_morph.Node.isJsxAttribute(attribute)) {
			const attributeName = attribute.getNameNode().getText();
			const stringLiterals = attribute.getDescendantsOfKind(ts_morph.SyntaxKind.StringLiteral);
			const values = [];
			if (stringLiterals.length !== 0) stringLiterals.forEach((stringLiteral) => values.push(stringLiteral.getLiteralText()));
			const typeName = attribute.getFirstChildByKindOrThrow(ts_morph.SyntaxKind.Identifier).getDefinitions().map((d) => d.getDeclarationNode()?.getChildrenOfKind(ts_morph.SyntaxKind.TypeReference).pop()?.getText()).pop();
			if (!typeName && !(0, lodash_es.isEmpty)(attributeName)) res.push([attributeName, values]);
			else if (variants[(0, lodash_es.camelCase)(typeName)] && !(0, lodash_es.isEmpty)(typeName)) res.push([(0, lodash_es.camelCase)(typeName), values]);
			else if (!(0, lodash_es.isEmpty)(attributeName)) res.push([attributeName, values]);
		}
		if (ts_morph.Node.isJsxSpreadAttribute(attribute)) {
			const componentPropsSpreadIdentifier = getParentSpreadOrParamIdentifier(reference);
			const children = attribute.getExpression().getChildren();
			const lastIdentifier = children.length === 0 ? attribute.getExpression() : children.pop();
			if (!lastIdentifier || lastIdentifier?.getText() === componentPropsSpreadIdentifier) return [];
			lastIdentifier.getType().getProperties().forEach((property) => {
				const propertyNameFromType = property.getDeclarations().map((declaration) => {
					return (0, lodash_es.camelCase)(declaration.getChildrenOfKind(ts_morph.SyntaxKind.TypeReference)[0]?.getText() ?? "");
				})[0];
				if (!(0, lodash_es.isEmpty)(property.getName())) res.push([property.getName(), []]);
				if (!(0, lodash_es.isEmpty)(propertyNameFromType)) res.push([propertyNameFromType, []]);
			});
		}
		return [];
	});
	return res;
};
//#endregion
exports.findReferencesAsJsxElements = findReferencesAsJsxElements;
exports.getUsedPropsInReference = getUsedPropsInReference;
