"use strict";
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/* eslint-disable deprecation/deprecation */
Object.defineProperty(exports, "__esModule", { value: true });
exports.useItemsManager = exports.useFrontstageManager = exports.getFrontstageStateSettingName = exports.FRONTSTAGE_SETTINGS_NAMESPACE = exports.useSaveFrontstageSettings = exports.useSavedFrontstageState = exports.setWidgetLabel = exports.expandWidget = exports.showWidget = exports.setWidgetState = exports.isPanelCollapsed = exports.packNineZoneState = exports.restoreNineZoneState = exports.initializeNineZoneState = exports.initializePanel = exports.isFrontstageStateSettingResult = exports.addPanelWidgets = exports.getPanelZoneWidgets = exports.getWidgetId = exports.removeMissingWidgets = exports.addMissingWidgets = exports.appendWidgets = exports.addWidgets = exports.useLabels = exports.ActiveFrontstageDefProvider = exports.WidgetPanelsFrontstage = exports.useNineZoneDispatch = exports.FrameworkStateReducer = exports.useUpdateNineZoneSize = exports.useNineZoneState = void 0;
/** @packageDocumentation
 * @module Frontstage
 */
// cSpell:ignore popout
require("./Frontstage.scss");
const immer_1 = require("immer");
const React = require("react");
const core_bentley_1 = require("@itwin/core-bentley");
const appui_abstract_1 = require("@itwin/appui-abstract");
const core_react_1 = require("@itwin/core-react");
const components_react_1 = require("@itwin/components-react");
const appui_layout_react_1 = require("@itwin/appui-layout-react");
const Frontstage_1 = require("../frontstage/Frontstage");
const StagePanelDef_1 = require("../stagepanels/StagePanelDef");
const UiFramework_1 = require("../UiFramework");
const useUiStateStorage_1 = require("../uistate/useUiStateStorage");
const ZoneDef_1 = require("../zones/ZoneDef");
const Content_1 = require("./Content");
const FrontstageContent_1 = require("./FrontstageContent");
const ModalFrontstageComposer_1 = require("./ModalFrontstageComposer");
const StatusBar_1 = require("./StatusBar");
const Tab_1 = require("./Tab");
const Toolbars_1 = require("./Toolbars");
const ToolSettings_1 = require("./ToolSettings");
const useEscapeSetFocusToHome_1 = require("../hooks/useEscapeSetFocusToHome");
const react_redux_1 = require("react-redux");
const useUiVisibility_1 = require("../hooks/useUiVisibility");
const core_frontend_1 = require("@itwin/core-frontend");
const FloatingWidget_1 = require("./FloatingWidget");
const InternalFrontstageManager_1 = require("../frontstage/InternalFrontstageManager");
const panelZoneKeys = ["start", "end"];
const WidgetInternals = React.memo(function WidgetInternals() {
    const activeModalFrontstageInfo = (0, ModalFrontstageComposer_1.useActiveModalFrontstageInfo)();
    const uiIsVisible = (0, useUiVisibility_1.useUiVisibility)();
    return (React.createElement(React.Fragment, null,
        React.createElement(components_react_1.ToolbarPopupAutoHideContext.Provider, { value: !uiIsVisible },
            React.createElement(ModalFrontstageComposer_1.ModalFrontstageComposer, { stageInfo: activeModalFrontstageInfo }),
            React.createElement(appui_layout_react_1.WidgetPanels, { className: "uifw-widgetPanels", centerContent: React.createElement(Toolbars_1.WidgetPanelsToolbars, null) },
                React.createElement(FrontstageContent_1.WidgetPanelsFrontstageContent, null)),
            React.createElement(StatusBar_1.WidgetPanelsStatusBar, null),
            React.createElement(appui_layout_react_1.FloatingWidgets, null))));
});
// istanbul ignore next
const WidgetPanelsFrontstageComponent = React.memo(function WidgetPanelsFrontstageComponent() {
    return (React.createElement(React.Fragment, null,
        React.createElement(ToolSettings_1.WidgetPanelsToolSettings, null),
        React.createElement(WidgetInternals, null)));
});
const widgetContent = React.createElement(Content_1.WidgetContent, null);
const toolSettingsContent = React.createElement(ToolSettings_1.ToolSettingsContent, null);
const widgetPanelsFrontstage = React.createElement(WidgetPanelsFrontstageComponent, null);
/** @internal */
function useNineZoneState(frontstageDef) {
    const lastFrontstageDef = React.useRef(frontstageDef);
    const [nineZone, setNineZone] = React.useState(frontstageDef.nineZoneState);
    React.useEffect(() => {
        setNineZone(frontstageDef.nineZoneState);
        lastFrontstageDef.current = frontstageDef;
    }, [frontstageDef]);
    React.useEffect(() => {
        const listener = (args) => {
            if (args.frontstageDef !== frontstageDef || frontstageDef.isStageClosing || frontstageDef.isApplicationClosing)
                return;
            setNineZone(args.state);
        };
        return InternalFrontstageManager_1.InternalFrontstageManager.onFrontstageNineZoneStateChangedEvent.addListener(listener);
    }, [frontstageDef]);
    return lastFrontstageDef.current === frontstageDef ? nineZone : frontstageDef.nineZoneState;
}
exports.useNineZoneState = useNineZoneState;
/** @returns Defined NineZoneState with fallback to last defined and default NineZoneState.
 * @internal
 */
function useCachedNineZoneState(nineZone) {
    const cached = React.useRef(nineZone || defaultNineZone);
    React.useEffect(() => {
        if (nineZone)
            cached.current = nineZone;
    }, [nineZone]);
    return nineZone || cached.current;
}
/** Update in-memory NineZoneState of newly activated frontstage with up to date size.
 * @internal
 */
