/*---------------------------------------------------------------------------------------------
 * 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
 */
import * as React from "react";
import { UiFramework } from "../UiFramework.js";
import { SyncUiEventDispatcher, SyncUiEventId, } from "../syncui/SyncUiEventDispatcher.js";
import { LocalStateStorage } from "./LocalStateStorage.js";
/** @public */
export function useUiStateStorageHandler() {
    return React.useContext(UiStateStorageContext);
}
/** @internal */
export const UiStateStorageContext = React.createContext(new LocalStateStorage());
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
 */
// eslint-disable-next-line @typescript-eslint/no-deprecated
export function UiStateStorageHandler(props) {
    const [stateStorage, setStateStorage] = React.useState(UiFramework.getUiStateStorage());
    React.useEffect(() => {
        return SyncUiEventDispatcher.onSyncUiEvent.addListener((args) => {
            if (SyncUiEventDispatcher.hasEventOfInterest(args.eventIds, [
                SyncUiEventId.UiStateStorageChanged,
            ]))
                setStateStorage(UiFramework.getUiStateStorage());
        });
    });
    return (React.createElement(UiStateStorageContext.Provider, { children: props.children, value: stateStorage }));
}
//# sourceMappingURL=useUiStateStorage.js.map