"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 UiStateStorage
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.UiStateStorageHandler = exports.UiStateStorageContext = exports.useUiStateStorageHandler = void 0;
const React = require("react");
const core_react_1 = require("@itwin/core-react");
const UiFramework_1 = require("../UiFramework");
const SyncUiEventDispatcher_1 = require("../syncui/SyncUiEventDispatcher");
/** @public */
function useUiStateStorageHandler() {
    return React.useContext(exports.UiStateStorageContext);
}
exports.useUiStateStorageHandler = useUiStateStorageHandler;
/** @internal */
exports.UiStateStorageContext = React.createContext(new core_react_1.LocalStateStorage());
exports.UiStateStorageContext.displayName = "uifw:UiStateStorageContext";
/** Allows to provide a custom [[UiStateStorage]] implementation to persist UI settings
 * through [[UiFramework.setUiStateStorage]].
 *
 * This component should wrap the [[ConfigurableUiContent]].
 *
 * ```tsx
 *  <UiStateStorageHandler>
 *    <ConfigurableUiContent />
 *  </UiStateStorageHandler>
 * ```
 * @public
 */
function UiStateStorageHandler(props) {
    const [stateStorage, setStateStorage] = React.useState(UiFramework_1.UiFramework.getUiStateStorage());
    React.useEffect(() => {
        // eslint-disable-next-line deprecation/deprecation
        const handleSyncUiEvent = (args) => {
            if (SyncUiEventDispatcher_1.SyncUiEventDispatcher.hasEventOfInterest(args.eventIds, [
                SyncUiEventDispatcher_1.SyncUiEventId.UiStateStorageChanged,
            ]))
                setStateStorage(UiFramework_1.UiFramework.getUiStateStorage());
        };
        return SyncUiEventDispatcher_1.SyncUiEventDispatcher.onSyncUiEvent.addListener(handleSyncUiEvent);
    });
    return (React.createElement(exports.UiStateStorageContext.Provider, { children: props.children, value: stateStorage }));
}
exports.UiStateStorageHandler = UiStateStorageHandler;
//# sourceMappingURL=useUiStateStorage.js.map