function useUpdateNineZoneSize(frontstageDef) {
    React.useEffect(() => {
        const size = InternalFrontstageManager_1.InternalFrontstageManager.nineZoneSize;
        let state = frontstageDef.nineZoneState;
        if (!size || !state)
            return;
        state = FrameworkStateReducer(state, {
            type: "RESIZE",
            size: {
                height: size.height,
                width: size.width,
            },
        }, frontstageDef);
        frontstageDef.nineZoneState = state;
    }, [frontstageDef]);
}
exports.useUpdateNineZoneSize = useUpdateNineZoneSize;
/** @internal */
function FrameworkStateReducer(state, action, frontstageDef) {
    state = (0, appui_layout_react_1.NineZoneStateReducer)(state, action);
    if (action.type === "RESIZE") {
        state = (0, immer_1.default)(state, (draft) => {
            for (const panelSide of appui_layout_react_1.panelSides) {
                const panel = draft.panels[panelSide];
                const location = toStagePanelLocation(panelSide);
                const panelDef = frontstageDef.getStagePanelDef(location);
                if (panelDef === null || panelDef === void 0 ? void 0 : panelDef.maxSizeSpec) {
                    panel.maxSize = getPanelMaxSize(panelDef.maxSizeSpec, panelSide, action.size);
                    if (panel.size) {
                        panel.size = Math.min(Math.max(panel.size, panel.minSize), panel.maxSize);
                    }
                }
            }
        });
    }
    return state;
}
exports.FrameworkStateReducer = FrameworkStateReducer;
/** @internal */
function useNineZoneDispatch(frontstageDef) {
    const dispatch = React.useCallback((action) => {
        if (action.type === "RESIZE") {
            InternalFrontstageManager_1.InternalFrontstageManager.nineZoneSize = core_react_1.Size.create(action.size);
        }
        // istanbul ignore if
        if (action.type === "TOOL_SETTINGS_DRAG_START") {
            // eslint-disable-next-line @typescript-eslint/no-floating-promises
            UiFramework_1.UiFramework.postTelemetry("Tool Settings Undocking", "28B04E07-AE73-4533-A0BA-8E2A8DC99ADF");
        }
        // istanbul ignore if
        if (action.type === "TOOL_SETTINGS_DOCK") {
            // eslint-disable-next-line @typescript-eslint/no-floating-promises
            UiFramework_1.UiFramework.postTelemetry("Tool Settings Docking to Settings Bar", "BEDE684B-B3DB-4637-B3AF-DC3CBA223F94");
        }
        if (action.type === "WIDGET_TAB_POPOUT") {
            const tabId = action.id;
            frontstageDef.popoutWidget(tabId);
            return;
        }
        const nineZoneState = frontstageDef.nineZoneState;
        if (!nineZoneState)
            return;
        frontstageDef.nineZoneState = FrameworkStateReducer(nineZoneState, action, frontstageDef);
    }, [frontstageDef]);
    return dispatch;
}
exports.useNineZoneDispatch = useNineZoneDispatch;
/** @internal */
exports.WidgetPanelsFrontstage = React.memo(function WidgetPanelsFrontstage() {
    const frontstageDef = (0, Frontstage_1.useActiveFrontstageDef)();
    React.useEffect(() => {
        const triggerWidowCloseProcessing = () => {
            frontstageDef && frontstageDef.setIsApplicationClosing(true);
        };
        frontstageDef && frontstageDef.setIsApplicationClosing(false);
        window.addEventListener("unload", triggerWidowCloseProcessing);
        return () => {
            window.removeEventListener("unload", triggerWidowCloseProcessing);
        };
    }, [frontstageDef]);
    if (!frontstageDef)
        return null;
    return (React.createElement(ActiveFrontstageDefProvider, { frontstageDef: frontstageDef }));
});
const defaultNineZone = (0, appui_layout_react_1.createNineZoneState)();
const tabElement = React.createElement(Tab_1.WidgetPanelsTab, null);
const floatingWidgetElement = React.createElement(FloatingWidget_1.FloatingWidget, null);
/** @internal */
function ActiveFrontstageDefProvider({ frontstageDef }) {
    let nineZone = useNineZoneState(frontstageDef);
    nineZone = useCachedNineZoneState(nineZone);
    const dispatch = useNineZoneDispatch(frontstageDef);
    useUpdateNineZoneSize(frontstageDef);
    useSavedFrontstageState(frontstageDef);
    useSaveFrontstageSettings(frontstageDef);
    useItemsManager(frontstageDef);
    const labels = useLabels();
    const uiIsVisible = (0, useUiVisibility_1.useUiVisibility)();
    const showWidgetIcon = (0, react_redux_1.useSelector)((state) => {
        const frameworkState = state[UiFramework_1.UiFramework.frameworkStateKey];
        return !!frameworkState.configurableUiState.showWidgetIcon;
    });
    const autoCollapseUnpinnedPanels = (0, react_redux_1.useSelector)((state) => {
        const frameworkState = state[UiFramework_1.UiFramework.frameworkStateKey];
        return !!frameworkState.configurableUiState.autoCollapseUnpinnedPanels;
    });
    const animateToolSettings = (0, react_redux_1.useSelector)((state) => {
        const frameworkState = state[UiFramework_1.UiFramework.frameworkStateKey];
        return !!frameworkState.configurableUiState.animateToolSettings;
    });
    const useToolAsToolSettingsLabel = (0, react_redux_1.useSelector)((state) => {
        const frameworkState = state[UiFramework_1.UiFramework.frameworkStateKey];
        return !!frameworkState.configurableUiState.useToolAsToolSettingsLabel;
    });
    useFrontstageManager(frontstageDef, useToolAsToolSettingsLabel);
    const handleKeyDown = (0, useEscapeSetFocusToHome_1.useEscapeSetFocusToHome)();
    return (React.createElement("div", { className: "uifw-widgetPanels-frontstage", onKeyDown: handleKeyDown, role: "presentation" },
        React.createElement(appui_layout_react_1.NineZone, { dispatch: dispatch, labels: labels, state: nineZone, tab: tabElement, floatingWidget: floatingWidgetElement, showWidgetIcon: showWidgetIcon, autoCollapseUnpinnedPanels: autoCollapseUnpinnedPanels, toolSettingsContent: toolSettingsContent, widgetContent: widgetContent, animateDockedToolSettings: animateToolSettings, uiIsVisible: uiIsVisible }, widgetPanelsFrontstage)));
}
exports.ActiveFrontstageDefProvider = ActiveFrontstageDefProvider;
/** @internal */
function useLabels() {
    return React.useMemo(() => ({
        dockToolSettingsTitle: UiFramework_1.UiFramework.translate("widget.tooltips.dockToolSettings"),
        moreWidgetsTitle: UiFramework_1.UiFramework.translate("widget.tooltips.moreWidgets"),
        moreToolSettingsTitle: UiFramework_1.UiFramework.translate("widget.tooltips.moreToolSettings"),
        pinPanelTitle: UiFramework_1.UiFramework.translate("widget.tooltips.pinPanel"),
        resizeGripTitle: UiFramework_1.UiFramework.translate("widget.tooltips.resizeGrip"),
        sendWidgetHomeTitle: UiFramework_1.UiFramework.translate("widget.tooltips.sendHome"),
        toolSettingsHandleTitle: UiFramework_1.UiFramework.translate("widget.tooltips.toolSettingsHandle"),
        unpinPanelTitle: UiFramework_1.UiFramework.translate("widget.tooltips.unpinPanel"),
        popoutActiveTab: UiFramework_1.UiFramework.translate("widget.tooltips.popoutActiveTab"),
    }), []);
}
exports.useLabels = useLabels;
/** @internal */
function addWidgets(state, widgets, side, widgetId) {
    const visibleWidgets = widgets.filter((w) => w.isVisible && (w.defaultState !== appui_abstract_1.WidgetState.Floating));
    if (visibleWidgets.length === 0)
        return state;
    const tabs = new Array();
    for (const widget of visibleWidgets) {
        const label = getWidgetLabel(widget.label);
        state = (0, appui_layout_react_1.addTab)(state, widget.id, {
            label,
            iconSpec: widget.iconSpec,
            preferredPanelWidgetSize: widget.preferredPanelSize,
            preferredFloatingWidgetSize: widget.defaultFloatingSize,
            canPopout: widget.canPopout,
            isFloatingStateWindowResizable: widget.isFloatingStateWindowResizable,
            hideWithUiWhenFloating: !!widget.hideWithUiWhenFloating,
            allowedPanelTargets: widget.allowedPanelTargets,
        });
        tabs.push(widget.id);
    }
    const activeWidget = visibleWidgets.find((widget) => widget.isActive);
    const minimized = false;
    // istanbul ignore else
    if ((activeWidget === null || activeWidget === void 0 ? void 0 : activeWidget.defaultState) !== appui_abstract_1.WidgetState.Floating) {
        state = (0, appui_layout_react_1.addPanelWidget)(state, side, widgetId, tabs, {
            activeTabId: activeWidget ? activeWidget.id : tabs[0],
            minimized,
        });
    }
    return state;
}
exports.addWidgets = addWidgets;
/** @internal */
// istanbul ignore next
function appendWidgets(state, widgetDefs, side, preferredWidgetIndex) {
    var _a;
    if (widgetDefs.length === 0)
        return state;
    // Add missing widget tabs.
    for (const widgetDef of widgetDefs) {
        const label = getWidgetLabel(widgetDef.label);
        const saveTab = state.tabs[widgetDef.id];
        const preferredPanelWidgetSize = saveTab ? saveTab.preferredPanelWidgetSize : widgetDef.preferredPanelSize;
        const preferredFloatingWidgetSize = saveTab ? saveTab.preferredFloatingWidgetSize : widgetDef.defaultFloatingSize;
        // if a defaultFloatingSize is specified then this mean the widget can't determine its intrinsic size and must be explicitly sized
        const userSized = saveTab ? (saveTab.userSized || !!widgetDef.defaultFloatingSize && !!saveTab.preferredFloatingWidgetSize)
            : !!widgetDef.defaultFloatingSize;
        state = (0, appui_layout_react_1.addTab)(state, widgetDef.id, {
            label,
            iconSpec: widgetDef.iconSpec,
            canPopout: widgetDef.canPopout,
            preferredPanelWidgetSize,
            preferredFloatingWidgetSize,
            userSized,
            isFloatingStateWindowResizable: widgetDef.isFloatingStateWindowResizable,
            hideWithUiWhenFloating: !!widgetDef.hideWithUiWhenFloating,
            allowedPanelTargets: widgetDef.allowedPanelTargets,
        });
        if (widgetDef.isFloatingStateSupported && widgetDef.defaultState === appui_abstract_1.WidgetState.Floating) {
            const floatingContainerId = (_a = widgetDef.floatingContainerId) !== null && _a !== void 0 ? _a : (0, appui_layout_react_1.getUniqueId)();
            const widgetContainerId = getWidgetId(side, panelZoneKeys[preferredWidgetIndex]);
            const home = { side, widgetId: widgetContainerId, widgetIndex: 0 };
            const preferredPosition = widgetDef.defaultFloatingPosition;
            if (floatingContainerId in state.widgets) {
                state = (0, appui_layout_react_1.addTabToWidget)(state, widgetDef.id, floatingContainerId);
            }
            else {
                const tab = state.tabs[widgetDef.id];
                const size = { height: 200, width: 300, ...tab.preferredFloatingWidgetSize };
                const preferredPoint = preferredPosition !== null && preferredPosition !== void 0 ? preferredPosition : { x: (state.size.width - size.width) / 2, y: (state.size.height - size.height) / 2 };
                const nzBounds = core_react_1.Rectangle.createFromSize(state.size);
                const bounds = core_react_1.Rectangle.createFromSize(size).offset(preferredPoint);
                const containedBounds = bounds.containIn(nzBounds);
                state = (0, appui_layout_react_1.addFloatingWidget)(state, floatingContainerId, [widgetDef.id], {
                    bounds: containedBounds.toProps(),
                    home,
                    userSized,
                }, {
                    isFloatingStateWindowResizable: widgetDef.isFloatingStateWindowResizable,
                });
            }
        }
        else {
            const widgetPanelSectionId = (0, appui_layout_react_1.getWidgetPanelSectionId)(side, preferredWidgetIndex);
            if (widgetPanelSectionId in state.widgets) {
                state = (0, appui_layout_react_1.addTabToWidget)(state, widgetDef.id, widgetPanelSectionId);
            }
            else {
                const panel = state.panels[side];
                if (panel.widgets.length < panel.maxWidgetCount) {
                    state = (0, appui_layout_react_1.addPanelWidget)(state, side, widgetPanelSectionId, [widgetDef.id]);
                }
                else {
                    const widgetIndex = Math.min(preferredWidgetIndex, panel.widgets.length - 1);
                    const widgetId = panel.widgets[widgetIndex];
                    state = (0, appui_layout_react_1.addTabToWidget)(state, widgetDef.id, widgetId);
                }
            }
        }
    }
    return state;
}
exports.appendWidgets = appendWidgets;
function processPopoutWidgets(initialState, frontstageDef) {
    // istanbul ignore next
    if (!initialState.popoutWidgets)
        return initialState;
    // istanbul ignore next - not unit testing electron case that reopens popout windows
    if (initialState.popoutWidgets && core_bentley_1.ProcessDetector.isElectronAppFrontend) {
        if (initialState.popoutWidgets.allIds.length && core_bentley_1.ProcessDetector.isElectronAppFrontend) {
            for (const widgetContainerId of initialState.popoutWidgets.allIds) {
                frontstageDef.openPopoutWidgetContainer(initialState, widgetContainerId);
            }
        }
        return initialState;
    }
    return (0, appui_layout_react_1.convertAllPopupWidgetContainersToFloating)(initialState);
}
/** Adds frontstageDef widgets that are missing in NineZoneState.
 * @internal
 */
