import { createContext, useContext, useEffect, useRef, useState } from "react";
import { jsx } from "react/jsx-runtime";
//#region ../react/src/shell/ScreenHeaderContext.tsx
/**
* Split into separate read/write contexts so that components calling
* useScreenHeaderActions (writers) don't re-render when the actions
* state changes — only the ScreenHeader (reader) re-renders.
*/
const ScreenHeaderWriteContext = createContext(null);
const ScreenHeaderReadContext = createContext(null);
const ScreenHeaderBreadcrumbsWriteContext = createContext(null);
const ScreenHeaderBreadcrumbsReadContext = createContext(null);
function ScreenHeaderProvider({ children }) {
	const [actions, setActions] = useState(null);
	const [breadcrumbs, setBreadcrumbs] = useState(null);
	return /* @__PURE__ */ jsx(ScreenHeaderWriteContext.Provider, {
		value: setActions,
		children: /* @__PURE__ */ jsx(ScreenHeaderReadContext.Provider, {
			value: actions,
			children: /* @__PURE__ */ jsx(ScreenHeaderBreadcrumbsWriteContext.Provider, {
				value: setBreadcrumbs,
				children: /* @__PURE__ */ jsx(ScreenHeaderBreadcrumbsReadContext.Provider, {
					value: breadcrumbs,
					children
				})
			})
		})
	});
}
/**
* Sets actions to display in the ScreenHeader.
* Returns `true` if inside a ScreenHeaderProvider (i.e., in the portal shell),
* `false` otherwise. Pages can use this boolean to conditionally skip
* rendering their own PageHeader.
*
* Cleans up actions on unmount so navigating away doesn't leave stale actions.
*/
function useScreenHeaderActions(actions) {
	const setActions = useContext(ScreenHeaderWriteContext);
	const setActionsRef = useRef(setActions);
	setActionsRef.current = setActions;
	useEffect(() => {
		if (!setActions) return;
		setActions(actions);
	}, [actions, setActions]);
	useEffect(() => {
		return () => setActionsRef.current?.(null);
	}, []);
	return setActions !== null;
}
/**
* Sets breadcrumbs to display in the ScreenHeader (in place of the plain title).
* Returns `true` if inside a ScreenHeaderProvider (i.e., in the portal shell),
* `false` otherwise.
*
* Cleans up breadcrumbs on unmount so navigating away doesn't leave stale breadcrumbs.
*/
function useScreenHeaderBreadcrumbs(breadcrumbs) {
	const setBreadcrumbs = useContext(ScreenHeaderBreadcrumbsWriteContext);
	const setBreadcrumbsRef = useRef(setBreadcrumbs);
	setBreadcrumbsRef.current = setBreadcrumbs;
	useEffect(() => {
		if (!setBreadcrumbs) return;
		setBreadcrumbs(breadcrumbs);
	}, [breadcrumbs, setBreadcrumbs]);
	useEffect(() => {
		return () => setBreadcrumbsRef.current?.(null);
	}, []);
	return setBreadcrumbs !== null;
}
/**
* Reads the current screen header actions and breadcrumbs. Used internally by ScreenHeader.
*/
function useScreenHeaderContext() {
	const actions = useContext(ScreenHeaderReadContext);
	const breadcrumbs = useContext(ScreenHeaderBreadcrumbsReadContext);
	return {
		actions: actions ?? null,
		breadcrumbs: breadcrumbs ?? null
	};
}
//#endregion
export { useScreenHeaderContext as i, useScreenHeaderActions as n, useScreenHeaderBreadcrumbs as r, ScreenHeaderProvider as t };

//# sourceMappingURL=ScreenHeaderContext-kG_zh0zW.mjs.map