import { c } from "react-compiler-runtime";
import React, { createContext, useReducer } from "react";
//#region src/contexts/PageContext.tsx
const initialState = {};
/**
* The Page context, shared amongst the Page
* and all children. Generally used to share state between
* Widgets. Call dispatch() to update the state. Currently
* only supports dispatch types of "set <state_property>".
* Example: dispatch({ type: "set mySharedProperty", value: oldSharedProperty + 1 });
*/
const pageContext = createContext({});
const { Provider } = pageContext;
/**
* Sets shared state based on an action type and a value.
* action.type is in the format: <operator> <prop>
* Currently the only supported action type is "set <prop>",
* which will set state.prop to action.value.
* @param state The current state
* @param action A type and value to alter the current state
* @returns The new state
*/
const reducer = (state, action) => {
	const operator = action.type.substr(0, action.type.indexOf(" "));
	const prop = action.type.substr(action.type.indexOf(" ") + 1);
	switch (operator) {
		case "set": return {
			...state,
			[prop]: action.value
		};
		default: throw new Error("Unknown operator");
	}
};
/**
* A Context component to wrap around a Page. Allows
* sharing of state amongst sibling Widgets.
* @returns
*/
const PageContextProvider = (t0) => {
	const $ = c(5);
	const { children } = t0;
	const [state, dispatch] = useReducer(reducer, initialState);
	let t1;
	if ($[0] !== state) {
		t1 = {
			state,
			dispatch
		};
		$[0] = state;
		$[1] = t1;
	} else t1 = $[1];
	let t2;
	if ($[2] !== children || $[3] !== t1) {
		t2 = /* @__PURE__ */ React.createElement(Provider, { value: t1 }, children);
		$[2] = children;
		$[3] = t1;
		$[4] = t2;
	} else t2 = $[4];
	return t2;
};
//#endregion
export { PageContextProvider, pageContext };

//# sourceMappingURL=PageContext.mjs.map