function addMissingWidgets(frontstageDef, initialState) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;
    let state = initialState;
    state = appendWidgets(state, determineNewWidgets((_a = frontstageDef.centerLeft) === null || _a === void 0 ? void 0 : _a.widgetDefs, state), "left", 0);
    state = appendWidgets(state, determineNewWidgets((_b = frontstageDef.leftPanel) === null || _b === void 0 ? void 0 : _b.panelZones.start.widgetDefs, state), "left", 0);
    state = appendWidgets(state, determineNewWidgets((_c = frontstageDef.bottomLeft) === null || _c === void 0 ? void 0 : _c.widgetDefs, state), "left", 1);
    state = appendWidgets(state, determineNewWidgets((_d = frontstageDef.leftPanel) === null || _d === void 0 ? void 0 : _d.panelWidgetDefs, state), "left", 1);
    state = appendWidgets(state, determineNewWidgets((_e = frontstageDef.leftPanel) === null || _e === void 0 ? void 0 : _e.panelZones.end.widgetDefs, state), "left", 1);
    state = appendWidgets(state, determineNewWidgets((_f = frontstageDef.centerRight) === null || _f === void 0 ? void 0 : _f.widgetDefs, state), "right", 0);
    state = appendWidgets(state, determineNewWidgets((_g = frontstageDef.rightPanel) === null || _g === void 0 ? void 0 : _g.panelZones.start.widgetDefs, state), "right", 0);
    state = appendWidgets(state, determineNewWidgets((_h = frontstageDef.bottomRight) === null || _h === void 0 ? void 0 : _h.widgetDefs, state), "right", 1);
    state = appendWidgets(state, determineNewWidgets((_j = frontstageDef.rightPanel) === null || _j === void 0 ? void 0 : _j.panelWidgetDefs, state), "right", 1);
    state = appendWidgets(state, determineNewWidgets((_k = frontstageDef.rightPanel) === null || _k === void 0 ? void 0 : _k.panelZones.end.widgetDefs, state), "right", 1);
    state = appendWidgets(state, determineNewWidgets((_l = frontstageDef.topPanel) === null || _l === void 0 ? void 0 : _l.panelWidgetDefs, state), "top", 0);
    state = appendWidgets(state, determineNewWidgets((_m = frontstageDef.topPanel) === null || _m === void 0 ? void 0 : _m.panelZones.start.widgetDefs, state), "top", 0);
    state = appendWidgets(state, determineNewWidgets((_o = frontstageDef.topMostPanel) === null || _o === void 0 ? void 0 : _o.panelWidgetDefs, state), "top", 1); // eslint-disable-line deprecation/deprecation
    state = appendWidgets(state, determineNewWidgets((_p = frontstageDef.topPanel) === null || _p === void 0 ? void 0 : _p.panelZones.end.widgetDefs, state), "top", 1);
    state = appendWidgets(state, determineNewWidgets((_q = frontstageDef.bottomPanel) === null || _q === void 0 ? void 0 : _q.panelWidgetDefs, state), "bottom", 0);
    state = appendWidgets(state, determineNewWidgets((_r = frontstageDef.bottomPanel) === null || _r === void 0 ? void 0 : _r.panelZones.start.widgetDefs, state), "bottom", 0);
    state = appendWidgets(state, determineNewWidgets((_s = frontstageDef.bottomMostPanel) === null || _s === void 0 ? void 0 : _s.panelWidgetDefs, state), "bottom", 1); // eslint-disable-line deprecation/deprecation
    state = appendWidgets(state, determineNewWidgets((_t = frontstageDef.bottomPanel) === null || _t === void 0 ? void 0 : _t.panelZones.end.widgetDefs, state), "bottom", 1);
    return state;
}
exports.addMissingWidgets = addMissingWidgets;
/** Hide widgets with Hidden defaultState. */
function hideWidgets(state, frontstageDef) {
    const tabs = Object.values(state.tabs);
    for (const tab of tabs) {
        const widgetDef = frontstageDef.findWidgetDef(tab.id);
        if (!widgetDef)
            continue;
        if (widgetDef.defaultState === appui_abstract_1.WidgetState.Hidden) {
            state = hideWidget(state, widgetDef);
        }
    }
    return state;
}
/** Removes NineZoneState widgets that are missing in frontstageDef.
 * @internal
 */
