import { composeRefs } from "../utils/compose-refs.js";
import { Children, cloneElement, createElement, forwardRef, isValidElement, memo } from "react";
import { mergeProps } from "@zag-js/core";
//#region src/components/factory.ts
function getRef(element) {
	let getter = Object.getOwnPropertyDescriptor(element.props, "ref")?.get;
	let mayWarn = getter && "isReactWarning" in getter && getter.isReactWarning;
	if (mayWarn) return element.ref;
	getter = Object.getOwnPropertyDescriptor(element, "ref")?.get;
	mayWarn = getter && "isReactWarning" in getter && getter.isReactWarning;
	if (mayWarn) return element.props.ref;
	return element.props.ref || element.ref;
}
var withAsChild = (Component) => {
	const Comp = memo(forwardRef((props, ref) => {
		const { asChild, children, ...restProps } = props;
		if (!asChild) return createElement(Component, {
			...restProps,
			ref
		}, children);
		if (!isValidElement(children)) return null;
		const onlyChild = Children.only(children);
		const childRef = getRef(onlyChild);
		return cloneElement(onlyChild, {
			...mergeProps(restProps, onlyChild.props),
			ref: ref ? composeRefs(ref, childRef) : childRef
		});
	}));
	Comp.displayName = Component.displayName || Component.name;
	return Comp;
};
var jsxFactory = () => {
	const cache = /* @__PURE__ */ new Map();
	return new Proxy(withAsChild, {
		apply(_target, _thisArg, argArray) {
			return withAsChild(argArray[0]);
		},
		get(_, element) {
			const asElement = element;
			if (!cache.has(asElement)) cache.set(asElement, withAsChild(asElement));
			return cache.get(asElement);
		}
	});
};
var ark = jsxFactory();
//#endregion
export { ark, jsxFactory };
