import { n as mergeRefs } from "./refs.js";
import React from "react";
import styles from "./components/AsChildSlot/styles.module.scss";
var isAriaDisabled = (v) => v === true || v === "true";
const AsChildSlot = (props) => {
	const { children, className, content, disabled, onSelect, ariaCurrent, elementRef, ref } = props;
	const childElement = React.Children.toArray(children).filter(React.isValidElement)[0];
	const nodeRef = React.useRef(null);
	const childPropsRef = childElement?.props?.ref;
	const mergedRef = mergeRefs([
		(node) => {
			nodeRef.current = node;
		},
		childPropsRef,
		elementRef
	]);
	const childProps = childElement?.props || {};
	const childDisabled = !!childProps.disabled || isAriaDisabled(childProps["aria-disabled"]);
	const isDisabled = !!disabled || childDisabled;
	React.useImperativeHandle(ref, () => ({ click: () => {
		if (isDisabled) return;
		nodeRef.current?.click();
	} }), [isDisabled]);
	if (!childElement) return null;
	const childOnClick = childProps.onClick;
	const wrappedOnClick = (e) => {
		if (isDisabled) {
			e.preventDefault();
			e.stopPropagation();
			return;
		}
		onSelect?.(e);
		childOnClick?.(e);
	};
	const childOnKeyDown = childProps.onKeyDown;
	const wrappedOnKeyDown = (e) => {
		childOnKeyDown?.(e);
		if (e.defaultPrevented) return;
		if (e.key === " " || e.key === "Spacebar" || e.code === "Space") {
			if (isDisabled) {
				e.preventDefault();
				return;
			}
			e.preventDefault();
			nodeRef.current?.click();
		}
	};
	const isButtonLike = typeof childProps.href === "undefined";
	const elementWithChildren = childElement;
	const wrappedChildren = content && React.isValidElement(content) ? React.cloneElement(content, void 0, elementWithChildren.props.children) : elementWithChildren.props.children;
	const nextProps = {
		ref: mergedRef,
		className: `${styles["as-child-reset"]} ${className ?? ""}`,
		style: void 0,
		"aria-disabled": isDisabled || void 0,
		"aria-current": ariaCurrent,
		...isButtonLike && !("type" in childProps) ? { type: "button" } : {},
		onClick: wrappedOnClick,
		onKeyDown: wrappedOnKeyDown,
		children: wrappedChildren
	};
	return React.cloneElement(childElement, nextProps);
};
var AsChildSlot_default$1 = AsChildSlot;
export { AsChildSlot as n, AsChildSlot_default$1 as t };

//# sourceMappingURL=AsChildSlot.js.map