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

import { usePresenceContext } from "./usePresenceContext.js";
import React, { createContext, useContext, useMemo } from "react";
//#region src/components/Tearsheet/TearsheetPresence.tsx
/**
* Copyright IBM Corp. 2016, 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.
*/
const TearsheetPresence = ({ open, _presenceId: presenceId, _autoEnablePresence: autoEnablePresence = true, children }) => {
	const [isPresent, context] = usePresenceContext(open, presenceId);
	const contextValue = useMemo(() => ({
		autoEnablePresence,
		...context
	}), [autoEnablePresence, context]);
	if (!isPresent) return null;
	return /* @__PURE__ */ React.createElement(TearsheetPresenceContext.Provider, { value: contextValue }, children);
};
const TearsheetPresenceContext = createContext(void 0);
/**
* Handles occurrences where only a single modal must consume a context.
*/
const useExclusiveTearsheetPresenceContext = (id) => {
	const ctx = useContext(TearsheetPresenceContext);
	return ctx?.isPresenceExclusive(id) ? ctx : void 0;
};
/**
* Higher-order function that wraps a component with ModalPresence
*/
const withTearsheetPresence = (Component) => {
	const WithModalPresence = (props) => {
		const { open, ...componentProps } = props;
		return /* @__PURE__ */ React.createElement(TearsheetPresence, { open }, /* @__PURE__ */ React.createElement(Component, componentProps));
	};
	WithModalPresence.displayName = `withModalPresence(${Component.displayName || Component.name || "Component"})`;
	return WithModalPresence;
};
//#endregion
export { TearsheetPresence, TearsheetPresenceContext, useExclusiveTearsheetPresenceContext, withTearsheetPresence };