function removeMissingWidgets(frontstageDef, initialState) {
    let state = initialState;
    for (const [, tab] of Object.entries(state.tabs)) {
        if (tab.id === appui_layout_react_1.toolSettingsTabId)
            continue;
        const widgetDef = frontstageDef.findWidgetDef(tab.id);
        if (widgetDef)
            continue;
        state = (0, appui_layout_react_1.removeTab)(state, tab.id);
    }
    return state;
}
exports.removeMissingWidgets = removeMissingWidgets;
function getWidgetLabel(label) {
    return label === "" ? "Widget" : label;
}
function toStagePanelLocation(side) {
    switch (side) {
        case "bottom":
            return appui_abstract_1.StagePanelLocation.Bottom;
        case "left":
            return appui_abstract_1.StagePanelLocation.Left;
        case "right":
            return appui_abstract_1.StagePanelLocation.Right;
        case "top":
            return appui_abstract_1.StagePanelLocation.Top;
    }
}
/** @internal */
function getWidgetId(side, key) {
    switch (side) {
        case "left": {
            if (key === "start") {
                return "leftStart";
            }
            return "leftEnd";
        }
        case "right": {
            if (key === "start") {
                return "rightStart";
            }
            return "rightEnd";
        }
        case "top": {
            if (key === "start") {
                return "topStart";
            }
            return "topEnd";
        }
        case "bottom": {
            if (key === "start")
                return "bottomStart";
            return "bottomEnd";
        }
    }
}
exports.getWidgetId = getWidgetId;
/** @internal */
function getPanelZoneWidgets(frontstageDef, panelZone) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;
    switch (panelZone) {
        case "leftStart": {
            const outArray = [...((_a = frontstageDef.centerLeft) === null || _a === void 0 ? void 0 : _a.widgetDefs) || []];
            (_b = frontstageDef.leftPanel) === null || _b === void 0 ? void 0 : _b.panelZones.start.widgetDefs.forEach((widgetDef) => {
                // istanbul ignore else
                if (undefined === outArray.find((widget) => widget.id === widgetDef.id))
                    outArray.push(widgetDef);
            });
            return outArray;
        }
        case "leftEnd": {
            const outArray = [...((_c = frontstageDef.bottomLeft) === null || _c === void 0 ? void 0 : _c.widgetDefs) || []];
            (_d = frontstageDef.leftPanel) === null || _d === void 0 ? void 0 : _d.panelZones.end.widgetDefs.forEach((widgetDef) => {
                // istanbul ignore else
                if (undefined === outArray.find((widget) => widget.id === widgetDef.id))
                    outArray.push(widgetDef);
            });
            (_e = frontstageDef.leftPanel) === null || _e === void 0 ? void 0 : _e.panelWidgetDefs.forEach((widgetDef) => {
                // istanbul ignore else
                if (undefined === outArray.find((widget) => widget.id === widgetDef.id))
                    outArray.push(widgetDef);
            });
            return outArray;
        }
        case "rightStart": {
            const outArray = [...((_f = frontstageDef.centerRight) === null || _f === void 0 ? void 0 : _f.widgetDefs) || []];
            (_g = frontstageDef.rightPanel) === null || _g === void 0 ? void 0 : _g.panelZones.start.widgetDefs.forEach((widgetDef) => {
                // istanbul ignore else
                if (undefined === outArray.find((widget) => widget.id === widgetDef.id))
                    outArray.push(widgetDef);
            });
            return outArray;
        }
        case "rightEnd": {
            const outArray = [...((_h = frontstageDef.bottomRight) === null || _h === void 0 ? void 0 : _h.widgetDefs) || []];
            (_j = frontstageDef.rightPanel) === null || _j === void 0 ? void 0 : _j.panelZones.end.widgetDefs.forEach((widgetDef) => {
                // istanbul ignore else
                if (undefined === outArray.find((widget) => widget.id === widgetDef.id))
                    outArray.push(widgetDef);
            });
            (_k = frontstageDef.rightPanel) === null || _k === void 0 ? void 0 : _k.panelWidgetDefs.forEach((widgetDef) => {
                // istanbul ignore else
                if (undefined === outArray.find((widget) => widget.id === widgetDef.id))
                    outArray.push(widgetDef);
            });
            return outArray;
        }
        case "topStart": {
            const outArray = [...((_l = frontstageDef.topPanel) === null || _l === void 0 ? void 0 : _l.panelWidgetDefs) || []];
            (_m = frontstageDef.topPanel) === null || _m === void 0 ? void 0 : _m.panelZones.start.widgetDefs.forEach((widgetDef) => {
                // istanbul ignore else
                if (undefined === outArray.find((widget) => widget.id === widgetDef.id))
                    outArray.push(widgetDef);
            });
            return outArray;
        }
        case "topEnd": {
            const outArray = [...((_o = frontstageDef.topMostPanel) === null || _o === void 0 ? void 0 : _o.panelWidgetDefs) || []]; // eslint-disable-line deprecation/deprecation
            (_p = frontstageDef.topPanel) === null || _p === void 0 ? void 0 : _p.panelZones.end.widgetDefs.forEach((widgetDef) => {
                // istanbul ignore else
                if (undefined === outArray.find((widget) => widget.id === widgetDef.id))
                    outArray.push(widgetDef);
            });
            return outArray;
        }
        case "bottomStart": {
            const outArray = [...((_q = frontstageDef.bottomPanel) === null || _q === void 0 ? void 0 : _q.panelWidgetDefs) || []];
            (_r = frontstageDef.bottomPanel) === null || _r === void 0 ? void 0 : _r.panelZones.start.widgetDefs.forEach((widgetDef) => {
                // istanbul ignore else
                if (undefined === outArray.find((widget) => widget.id === widgetDef.id))
                    outArray.push(widgetDef);
            });
            return outArray;
        }
        case "bottomEnd": {
            const outArray = [...((_s = frontstageDef.bottomMostPanel) === null || _s === void 0 ? void 0 : _s.panelWidgetDefs) || []]; // eslint-disable-line deprecation/deprecation
            (_t = frontstageDef.bottomPanel) === null || _t === void 0 ? void 0 : _t.panelZones.end.widgetDefs.forEach((widgetDef) => {
                // istanbul ignore else
                if (undefined === outArray.find((widget) => widget.id === widgetDef.id))
                    outArray.push(widgetDef);
            });
            return outArray;
        }
    }
}
exports.getPanelZoneWidgets = getPanelZoneWidgets;
/** @internal */
function addPanelWidgets(state, frontstageDef, side) {
    const start = getWidgetId(side, "start");
    const startWidgets = getPanelZoneWidgets(frontstageDef, start);
    state = addWidgets(state, determineNewWidgets(startWidgets, state), side, start);
    const end = getWidgetId(side, "end");
    const endWidgets = getPanelZoneWidgets(frontstageDef, end);
    state = addWidgets(state, determineNewWidgets(endWidgets, state), side, end);
    return state;
}
exports.addPanelWidgets = addPanelWidgets;
/** @internal */
function isFrontstageStateSettingResult(settingsResult) {
    if (settingsResult.status === core_react_1.UiStateStorageStatus.Success)
        return true;
    return false;
}
exports.isFrontstageStateSettingResult = isFrontstageStateSettingResult;
/** @internal */
function initializePanel(state, frontstageDef, panelSide) {
    state = addPanelWidgets(state, frontstageDef, panelSide);
    const location = toStagePanelLocation(panelSide);
    const panelDef = frontstageDef.getStagePanelDef(location);
    state = (0, immer_1.default)(state, (draft) => {
        var _a, _b, _c;
        const panel = draft.panels[panelSide];
        panel.minSize = (_a = panelDef === null || panelDef === void 0 ? void 0 : panelDef.minSize) !== null && _a !== void 0 ? _a : panel.minSize;
        panel.pinned = (_b = panelDef === null || panelDef === void 0 ? void 0 : panelDef.pinned) !== null && _b !== void 0 ? _b : panel.pinned;
        panel.resizable = (_c = panelDef === null || panelDef === void 0 ? void 0 : panelDef.resizable) !== null && _c !== void 0 ? _c : panel.resizable;
        if (panelDef === null || panelDef === void 0 ? void 0 : panelDef.maxSizeSpec) {
            panel.maxSize = getPanelMaxSize(panelDef.maxSizeSpec, panelSide, state.size);
        }
        const size = panelDef === null || panelDef === void 0 ? void 0 : panelDef.size;
        panel.size = size === undefined ? size : Math.min(Math.max(size, panel.minSize), panel.maxSize);
    });
    return state;
}
exports.initializePanel = initializePanel;
function getPanelMaxSize(maxSizeSpec, panel, nineZoneSize) {
    if (typeof maxSizeSpec === "number") {
        return maxSizeSpec;
    }
    const size = (0, appui_layout_react_1.isHorizontalPanelSide)(panel) ? nineZoneSize.height : nineZoneSize.width;
    return maxSizeSpec.percentage / 100 * size;
}
const stateVersion = 13; // this needs to be bumped when NineZoneState is changed (to recreate the layout).
/** @internal */
function initializeNineZoneState(frontstageDef) {
    let nineZone = defaultNineZone;
    nineZone = (0, immer_1.default)(nineZone, (stateDraft) => {
        // istanbul ignore next
        if (!InternalFrontstageManager_1.InternalFrontstageManager.nineZoneSize)
            return;
        stateDraft.size = {
            height: InternalFrontstageManager_1.InternalFrontstageManager.nineZoneSize.height,
            width: InternalFrontstageManager_1.InternalFrontstageManager.nineZoneSize.width,
        };
    });
    nineZone = initializePanel(nineZone, frontstageDef, "left");
    nineZone = initializePanel(nineZone, frontstageDef, "right");
    nineZone = initializePanel(nineZone, frontstageDef, "top");
    nineZone = initializePanel(nineZone, frontstageDef, "bottom");
    nineZone = (0, immer_1.default)(nineZone, (stateDraft) => {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
        for (const [, panel] of Object.entries(stateDraft.panels)) {
            const expanded = panel.widgets.find((widgetId) => stateDraft.widgets[widgetId].minimized === false);
            const firstWidget = panel.widgets.length > 0 ? stateDraft.widgets[panel.widgets[0]] : undefined;
            // istanbul ignore next
            if (!expanded && firstWidget) {
                firstWidget.minimized = false;
            }
        }
        stateDraft.panels.left.collapsed = isPanelCollapsed([
            (_a = frontstageDef.centerLeft) === null || _a === void 0 ? void 0 : _a.zoneState,
            (_b = frontstageDef.bottomLeft) === null || _b === void 0 ? void 0 : _b.zoneState,
        ], [(_c = frontstageDef.leftPanel) === null || _c === void 0 ? void 0 : _c.panelState]);
        stateDraft.panels.right.collapsed = isPanelCollapsed([
            (_d = frontstageDef.centerRight) === null || _d === void 0 ? void 0 : _d.zoneState,
            (_e = frontstageDef.bottomRight) === null || _e === void 0 ? void 0 : _e.zoneState,
        ], [(_f = frontstageDef.rightPanel) === null || _f === void 0 ? void 0 : _f.panelState]);
        stateDraft.panels.top.collapsed = isPanelCollapsed([], [
            // istanbul ignore next - ignore topMostPanel if topPanel is defined
            frontstageDef.topPanel ? (_g = frontstageDef.topPanel) === null || _g === void 0 ? void 0 : _g.panelState : (_h = frontstageDef.topMostPanel) === null || _h === void 0 ? void 0 : _h.panelState, // eslint-disable-line deprecation/deprecation
        ]);
        stateDraft.panels.bottom.collapsed = isPanelCollapsed([], [
            // istanbul ignore next - ignore bottomMostPanel if bottomPanel is defined
            frontstageDef.bottomPanel ? (_j = frontstageDef.bottomPanel) === null || _j === void 0 ? void 0 : _j.panelState : (_k = frontstageDef.bottomMostPanel) === null || _k === void 0 ? void 0 : _k.panelState, // eslint-disable-line deprecation/deprecation
        ]);
        const topCenterDef = frontstageDef.topCenter;
        const toolSettingsWidgetDef = topCenterDef === null || topCenterDef === void 0 ? void 0 : topCenterDef.getSingleWidgetDef();
        if (toolSettingsWidgetDef) {
            const toolSettingsTab = stateDraft.tabs[appui_layout_react_1.toolSettingsTabId];
            toolSettingsTab.preferredPanelWidgetSize = toolSettingsWidgetDef.preferredPanelSize;
        }
    });
    nineZone = addMissingWidgets(frontstageDef, nineZone);
    nineZone = hideWidgets(nineZone, frontstageDef);
    nineZone = processPopoutWidgets(nineZone, frontstageDef);
    return nineZone;
}
exports.initializeNineZoneState = initializeNineZoneState;
/** Converts from saved NineZoneState to NineZoneState.
 * @note Restores toolSettings tab.
 * @note Restores tab labels.
 * @internal
 */
