import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { DEFAULT_SECONDARY_DISPLAY_SETTINGS, SecondaryScreenSettingsModal, getStoredSecondaryDisplaySettings, hasPersistedSecondaryDisplaySettings, persistSecondaryDisplaySettings } from "./ScaleModal.js";
import React, { useEffect, useMemo, useRef, useState } from "react";
import { useMemoizedFn } from "ahooks";
//#region src/components/ticketBooking/components/ticketBooking/useTicketBookingSecondaryScreen.tsx
const MAIN_CONTENT_TAP_WINDOW_MS = 800;
const MAIN_CONTENT_TAP_COUNT = 5;
/**
* 票务预约副屏：角色判断、主内容区 transform、连点打开设置弹窗、本地持久化展示配置。
*/
function useTicketBookingSecondaryScreen(runtime, displayConfig) {
	const isSecondaryScreen = useMemo(() => (runtime === null || runtime === void 0 ? void 0 : runtime.role) === "secondary-screen", [runtime]);
	const [secondaryDisplaySettings, setSecondaryDisplaySettings] = useState(() => (runtime === null || runtime === void 0 ? void 0 : runtime.role) === "secondary-screen" ? getStoredSecondaryDisplaySettings(displayConfig) : DEFAULT_SECONDARY_DISPLAY_SETTINGS);
	const [settingsModalOpen, setSettingsModalOpen] = useState(false);
	/**
	* 主屏：不读副屏 localStorage，固定默认配置并关闭设置弹窗。
	* 副屏：已持久化则只读存储；否则存储迁移 + props 兜底，且随 props 更新。
	*/
	useEffect(() => {
		if (!isSecondaryScreen) {
			setSecondaryDisplaySettings(DEFAULT_SECONDARY_DISPLAY_SETTINGS);
			setSettingsModalOpen(false);
			return;
		}
		if (hasPersistedSecondaryDisplaySettings()) {
			setSecondaryDisplaySettings(getStoredSecondaryDisplaySettings());
			return;
		}
		setSecondaryDisplaySettings(getStoredSecondaryDisplaySettings(displayConfig));
	}, [
		isSecondaryScreen,
		displayConfig === null || displayConfig === void 0 ? void 0 : displayConfig.productsPerRow,
		displayConfig === null || displayConfig === void 0 ? void 0 : displayConfig.showCustomerInfo
	]);
	const secondaryScaleTapRef = useRef({
		count: 0,
		resetTimer: null
	});
	useEffect(() => {
		return () => {
			const { resetTimer } = secondaryScaleTapRef.current;
			if (resetTimer) clearTimeout(resetTimer);
		};
	}, []);
	const secondaryProductsPerRow = useMemo(() => {
		if (!isSecondaryScreen) return 1;
		return secondaryDisplaySettings.productsPerRow;
	}, [isSecondaryScreen, secondaryDisplaySettings.productsPerRow]);
	const showSecondaryCustomerInfo = useMemo(() => {
		if (!isSecondaryScreen) return true;
		return secondaryDisplaySettings.showCustomerInfo;
	}, [isSecondaryScreen, secondaryDisplaySettings.showCustomerInfo]);
	const bookingInfoModules = useMemo(() => ({
		client: isSecondaryScreen && !showSecondaryCustomerInfo ? 0 : 1,
		date: 0,
		service: 1,
		holder: 1,
		note: 1
	}), [isSecondaryScreen, showSecondaryCustomerInfo]);
	const transformStyle = useMemo(() => {
		if (!isSecondaryScreen) return { width: 400 };
		const scale = secondaryDisplaySettings.scale;
		return {
			flex: ` 0 0 ${100 / scale}%`,
			height: `${100 / scale}%`,
			transform: `scale(${scale})`,
			transformOrigin: "top left"
		};
	}, [isSecondaryScreen, secondaryDisplaySettings.scale]);
	const handleMainContentClick = useMemoizedFn(() => {
		if (!isSecondaryScreen) return;
		const box = secondaryScaleTapRef.current;
		if (box.resetTimer) {
			clearTimeout(box.resetTimer);
			box.resetTimer = null;
		}
		box.count += 1;
		if (box.count >= MAIN_CONTENT_TAP_COUNT) {
			box.count = 0;
			setSettingsModalOpen(true);
			return;
		}
		box.resetTimer = setTimeout(() => {
			box.count = 0;
			box.resetTimer = null;
		}, MAIN_CONTENT_TAP_WINDOW_MS);
	});
	const handleSettingsConfirm = useMemoizedFn((next) => {
		if (!isSecondaryScreen) return;
		setSecondaryDisplaySettings(next);
		persistSecondaryDisplaySettings(next);
		setSettingsModalOpen(false);
	});
	return {
		isSecondaryScreen,
		secondaryDisplaySettings,
		secondaryProductsPerRow,
		showSecondaryCustomerInfo,
		bookingInfoModules,
		mainContentWrapperProps: useMemo(() => ({
			id: "ticket-booking-main-content",
			className: "ticket-booking-main-content",
			style: _objectSpread2({ flexShrink: 0 }, transformStyle),
			onClick: handleMainContentClick
		}), [transformStyle, handleMainContentClick]),
		secondaryScreenSettingsModal: isSecondaryScreen ? /* @__PURE__ */ React.createElement(SecondaryScreenSettingsModal, {
			open: settingsModalOpen,
			settings: secondaryDisplaySettings,
			onCancel: () => setSettingsModalOpen(false),
			onConfirm: handleSettingsConfirm
		}) : null
	};
}
//#endregion
export { useTicketBookingSecondaryScreen };
