/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module Frontstage
 */
import * as React from "react";
import { useUiStateStorageHandler } from "../uistate/useUiStateStorage.js";
import { FRONTSTAGE_SETTINGS_NAMESPACE, getFrontstageStateSettingName, packNineZoneState, stateVersion, } from "./Frontstage.js";
import debounce from "lodash/debounce.js";
/** @internal */
export function useSaveFrontstageSettings(frontstageDef, store) {
    const uiSettingsStorage = useUiStateStorageHandler();
    const saveSettingRef = React.useRef(undefined);
    const save = React.useCallback(async (frontstage, state) => {
        if (!saveSettingRef.current)
            return;
        if (state.draggedTab)
            return;
        return saveSettingRef.current(frontstage, state);
    }, []);
    React.useEffect(() => {
        const saveSetting = (saveSettingRef.current = debounce(async (frontstage, state) => {
            const id = frontstage.id;
            const setting = {
                id,
                version: frontstage.version,
                stateVersion,
                nineZone: packNineZoneState(state),
            };
            await uiSettingsStorage.saveSetting(FRONTSTAGE_SETTINGS_NAMESPACE, getFrontstageStateSettingName(id), setting);
        }, 1000));
        return () => {
            saveSettingRef.current = undefined;
            saveSetting.cancel();
        };
    }, [uiSettingsStorage]);
    React.useEffect(() => {
        return () => {
            // Save pending frontstage changes when changing frontstage.
            void saveSettingRef.current?.flush();
        };
    }, [frontstageDef]);
    React.useEffect(() => {
        return store.subscribe(() => {
            void save(frontstageDef, store.getState());
        });
    }, [save, frontstageDef, store]);
}
//# sourceMappingURL=useSaveFrontstageSettings.js.map