/*! © 2026 Yahoo, Inc. UDS v0.0.0-development */
"use client";
import { cx } from "../../../styles/styler.js";
import { hasDisplayName } from "../../../utils/hasDisplayName.js";
import { Box } from "../../Box.js";
import { useRtl } from "../../../hooks/useRtl.js";
import { PaddleNavContext, defaultPaddleHiddenByRole } from "./paddleNavContext.js";
import { PaddleButtonNext } from "./PaddleButtonNext.js";
import { PaddleButtonPrevious } from "./PaddleButtonPrevious.js";
import { PaddleNavContent } from "./PaddleNavContent.js";
import { resolvePaddleNavCornerOrientation } from "./utils/resolvePaddleNavCornerOrientation.js";
import { Children, forwardRef, isValidElement, useCallback, useMemo, useState } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
//#region src/components/client/PaddleNav/PaddleNav.tsx
function isPaddleButtonPrevious(child) {
	return child.type === PaddleButtonPrevious || hasDisplayName(child, "PaddleButtonPrevious");
}
function isPaddleButtonNext(child) {
	return child.type === PaddleButtonNext || hasDisplayName(child, "PaddleButtonNext");
}
function partitionChildren(children) {
	const contentNodes = [];
	let previousPaddle;
	let nextPaddle;
	const passthroughNodes = [];
	Children.forEach(children, (child) => {
		if (!isValidElement(child)) {
			passthroughNodes.push(child);
			return;
		}
		if (isPaddleButtonPrevious(child)) {
			if (!previousPaddle) previousPaddle = child;
			return;
		}
		if (isPaddleButtonNext(child)) {
			if (!nextPaddle) nextPaddle = child;
			return;
		}
		if (child.type === PaddleNavContent || hasDisplayName(child, "PaddleNavContent")) {
			contentNodes.push(child);
			return;
		}
		passthroughNodes.push(child);
	});
	return {
		contentNodes,
		previousPaddle,
		nextPaddle,
		passthroughNodes
	};
}
function getPaddleWrapperClassName(positionClasses, hidden) {
	return cx(positionClasses, hidden && "pointer-events-none");
}
function getCornerInsetClasses(orientation, outsideContent) {
	const inside = !outsideContent;
	switch (orientation) {
		case "top-left": return inside ? "top-2 left-2" : "-top-12 left-0";
		case "top-right": return inside ? "top-2 right-2" : "-top-12 right-0";
		case "bottom-left": return inside ? "bottom-2 left-2" : "-bottom-12 left-0";
		case "bottom-right": return inside ? "bottom-2 right-2" : "-bottom-12 right-0";
		default: return "";
	}
}
const horizontalOverlayStartClass = "left-2 top-1/2 -translate-y-1/2";
const horizontalOverlayEndClass = "right-2 top-1/2 -translate-y-1/2";
/** Keeps prev/next paddles in [←][→] screen order inside a cluster when an ancestor sets dir="rtl". */
const paddleClusterClassName = "inline-flex flex-row gap-2 items-center pointer-events-auto";
const PaddleNav = forwardRef(function PaddleNav({ children, orientation = "horizontal", outsideContent = false, variant = "primary", size = "md", className, ...rest }, ref) {
	const isRtl = useRtl();
	const [paddleHiddenByRole, setPaddleHiddenByRole] = useState(defaultPaddleHiddenByRole);
	const registerPaddleHidden = useCallback((role, hidden) => {
		setPaddleHiddenByRole((current) => {
			if (current[role] === hidden) return current;
			return {
				...current,
				[role]: hidden
			};
		});
	}, []);
	const paddleNavContextValue = useMemo(() => ({
		orientation,
		outsideContent,
		variant,
		size,
		paddleHiddenByRole,
		registerPaddleHidden
	}), [
		orientation,
		outsideContent,
		variant,
		size,
		paddleHiddenByRole,
		registerPaddleHidden
	]);
	const { contentNodes, previousPaddle, nextPaddle, passthroughNodes } = partitionChildren(children);
	const isVertical = orientation === "vertical";
	const isHorizontalCenter = orientation === "horizontal";
	const isCenter = isVertical || isHorizontalCenter;
	const isCorner = !isCenter;
	const resolvedOrientation = resolvePaddleNavCornerOrientation(orientation, isCorner ? isRtl : false);
	const physicalCornerOrientation = isCorner && resolvedOrientation !== "horizontal" && resolvedOrientation !== "vertical" ? resolvedOrientation : void 0;
	if (!(contentNodes.length > 0)) return /* @__PURE__ */ jsx(PaddleNavContext.Provider, {
		value: paddleNavContextValue,
		children: /* @__PURE__ */ jsxs(Box, {
			ref,
			dir: isVertical ? void 0 : "ltr",
			className: cx(paddleClusterClassName, isVertical && "flex-col", className),
			"data-paddle-nav-orientation": orientation,
			"data-paddle-nav-outside": outsideContent ? "true" : "false",
			...rest,
			children: [
				previousPaddle,
				nextPaddle,
				passthroughNodes
			]
		})
	});
	if (isCenter) {
		if (outsideContent) return /* @__PURE__ */ jsx(PaddleNavContext.Provider, {
			value: paddleNavContextValue,
			children: /* @__PURE__ */ jsxs(Box, {
				ref,
				dir: isHorizontalCenter ? "ltr" : void 0,
				display: "inline-flex",
				alignItems: "center",
				justifyContent: "center",
				flexDirection: isVertical ? "column" : "row",
				columnGap: isVertical ? void 0 : "2",
				rowGap: isVertical ? "2" : void 0,
				className,
				"data-paddle-nav-orientation": orientation,
				"data-paddle-nav-outside": "true",
				...rest,
				children: [
					previousPaddle,
					/* @__PURE__ */ jsx("div", {
						className: cx("relative min-w-0 min-h-0 flex-1"),
						children: contentNodes
					}),
					nextPaddle,
					passthroughNodes
				]
			})
		});
		return /* @__PURE__ */ jsx(PaddleNavContext.Provider, {
			value: paddleNavContextValue,
			children: /* @__PURE__ */ jsxs("div", {
				ref,
				className: cx("relative inline-block", className),
				"data-paddle-nav-orientation": orientation,
				"data-paddle-nav-outside": "false",
				...rest,
				children: [
					contentNodes,
					previousPaddle && /* @__PURE__ */ jsx("div", {
						className: getPaddleWrapperClassName(cx("absolute z-10 pointer-events-auto", isVertical ? "top-2 left-1/2 -translate-x-1/2" : horizontalOverlayStartClass), paddleHiddenByRole.previous),
						children: previousPaddle
					}),
					nextPaddle && /* @__PURE__ */ jsx("div", {
						className: getPaddleWrapperClassName(cx("absolute z-10 pointer-events-auto", isVertical ? "bottom-2 left-1/2 -translate-x-1/2" : horizontalOverlayEndClass), paddleHiddenByRole.next),
						children: nextPaddle
					}),
					passthroughNodes
				]
			})
		});
	}
	return /* @__PURE__ */ jsx(PaddleNavContext.Provider, {
		value: paddleNavContextValue,
		children: /* @__PURE__ */ jsxs("div", {
			ref,
			className: cx("relative inline-block", className),
			"data-paddle-nav-orientation": orientation,
			"data-paddle-nav-outside": outsideContent ? "true" : "false",
			...rest,
			children: [
				contentNodes,
				(previousPaddle || nextPaddle) && physicalCornerOrientation && /* @__PURE__ */ jsxs("div", {
					dir: "ltr",
					className: cx("absolute z-10", paddleClusterClassName, getCornerInsetClasses(physicalCornerOrientation, outsideContent)),
					children: [previousPaddle, nextPaddle]
				}),
				passthroughNodes
			]
		})
	});
});
PaddleNav.displayName = "PaddleNav";
//#endregion
export { PaddleNav };
