import React, { createContext, useReducer } from 'react';
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) => {
    // Action.type should be in the format: <operator> <prop>. Ex: set sharedId
    const operator = action.type.substr(0, action.type.indexOf(' '));
    const prop = action.type.substr(action.type.indexOf(' ') + 1);
    switch (operator) {
        case 'set':
            const newState = Object.assign(Object.assign({}, state), { [prop]: action.value });
            return newState;
        default:
            throw new Error('Unknown operator');
    }
};
/**
 * A Context component to wrap around a Page. Allows
 * sharing of state amongst sibling Widgets.
 * @returns
 */
const PageContextProvider = ({ children }) => {
    const [state, dispatch] = useReducer(reducer, initialState);
    return React.createElement(Provider, { value: { state, dispatch } }, children);
};
export { pageContext, PageContextProvider };
//# sourceMappingURL=PageContext.js.map