function restoreNineZoneState(frontstageDef, saved) {
    let restored = {
        ...saved,
        tabs: defaultNineZone.tabs,
    };
    for (const [, tab] of Object.entries(saved.tabs)) {
        // Re-add the placeholder tab in order to correctly remove it from the state if WidgetDef is not found.
        restored = (0, appui_layout_react_1.addTab)(restored, tab.id);
        const widgetDef = frontstageDef.findWidgetDef(tab.id);
        if (!widgetDef) {
            core_bentley_1.Logger.logInfo(UiFramework_1.UiFramework.loggerCategory(restoreNineZoneState), "WidgetDef is not found for saved tab.", () => ({
                frontstageId: frontstageDef.id,
                tabId: tab.id,
            }));
            restored = (0, appui_layout_react_1.removeTab)(restored, tab.id);
            continue;
        }
        restored = (0, immer_1.default)(restored, (draft) => {
            draft.tabs[tab.id] = {
                ...tab,
                label: getWidgetLabel(widgetDef.label),
                hideWithUiWhenFloating: widgetDef.hideWithUiWhenFloating,
                iconSpec: widgetDef.iconSpec,
                canPopout: widgetDef.canPopout,
                isFloatingStateWindowResizable: widgetDef.isFloatingStateWindowResizable,
                allowedPanelTargets: widgetDef.allowedPanelTargets,
            };
        });
    }
    restored = (0, immer_1.default)(restored, (draft) => {
        // remove center panel section if one is found in left or right panels
        const oldLeftMiddleIndex = saved.panels.left.widgets.findIndex((value) => value === "leftMiddle");
        // istanbul ignore next
        if (-1 !== oldLeftMiddleIndex) {
            draft.panels.left.widgets = saved.panels.left.widgets.filter((value) => value !== "leftMiddle");
            if ("leftEnd" in draft.widgets) {
                draft.widgets.leftMiddle.tabs.forEach((tab) => draft.widgets.leftEnd.tabs.push(tab));
            }
            else {
                draft.widgets.leftEnd = { ...draft.widgets.leftMiddle };
                delete draft.widgets.leftMiddle;
            }
        }
        const oldRightMiddleIndex = saved.panels.right.widgets.findIndex((value) => value === "rightMiddle");
        // istanbul ignore next
        if (-1 !== oldRightMiddleIndex) {
            draft.panels.right.widgets = saved.panels.right.widgets.filter((value) => value !== "rightMiddle");
            if ("rightEnd" in draft.widgets) {
                draft.widgets.rightMiddle.tabs.forEach((tab) => draft.widgets.rightEnd.tabs.push(tab));
            }
            else {
                draft.widgets.rightEnd = { ...draft.widgets.rightMiddle };
                delete draft.widgets.rightMiddle;
            }
        }
        return;
    });
    if (InternalFrontstageManager_1.InternalFrontstageManager.nineZoneSize && (0 !== InternalFrontstageManager_1.InternalFrontstageManager.nineZoneSize.height || 0 !== InternalFrontstageManager_1.InternalFrontstageManager.nineZoneSize.width)) {
        restored = FrameworkStateReducer(restored, {
            type: "RESIZE",
            size: {
                height: InternalFrontstageManager_1.InternalFrontstageManager.nineZoneSize.height,
                width: InternalFrontstageManager_1.InternalFrontstageManager.nineZoneSize.width,
            },
        }, frontstageDef);
    }
    return restored;
}
exports.restoreNineZoneState = restoreNineZoneState;
/** Prepares NineZoneState to be saved.
 * @note Removes toolSettings tab.
 * @note Removes tab labels and iconSpec.
 * @internal
 */
