/*---------------------------------------------------------------------------------------------
 * 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 { LocalStateStorage } from "@itwin/core-react";
import { UiFramework } from "../UiFramework";
import { SyncUiEventDispatcher, SyncUiEventId, } from "../syncui/SyncUiEventDispatcher";
/** @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.
 * @public
 */
export function UiStateStorageHandler(props) {
    const [stateStorage, setStateStorage] = React.useState(UiFramework.getUiStateStorage());
    React.useEffect(() => {
        // istanbul ignore next
        const handleSyncUiEvent = (args) => {
            if (SyncUiEventDispatcher.hasEventOfInterest(args.eventIds, [
                SyncUiEventId.UiStateStorageChanged,
            ]))
                setStateStorage(UiFramework.getUiStateStorage());
        };
        return SyncUiEventDispatcher.onSyncUiEvent.addListener(handleSyncUiEvent);
    });
    return (React.createElement(UiStateStorageContext.Provider, { children: props.children, value: stateStorage }));
}
//# sourceMappingURL=useUiStateStorage.js.map