import React, { useCallback } from "react";
//#region src/components/pisellTimeNavigator/components/GestureLayer.tsx
/**
* 交互层：点击刻度跳转、键盘左右/上下 + 回车
*/
function GestureLayer({ orientation, cellSizePx, axisStartMarginPx, offsetToTime, offsetToTimeContinuous, onSelectTime, isInDisabledRanges, snapToStep, stepMinutes, snapFn, scrollPosition, viewportSize, onPointerDown, onPointerMove, onPointerUp, onPointerLeave, onUserInteraction, children }) {
	const isHorizontal = orientation === "horizontal";
	const handleClick = useCallback((e) => {
		onUserInteraction === null || onUserInteraction === void 0 || onUserInteraction();
		const viewport = e.currentTarget.parentElement;
		if (!viewport) return;
		const vRect = viewport.getBoundingClientRect();
		const scroll = isHorizontal ? viewport.scrollLeft : viewport.scrollTop;
		const scrollContentPos = isHorizontal ? scroll + (e.clientX - vRect.left) : scroll + (e.clientY - vRect.top);
		const offset = Math.max(0, scrollContentPos - axisStartMarginPx);
		/**
		* snapToStep=false：点击直接跳到指针对应的连续时间，避免被 offsetToTime
		* 内部的 Math.round 吸附到最近的 tick（会让点击在 6:50 落到 7:00 这种）；
		* snapToStep=true：维持原有「吸到最近 step 网格」语义。
		*/
		const t = snapToStep ? offsetToTime(offset) : offsetToTimeContinuous(offset);
		if (t == null) return;
		const candidate = snapToStep ? snapFn(t) : t;
		if (isInDisabledRanges(candidate)) return;
		onSelectTime(candidate);
	}, [
		isHorizontal,
		axisStartMarginPx,
		offsetToTime,
		offsetToTimeContinuous,
		onSelectTime,
		isInDisabledRanges,
		snapToStep,
		snapFn,
		onUserInteraction
	]);
	const handleKeyDown = useCallback((e) => {
		const scrollContentCenter = orientation === "horizontal" ? scrollPosition.scrollLeft + viewportSize.w / 2 : scrollPosition.scrollTop + viewportSize.h / 2;
		const centerOffset = Math.max(0, scrollContentCenter - axisStartMarginPx);
		const currentTime = snapToStep ? offsetToTime(centerOffset) : offsetToTimeContinuous(centerOffset);
		if (currentTime == null) return;
		if (e.key === "Enter") {
			const candidate = snapToStep ? snapFn(currentTime) : currentTime;
			if (!isInDisabledRanges(candidate)) {
				onUserInteraction === null || onUserInteraction === void 0 || onUserInteraction();
				onSelectTime(candidate);
			}
			e.preventDefault();
			return;
		}
		const step = stepMinutes * 60 * 1e3;
		let next;
		if (orientation === "horizontal") if (e.key === "ArrowLeft") next = currentTime.subtract(step, "ms");
		else if (e.key === "ArrowRight") next = currentTime.add(step, "ms");
		else return;
		else if (e.key === "ArrowUp") next = currentTime.subtract(step, "ms");
		else if (e.key === "ArrowDown") next = currentTime.add(step, "ms");
		else return;
		e.preventDefault();
		const snapped = snapToStep ? snapFn(next) : next;
		if (!isInDisabledRanges(snapped)) {
			onUserInteraction === null || onUserInteraction === void 0 || onUserInteraction();
			onSelectTime(snapped);
		}
	}, [
		orientation,
		axisStartMarginPx,
		offsetToTime,
		offsetToTimeContinuous,
		onSelectTime,
		isInDisabledRanges,
		snapToStep,
		snapFn,
		stepMinutes,
		scrollPosition.scrollLeft,
		scrollPosition.scrollTop,
		viewportSize.w,
		viewportSize.h,
		onUserInteraction
	]);
	const handlePointerDown = useCallback((e) => {
		onUserInteraction === null || onUserInteraction === void 0 || onUserInteraction();
		if (onPointerDown) {
			e.currentTarget.setPointerCapture(e.pointerId);
			onPointerDown(e);
		}
	}, [onPointerDown, onUserInteraction]);
	const handlePointerUp = useCallback((e) => {
		if (onPointerUp) {
			e.currentTarget.releasePointerCapture(e.pointerId);
			onPointerUp(e);
		}
	}, [onPointerUp]);
	return /* @__PURE__ */ React.createElement("div", {
		className: `pisell-time-navigator-gesture pisell-time-navigator-gesture--${orientation}`,
		onClick: handleClick,
		onKeyDown: handleKeyDown,
		onPointerDown: handlePointerDown,
		onPointerMove,
		onPointerUp: handlePointerUp,
		onPointerLeave,
		role: "slider",
		tabIndex: 0,
		"aria-label": "Time"
	}, children);
}
//#endregion
export { GestureLayer };
