const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
let dayjs = require("dayjs");
dayjs = require_runtime.__toESM(dayjs);
let react = require("react");
//#region src/plus/pisellReservation/hooks/useReservationWallClockFollow.ts
/**
* 顶栏「跟随当前时间」：在每自然分钟整点（*: * :00）对齐日程，`at` 使用当前分钟的 `startOf('minute')`，
* 并通过 flushCommittedSchedule 触发防抖链。
*
* @param wallClockFollowLocked 为 true 时（如大屏锁定 resourceWall）：定时器始终运行，且不可通过交互关闭跟随。
*/
function useReservationWallClockFollow(scheduleValueProp, setInternalSchedule, flushCommittedSchedule, onScheduleChange, wallClockFollowLocked = false) {
	const [followWallClock, setFollowWallClockInternal] = (0, react.useState)(true);
	const onScheduleChangeRef = (0, react.useRef)(onScheduleChange);
	/** 避免 effect 依赖整个 `scheduleValueProp` 引用：父级每次 render 新对象会误重置定时器并多次首帧 tick */
	const scheduleValuePropRef = (0, react.useRef)(scheduleValueProp);
	scheduleValuePropRef.current = scheduleValueProp;
	const scheduleControlled = scheduleValueProp !== void 0;
	const followWallClockEffective = wallClockFollowLocked || followWallClock;
	/** 锁定模式下始终保持内部开关为 true，避免闭包里读到 false */
	(0, react.useEffect)(() => {
		if (wallClockFollowLocked) setFollowWallClockInternal(true);
	}, [wallClockFollowLocked]);
	(0, react.useEffect)(() => {
		onScheduleChangeRef.current = onScheduleChange;
	});
	(0, react.useEffect)(() => {
		if (!followWallClockEffective) return;
		let timeoutId = null;
		let cancelled = false;
		/** 距下一自然分钟整点的毫秒数（下一分钟的 :00.000） */
		const msUntilNextMinuteBoundary = () => {
			const now = (0, dayjs.default)();
			return Math.max(0, now.startOf("minute").add(1, "minute").diff(now));
		};
		const tick = () => {
			var _onScheduleChangeRef$;
			const now = (0, dayjs.default)().startOf("minute");
			const next = {
				date: now.startOf("day"),
				at: now
			};
			if (scheduleValuePropRef.current === void 0) setInternalSchedule(next);
			(_onScheduleChangeRef$ = onScheduleChangeRef.current) === null || _onScheduleChangeRef$ === void 0 || _onScheduleChangeRef$.call(onScheduleChangeRef, next);
			/** 跟随当前：立即提交日程，patch / 外部 onSearch 由 debouncedSchedule effect 统一处理 */
			flushCommittedSchedule(next);
		};
		const scheduleNextBoundary = () => {
			if (cancelled) return;
			timeoutId = window.setTimeout(() => {
				if (cancelled) return;
				tick();
				scheduleNextBoundary();
			}, msUntilNextMinuteBoundary());
		};
		/** 首帧：立刻对齐到当前整分；之后仅在每分钟 rollover 时再 tick */
		tick();
		scheduleNextBoundary();
		return () => {
			cancelled = true;
			if (timeoutId != null) window.clearTimeout(timeoutId);
		};
	}, [
		followWallClockEffective,
		flushCommittedSchedule,
		scheduleControlled
	]);
	return {
		followWallClock: followWallClockEffective,
		setFollowWallClock: (0, react.useCallback)((value) => {
			if (wallClockFollowLocked) return;
			setFollowWallClockInternal(value);
		}, [wallClockFollowLocked]),
		onFollowWallClockToggle: (0, react.useCallback)(() => {
			if (wallClockFollowLocked) return;
			setFollowWallClockInternal((v) => !v);
		}, [wallClockFollowLocked]),
		onTimeNavigatorUserInteraction: (0, react.useCallback)(() => {
			if (wallClockFollowLocked) return;
			setFollowWallClockInternal(false);
		}, [wallClockFollowLocked])
	};
}
//#endregion
exports.useReservationWallClockFollow = useReservationWallClockFollow;
