import { caseContextReducer as insightCaseContext } from '../../../features/case-context/case-context-slice.js';
import { loadReducerError } from '../../../utils/errors.js';
import { buildContext as buildCoreContext, } from '../../context/headless-context.js';
/**
 * @internal
 * @param additionalPropertiesFactory - The functions that the strict context will have.
 * @returns Two functions to create and mixin the strict context.
 */
export const generateStrictContextFunctions = (additionalPropertiesFactory) => {
    const mixinCustomContext = (engine, props, context) => {
        if (!loadContextReducers(engine)) {
            throw loadReducerError;
        }
        if (!enhanceContext(context, engine, additionalPropertiesFactory)) {
            throw loadReducerError;
        }
        for (const [key, value] of Object.entries(props.initialState ?? {})) {
            context.add(key, value);
        }
        return context;
    };
    const createCustomContext = (engine, props) => mixinCustomContext(engine, props, buildCoreContext(engine, props));
    return { createCustomContext, mixinCustomContext };
};
function enhanceContext(context, engine, additionalPropertiesFactory) {
    Object.assign(context, additionalPropertiesFactory(engine));
    return true;
}
function loadContextReducers(engine) {
    engine.addReducers({ insightCaseContext });
    return true;
}
