/**
 * 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.
 */

const require_runtime = require('../../_virtual/_rolldown/runtime.js');
const require_usePresence = require('./usePresence.js');
let react = require("react");

//#region src/components/Tearsheet/usePresenceContext.ts
/**
* Copyright IBM Corp. 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* Returns if the presence node is present and the context value to be used by a presence context, e.g. ModalPresence.
*/
const usePresenceContext = (open, initialPresenceId) => {
	const presenceIdRef = (0, react.useRef)(initialPresenceId);
	const presenceRef = (0, react.useRef)(null);
	const prevPresenceRef = (0, react.useRef)(null);
	if (!initialPresenceId && prevPresenceRef.current && !presenceRef.current) presenceIdRef.current = null;
	prevPresenceRef.current = presenceRef.current;
	const { isPresent, isExiting } = require_usePresence.usePresence(presenceRef, open);
	const isPresenceExclusive = (0, react.useCallback)((id) => {
		if (!id) return false;
		if (presenceIdRef.current && presenceIdRef.current !== id) return false;
		presenceIdRef.current = id;
		return true;
	}, []);
	return [isPresent, (0, react.useMemo)(() => ({
		presenceRef,
		isPresenceExclusive,
		isExiting
	}), [
		presenceRef,
		isPresenceExclusive,
		isExiting
	])];
};

//#endregion
exports.usePresenceContext = usePresenceContext;