import { Arr } from '@ephox/katamari';
import * as AlloyEvents from '../../api/events/AlloyEvents';
import * as SystemEvents from '../../api/events/SystemEvents';
import { withoutReuse, withReuse } from '../replacing/ReplacingAll';
const events = (reflectingConfig, reflectingState) => {
    const update = (component, data) => {
        reflectingConfig.updateState.each((updateState) => {
            const newState = updateState(component, data);
            reflectingState.set(newState);
        });
        // FIX: Partial duplication of Replacing + Receiving
        reflectingConfig.renderComponents.each((renderComponents) => {
            const newComponents = renderComponents(data, reflectingState.get());
            const replacer = reflectingConfig.reuseDom ? withReuse : withoutReuse;
            replacer(component, newComponents);
        });
    };
    return AlloyEvents.derive([
        AlloyEvents.run(SystemEvents.receive(), (component, message) => {
            // NOTE: Receiving event ignores the whole simulated event part.
            // TODO: Think about the types for this, or find a better way for this to rely on receiving.
            const receivingData = message;
            if (!receivingData.universal) {
                const channel = reflectingConfig.channel;
                if (Arr.contains(receivingData.channels, channel)) {
                    update(component, receivingData.data);
                }
            }
        }),
        AlloyEvents.runOnAttached((comp, _se) => {
            reflectingConfig.initialData.each((rawData) => {
                update(comp, rawData);
            });
        })
    ]);
};
export { events };
//# sourceMappingURL=ActiveReflecting.js.map