"use strict";
/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module State
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReducerRegistryInstance = exports.ReducerRegistry = void 0;
const appui_abstract_1 = require("@itwin/appui-abstract");
const UiFramework_1 = require("../UiFramework");
/** Redux Reducer Registry.
 * Follows the example at http://nicolasgallagher.com/redux-modules-and-code-splitting/
 * Allows for small modules to provide their own reducers so that the they can manage their own state
 * @public
 * @deprecated in 4.15.0. Use your preferred state management library instead.
 */
class ReducerRegistry {
    /** ReducerRegistry constructor that initializes an empty reducer map to be populated by called to registerReducer. */
    constructor() {
        this._reducers = {};
    }
    /** Call to register a reducer and its name. */
    registerReducer(name, reducer) {
        if (this._reducers[name]) {
            // eslint-disable-next-line deprecation/deprecation
            throw new appui_abstract_1.UiError(UiFramework_1.UiFramework.loggerCategory(this), `Redux Reducer with matching name of '${name}' is already registered`);
        }
        this._reducers = { ...this._reducers, [name]: reducer };
        if (this._onReducerListChanged) {
            this._onReducerListChanged(this._reducers);
        }
    }
    /** Supports a single listener which should be the Redux store, see [[StoreManager]].
     * @internal
     */
    setChangeListener(listener) {
        this._onReducerListChanged = listener;
    }
    /** Returns map of registered Reducers. */
    getReducers() {
        return this._reducers;
    }
    /** Clear reducers only required for unit testing
     * @internal
     */
    clearReducers() {
        this._reducers = {};
        if (this._onReducerListChanged) {
            this._onReducerListChanged(this._reducers);
        }
    }
}
exports.ReducerRegistry = ReducerRegistry;
/** ReducerRegistryInstance singleton instance of Reducer Registry
 * @beta
 * @deprecated in 4.15.0. Use your preferred state management library instead.
 */
exports.ReducerRegistryInstance = new ReducerRegistry();
//# sourceMappingURL=ReducerRegistry.js.map