function packNineZoneState(state) {
    let packed = {
        ...state,
        tabs: {},
    };
    packed = (0, immer_1.default)(packed, (draft) => {
        for (const [, tab] of Object.entries(state.tabs)) {
            if (tab.id === appui_layout_react_1.toolSettingsTabId)
                continue;
            draft.tabs[tab.id] = {
                id: tab.id,
                preferredFloatingWidgetSize: tab.preferredFloatingWidgetSize,
                allowedPanelTargets: tab.allowedPanelTargets,
                userSized: tab.userSized,
            };
        }
    });
    return packed;
}
exports.packNineZoneState = packNineZoneState;
// istanbul ignore next
function packSavedWidgets(frontstageDef) {
    let savedWidgets = [];
    for (const widgetDef of frontstageDef.widgetDefs) {
        const id = widgetDef.id;
        const initialWidget = {
            id,
        };
        const widget = (0, immer_1.default)(initialWidget, (draft) => {
            if (widgetDef.tabLocation) {
                draft.tabLocation = { ...widgetDef.tabLocation };
            }
            if (widgetDef.popoutBounds) {
                draft.popoutBounds = widgetDef.popoutBounds.toProps();
            }
        });
        if (widget === initialWidget)
            continue;
        savedWidgets.push(widget);
    }
    // Add previously saved widgets.
    const prevSavedWidgets = frontstageDef.savedWidgetDefs || [];
    for (const prevWidget of prevSavedWidgets) {
        if (savedWidgets.find((w) => w.id === prevWidget.id))
            continue;
        savedWidgets.push(prevWidget);
    }
    // Limit number of saved widgets.
    const maxWidgets = 100;
    if (savedWidgets.length > maxWidgets)
        savedWidgets = savedWidgets.slice(0, maxWidgets);
    return savedWidgets;
}
// istanbul ignore next
function restoreSavedWidgets(frontstage) {
    const savedWidgets = frontstage.savedWidgetDefs || [];
    for (const savedWidget of savedWidgets) {
        const widgetId = savedWidget.id;
        const widgetDef = frontstage.findWidgetDef(widgetId);
        if (!widgetDef)
            continue;
        if (savedWidget.tabLocation) {
            widgetDef.tabLocation = { ...savedWidget.tabLocation };
        }
        if (savedWidget.popoutBounds) {
            widgetDef.popoutBounds = core_react_1.Rectangle.create(savedWidget.popoutBounds);
        }
    }
}
/** @internal */
function isPanelCollapsed(zoneStates, panelStates) {
    const openZone = zoneStates.find((zoneState) => zoneState === ZoneDef_1.ZoneState.Open);
    const openPanel = panelStates.find((panelState) => panelState === StagePanelDef_1.StagePanelState.Open);
    return !openZone && !openPanel;
}
exports.isPanelCollapsed = isPanelCollapsed;
function addHiddenWidget(state, widgetDef) {
    const tabLocation = widgetDef.tabLocation || widgetDef.defaultTabLocation;
    const tabId = widgetDef.id;
    const { widgetId, tabIndex } = tabLocation;
    if (widgetId in state.widgets) {
        // Add to an existing widget (by widget id).
        return (0, appui_layout_react_1.insertTabToWidget)(state, tabId, widgetId, tabIndex);
    }
    else if (tabLocation.floatingWidget) {
        const floatingWidget = tabLocation.floatingWidget;
        (0, core_bentley_1.assert)(widgetId === floatingWidget.id);
        // Add to a new floating widget.
        const nzBounds = core_react_1.Rectangle.createFromSize(state.size);
        const bounds = core_react_1.Rectangle.create(floatingWidget.bounds).containIn(nzBounds);
        return (0, appui_layout_react_1.addFloatingWidget)(state, floatingWidget.id, [tabId], {
            ...floatingWidget,
            bounds: bounds.toProps(),
        });
    }
    // Add to a panel section.
    const panel = state.panels[tabLocation.side];
    // Add to existing panel section.
    if (panel.widgets.length >= panel.maxWidgetCount) {
        const sectionIndex = Math.min(panel.maxWidgetCount - 1, tabLocation.widgetIndex);
        const sectionId = panel.widgets[sectionIndex];
        return (0, appui_layout_react_1.insertTabToWidget)(state, tabId, sectionId, tabLocation.tabIndex);
    }
    // Create a new panel section.
    const newSectionId = (0, appui_layout_react_1.getUniqueId)();
    return (0, appui_layout_react_1.insertPanelWidget)(state, panel.side, newSectionId, [tabId], tabLocation.widgetIndex);
}
/** @internal */
function setWidgetState(state, widgetDef, widgetState) {
    var _a, _b;
    if (widgetState === appui_abstract_1.WidgetState.Hidden) {
        return hideWidget(state, widgetDef);
    }
    else if (widgetState === appui_abstract_1.WidgetState.Open) {
        const id = widgetDef.id;
        let location = (0, appui_layout_react_1.getTabLocation)(state, id);
        if (!location) {
            state = addHiddenWidget(state, widgetDef);
            location = (0, appui_layout_react_1.getTabLocation)(state, id);
        }
        return (0, immer_1.default)(state, (draft) => {
            var _a;
            (0, core_bentley_1.assert)(!!location);
            const widget = draft.widgets[location.widgetId];
            widget.minimized = false;
            widget.activeTabId = id;
            if ((0, appui_layout_react_1.isFloatingTabLocation)(location)) {
                const floatingWidget = draft.floatingWidgets.byId[location.floatingWidgetId];
                floatingWidget.hidden = false;
                // istanbul ignore else
            }
            else if ((0, appui_layout_react_1.isPanelTabLocation)(location)) {
                const panel = draft.panels[location.side];
                panel.collapsed = false;
                if (undefined === panel.size || 0 === panel.size) {
                    // istanbul ignore next
                    panel.size = (_a = panel.minSize) !== null && _a !== void 0 ? _a : 200;
                }
            }
        });
    }
    else if (widgetState === appui_abstract_1.WidgetState.Closed) {
        const id = widgetDef.id;
        let location = (0, appui_layout_react_1.getTabLocation)(state, id);
        if (!location) {
            state = addHiddenWidget(state, widgetDef);
            location = (0, appui_layout_react_1.getTabLocation)(state, id);
        }
        // TODO: should change activeTabId of a widget with multiple tabs.
        return (0, immer_1.default)(state, (draft) => {
            (0, core_bentley_1.assert)(!!location);
            const widget = draft.widgets[location.widgetId];
            if ((0, appui_layout_react_1.isFloatingTabLocation)(location)) {
                if (id === widget.activeTabId) {
                    widget.minimized = true;
                }
            }
        });
    }
    else if (widgetState === appui_abstract_1.WidgetState.Floating) {
        const id = widgetDef.id;
        let location = (0, appui_layout_react_1.getTabLocation)(state, id);
        if (!location) {
            state = addHiddenWidget(state, widgetDef);
            location = (0, appui_layout_react_1.getTabLocation)(state, id);
        }
        (0, core_bentley_1.assert)(!!location);
        if ((0, appui_layout_react_1.isFloatingTabLocation)(location))
            return state;
        // Widget is not floating.
        state = (0, appui_layout_react_1.removeTabFromWidget)(state, id);
        const bounds = (_b = (_a = widgetDef.tabLocation) === null || _a === void 0 ? void 0 : _a.floatingWidget) === null || _b === void 0 ? void 0 : _b.bounds;
        state = (0, appui_layout_react_1.addFloatingWidget)(state, (0, appui_layout_react_1.getUniqueId)(), [id], { bounds });
    }
    return state;
}
exports.setWidgetState = setWidgetState;
/** Stores widget location and hides it in the UI. */
function hideWidget(state, widgetDef) {
    const location = (0, appui_layout_react_1.getTabLocation)(state, widgetDef.id);
    if (!location)
        return state;
    // istanbul ignore else
    if ((0, appui_layout_react_1.isFloatingTabLocation)(location)) {
        const floatingWidget = state.floatingWidgets.byId[location.widgetId];
        widgetDef.setFloatingContainerId(location.floatingWidgetId);
        widgetDef.tabLocation = {
            side: floatingWidget.home.side,
            tabIndex: floatingWidget.home.widgetIndex,
            widgetId: floatingWidget.id,
            widgetIndex: floatingWidget.home.widgetIndex,
            floatingWidget,
        };
    }
    else if (!(0, appui_layout_react_1.isPopoutTabLocation)(location)) {
        const widgetId = location.widgetId;
        const side = (0, appui_layout_react_1.isPanelTabLocation)(location) ? location.side : "left";
        const widgetIndex = (0, appui_layout_react_1.isPanelTabLocation)(location) ? state.panels[side].widgets.indexOf(widgetId) : 0;
        const tabIndex = state.widgets[location.widgetId].tabs.indexOf(widgetDef.id);
        widgetDef.tabLocation = {
            side,
            tabIndex,
            widgetId,
            widgetIndex,
        };
    }
    return (0, appui_layout_react_1.removeTabFromWidget)(state, widgetDef.id);
}
/** @internal */
function showWidget(state, id) {
    const location = (0, appui_layout_react_1.getTabLocation)(state, id);
    if (!location)
        return state;
    state = (0, immer_1.default)(state, (draft) => {
        const widget = draft.widgets[location.widgetId];
        widget.activeTabId = id;
        widget.minimized = false;
        if ((0, appui_layout_react_1.isPanelTabLocation)(location)) {
            const panel = draft.panels[location.side];
            panel.collapsed = false;
        }
    });
    if ((0, appui_layout_react_1.isFloatingTabLocation)(location)) {
        state = (0, appui_layout_react_1.floatingWidgetBringToFront)(state, location.floatingWidgetId);
    }
    return state;
}
exports.showWidget = showWidget;
/** @internal */
function expandWidget(state, id) {
    const location = (0, appui_layout_react_1.getTabLocation)(state, id);
    if (!location)
        return state;
    return (0, immer_1.default)(state, (draft) => {
        const widget = draft.widgets[location.widgetId];
        if ((0, appui_layout_react_1.isPanelTabLocation)(location)) {
            const panel = draft.panels[location.side];
            panel.widgets.forEach((wId) => {
                const w = draft.widgets[wId];
                w.minimized = true;
            });
        }
        widget.minimized = false;
    });
}
exports.expandWidget = expandWidget;
/** @internal */
function setWidgetLabel(state, id, label) {
    if (!(id in state.tabs))
        return state;
    return (0, immer_1.default)(state, (draft) => {
        const tab = draft.tabs[id];
        tab.label = label;
    });
}
exports.setWidgetLabel = setWidgetLabel;
/** @internal */
function useSavedFrontstageState(frontstageDef) {
    const uiStateStorage = (0, useUiStateStorage_1.useUiStateStorageHandler)();
    const uiStateStorageRef = React.useRef(uiStateStorage);
    const isMountedRef = React.useRef(false);
    React.useEffect(() => {
        isMountedRef.current = true;
        return () => { isMountedRef.current = false; };
    }, []);
    React.useEffect(() => {
        uiStateStorageRef.current = uiStateStorage;
    }, [uiStateStorage]);
    React.useEffect(() => {
        async function fetchFrontstageState() {
            if (frontstageDef.nineZoneState) {
                frontstageDef.nineZoneState = processPopoutWidgets(frontstageDef.nineZoneState, frontstageDef);
                return;
            }
            const id = frontstageDef.id;
            const version = frontstageDef.version;
            const settingResult = await uiStateStorageRef.current.getSetting(exports.FRONTSTAGE_SETTINGS_NAMESPACE, getFrontstageStateSettingName(id));
            if (!isMountedRef.current)
                return;
            if (isFrontstageStateSettingResult(settingResult)) {
                const setting = settingResult.setting;
                if (setting.version >= version && setting.stateVersion >= stateVersion) {
                    const restored = restoreNineZoneState(frontstageDef, setting.nineZone);
                    frontstageDef.savedWidgetDefs = setting.widgets;
                    restoreSavedWidgets(frontstageDef);
                    let state = addMissingWidgets(frontstageDef, restored);
                    state = hideWidgets(state, frontstageDef);
                    state = processPopoutWidgets(state, frontstageDef);
                    frontstageDef.nineZoneState = state;
                    return;
                }
            }
            frontstageDef.nineZoneState = initializeNineZoneState(frontstageDef);
        }
        fetchFrontstageState(); // eslint-disable-line @typescript-eslint/no-floating-promises
    }, [frontstageDef]);
}
exports.useSavedFrontstageState = useSavedFrontstageState;
/** @internal */
function useSaveFrontstageSettings(frontstageDef) {
    const nineZone = useNineZoneState(frontstageDef);
    const uiSettingsStorage = (0, useUiStateStorage_1.useUiStateStorageHandler)();
    const saveSetting = React.useMemo(() => {
        return debounce(async (frontstage, state) => {
            const id = frontstage.id;
            const widgets = packSavedWidgets(frontstage);
            frontstage.savedWidgetDefs = widgets;
            const setting = {
                id,
                version: frontstage.version,
                stateVersion,
                nineZone: packNineZoneState(state),
                widgets,
            };
            await uiSettingsStorage.saveSetting(exports.FRONTSTAGE_SETTINGS_NAMESPACE, getFrontstageStateSettingName(id), setting);
        }, 1000);
    }, [uiSettingsStorage]);
    React.useEffect(() => {
        return () => {
            saveSetting.cancel();
        };
    }, [saveSetting]);
    React.useEffect(() => {
        if (!nineZone || nineZone.draggedTab)
            return;
        saveSetting(frontstageDef, nineZone);
    }, [frontstageDef, nineZone, saveSetting]);
}
exports.useSaveFrontstageSettings = useSaveFrontstageSettings;
/** @internal */
exports.FRONTSTAGE_SETTINGS_NAMESPACE = "uifw-frontstageSettings";
/** @internal */
function getFrontstageStateSettingName(frontstageId) {
    return `frontstageState[${frontstageId}]`;
}
exports.getFrontstageStateSettingName = getFrontstageStateSettingName;
// istanbul ignore next
function debounce(func, duration) {
    let timeout;
    const debounced = (...args) => {
        const handler = () => {
            timeout = undefined;
            return func(...args);
        };
        window.clearTimeout(timeout);
        timeout = window.setTimeout(handler, duration);
    };
    debounced.cancel = () => {
        window.clearTimeout(timeout);
    };
    return debounced;
}
const createListener = (frontstageDef, listener) => {
    return (...args) => {
        if (!frontstageDef.nineZoneState)
            return;
        listener(...args);
    };
};
/** @internal */
function useFrontstageManager(frontstageDef, useToolAsToolSettingsLabel) {
    React.useEffect(() => {
        const listener = createListener(frontstageDef, ({ widgetDef, widgetState }) => {
            (0, core_bentley_1.assert)(!!frontstageDef.nineZoneState);
            frontstageDef.nineZoneState = setWidgetState(frontstageDef.nineZoneState, widgetDef, widgetState);
        });
        UiFramework_1.UiFramework.frontstages.onWidgetStateChangedEvent.addListener(listener);
        return () => {
            UiFramework_1.UiFramework.frontstages.onWidgetStateChangedEvent.removeListener(listener);
        };
    }, [frontstageDef]);
    React.useEffect(() => {
        const listener = createListener(frontstageDef, ({ widgetDef }) => {
            (0, core_bentley_1.assert)(!!frontstageDef.nineZoneState);
            frontstageDef.nineZoneState = showWidget(frontstageDef.nineZoneState, widgetDef.id);
        });
        InternalFrontstageManager_1.InternalFrontstageManager.onWidgetShowEvent.addListener(listener);
        return () => {
            InternalFrontstageManager_1.InternalFrontstageManager.onWidgetShowEvent.removeListener(listener);
        };
    }, [frontstageDef]);
    React.useEffect(() => {
        const listener = createListener(frontstageDef, ({ widgetDef }) => {
            (0, core_bentley_1.assert)(!!frontstageDef.nineZoneState);
            let nineZoneState = showWidget(frontstageDef.nineZoneState, widgetDef.id);
            nineZoneState = expandWidget(nineZoneState, widgetDef.id);
            frontstageDef.nineZoneState = nineZoneState;
        });
        InternalFrontstageManager_1.InternalFrontstageManager.onWidgetExpandEvent.addListener(listener);
        return () => {
            InternalFrontstageManager_1.InternalFrontstageManager.onWidgetExpandEvent.removeListener(listener);
        };
    }, [frontstageDef]);
    const uiSettingsStorage = (0, useUiStateStorage_1.useUiStateStorageHandler)();
    React.useEffect(() => {
        const listener = (args) => {
            // TODO: track restoring frontstages to support workflows:  i.e. prevent loading frontstage OR saving layout when delete is pending
            uiSettingsStorage.deleteSetting(exports.FRONTSTAGE_SETTINGS_NAMESPACE, getFrontstageStateSettingName(args.frontstageDef.id)); // eslint-disable-line @typescript-eslint/no-floating-promises
            if (frontstageDef.id === args.frontstageDef.id) {
                args.frontstageDef.nineZoneState = initializeNineZoneState(frontstageDef);
            }
            else {
                args.frontstageDef.nineZoneState = undefined;
            }
        };
        InternalFrontstageManager_1.InternalFrontstageManager.onFrontstageRestoreLayoutEvent.addListener(listener);
        return () => {
            InternalFrontstageManager_1.InternalFrontstageManager.onFrontstageRestoreLayoutEvent.removeListener(listener);
        };
    }, [uiSettingsStorage, frontstageDef]);
    React.useEffect(() => {
        const listener = createListener(frontstageDef, ({ widgetDef }) => {
            (0, core_bentley_1.assert)(!!frontstageDef.nineZoneState);
            const label = widgetDef.label;
            frontstageDef.nineZoneState = setWidgetLabel(frontstageDef.nineZoneState, widgetDef.id, label);
        });
        InternalFrontstageManager_1.InternalFrontstageManager.onWidgetLabelChangedEvent.addListener(listener);
        return () => {
            InternalFrontstageManager_1.InternalFrontstageManager.onWidgetLabelChangedEvent.removeListener(listener);
        };
    }, [frontstageDef]);
    const defaultLabel = React.useMemo(() => UiFramework_1.UiFramework.translate("widget.labels.toolSettings"), []);
    React.useEffect(() => {
        const updateLabel = createListener(frontstageDef, () => {
            var _a;
            const toolId = UiFramework_1.UiFramework.frontstages.activeToolId;
            (0, core_bentley_1.assert)(!!frontstageDef.nineZoneState);
            const label = useToolAsToolSettingsLabel ?
                ((_a = core_frontend_1.IModelApp.tools.find(toolId)) === null || _a === void 0 ? void 0 : _a.flyover) || defaultLabel : defaultLabel;
            frontstageDef.nineZoneState = setWidgetLabel(frontstageDef.nineZoneState, appui_layout_react_1.toolSettingsTabId, label);
        });
        // Whenever the frontstageDef or the useTool... changes, keep the label up to date.
        updateLabel();
        // If useTool... is true, listen for events to keep up the label up to date.
        if (useToolAsToolSettingsLabel) {
            UiFramework_1.UiFramework.frontstages.onFrontstageReadyEvent.addListener(updateLabel);
            InternalFrontstageManager_1.InternalFrontstageManager.onFrontstageRestoreLayoutEvent.addListener(updateLabel);
            UiFramework_1.UiFramework.frontstages.onToolActivatedEvent.addListener(updateLabel);
            UiFramework_1.UiFramework.frontstages.onToolSettingsReloadEvent.addListener(updateLabel);
        }
        return () => {
            if (useToolAsToolSettingsLabel) {
                UiFramework_1.UiFramework.frontstages.onFrontstageReadyEvent.removeListener(updateLabel);
                InternalFrontstageManager_1.InternalFrontstageManager.onFrontstageRestoreLayoutEvent.removeListener(updateLabel);
                UiFramework_1.UiFramework.frontstages.onToolActivatedEvent.removeListener(updateLabel);
                UiFramework_1.UiFramework.frontstages.onToolSettingsReloadEvent.removeListener(updateLabel);
            }
        };
    }, [frontstageDef, useToolAsToolSettingsLabel, defaultLabel]);
}
exports.useFrontstageManager = useFrontstageManager;
/** @internal */
// istanbul ignore next
function useItemsManager(frontstageDef) {
    const refreshNineZoneState = (def) => {
        // Fired for both registered/unregistered. Update definitions and remove/add missing widgets.
        def.updateWidgetDefs();
        restoreSavedWidgets(def);
        let state = def.nineZoneState;
        if (!state)
            return;
        state = addMissingWidgets(def, state);
        state = removeMissingWidgets(def, state);
        state = hideWidgets(state, def); // TODO: should only apply to widgets provided by registered provider in onUiProviderRegisteredEvent.
        def.nineZoneState = state;
    };
    React.useEffect(() => {
        const remove = appui_abstract_1.UiItemsManager.onUiProviderRegisteredEvent.addListener(() => {
            refreshNineZoneState(frontstageDef);
        });
        // Need to refresh anytime frontstageDef changes because uiItemsProvider may have added something to
        // another stage before it was possibly unloaded in this stage.
        refreshNineZoneState(frontstageDef);
        return remove;
    }, [frontstageDef]);
}
exports.useItemsManager = useItemsManager;
// istanbul ignore next
function determineNewWidgets(defs, state) {
    return (defs || []).filter((def) => !(def.id in state.tabs));
}
//# sourceMappingURL=Frontstage.js.map