/**
 * Copyright IBM Corp. 2020, 2026
 *
 * This source code is licensed under the Apache-2.0 license found in the
 * LICENSE file in the root directory of this source tree.
 */

require("../../../_virtual/_rolldown/runtime.js");
const require_usePreviousValue = require("../../../global/js/hooks/usePreviousValue.js");
const require_handleMultipleKeys = require("../utils/handleMultipleKeys.js");
let react = require("react");
//#region src/components/DataSpreadsheet/hooks/useMultipleKeyTracking.js
/**
* Copyright IBM Corp. 2022, 2022
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
const hasFocus = () => typeof document !== "undefined" && document.hasFocus();
const useMultipleKeyTracking = ({ ref, containerHasFocus, isEditing }) => {
	const [usingMac, setUsingMac] = (0, react.useState)("");
	const [windowFocused, setWindowFocused] = (0, react.useState)(hasFocus);
	const [keysPressedList, setKeysPressedList] = (0, react.useState)([]);
	const previousState = require_usePreviousValue.usePreviousValue({
		isEditing,
		windowFocused,
		containerHasFocus
	});
	(0, react.useEffect)(() => {
		if (window.navigator.userAgent.includes("Macintosh")) setUsingMac(true);
		else setUsingMac(false);
		setWindowFocused(hasFocus());
		const onWindowFocus = () => setWindowFocused(true);
		const onWindowBlur = () => setWindowFocused(false);
		window.addEventListener("focus", onWindowFocus);
		window.addEventListener("blur", onWindowBlur);
		return () => {
			window.removeEventListener("focus", onWindowFocus);
			window.removeEventListener("blur", onWindowBlur);
		};
	}, []);
	(0, react.useEffect)(() => {
		if (ref && containerHasFocus && !isEditing) ref.current.onkeydown = ref.current.onkeyup = (event) => {
			if (event.type === "keydown") {
				if (keysPressedList.includes(event.code)) return;
				if (require_handleMultipleKeys.includesResourceKey(keysPressedList, usingMac) && keysPressedList.length > 1) {
					const filteredClonedKeys = [...keysPressedList].filter((item) => item === "MetaLeft" || item === "MetaRight");
					filteredClonedKeys.push(event.code);
					return setKeysPressedList([...new Set(filteredClonedKeys)]);
				}
				const tempKeys = [...keysPressedList];
				tempKeys.push(event.code);
				setKeysPressedList([...new Set(tempKeys)]);
			}
			if (event.type === "keyup") {
				const filteredClone = [...keysPressedList].filter((item) => item !== event.code);
				if (event.code === "MetaLeft" || event.code === "MetaRight") return setKeysPressedList([]);
				setKeysPressedList([...new Set(filteredClone)]);
			}
		};
		if (previousState?.windowFocused && !windowFocused) setKeysPressedList([]);
		if (ref && !containerHasFocus || isEditing) {
			ref.current.onkeydown = void 0;
			ref.current.onkeyup = void 0;
			if (!previousState?.isEditing && isEditing || previousState?.containerHasFocus && !containerHasFocus) setKeysPressedList([]);
		}
	}, [
		keysPressedList,
		containerHasFocus,
		ref,
		isEditing,
		previousState?.isEditing,
		previousState?.containerHasFocus,
		windowFocused,
		previousState?.windowFocused,
		usingMac
	]);
	return {
		keysPressedList,
		windowFocused,
		usingMac
	};
};
//#endregion
exports.useMultipleKeyTracking = useMultipleKeyTracking;
