"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 WidgetPanels
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.usePanelsAutoCollapse = void 0;
const React = require("react");
const core_react_1 = require("@itwin/core-react");
const NineZone_1 = require("../base/NineZone");
const Panel_1 = require("./Panel");
const LayoutStore_1 = require("../base/LayoutStore");
/** @internal */
function usePanelsAutoCollapse() {
    const layoutStore = (0, LayoutStore_1.useLayoutStore)();
    const panelsRef = React.useRef(layoutStore.getState().panels);
    const dispatch = React.useContext(NineZone_1.NineZoneDispatchContext);
    const autoCollapseUnpinnedPanels = React.useContext(NineZone_1.AutoCollapseUnpinnedPanelsContext);
    React.useEffect(() => layoutStore.subscribe((state) => {
        panelsRef.current = state.panels;
    }), [layoutStore]);
    const collapsePanels = React.useCallback(() => {
        for (const side of Panel_1.panelSides) {
            const panel = panelsRef.current[side];
            if (panel.collapsed || panel.pinned)
                continue;
            dispatch({
                type: "PANEL_SET_COLLAPSED",
                collapsed: true,
                side: panel.side,
            });
        }
    }, [dispatch]);
    const mouseDownRef = (0, core_react_1.useRefEffect)((instance) => {
        if (!instance)
            return;
        instance.addEventListener("mousedown", collapsePanels, true);
        return () => {
            instance.removeEventListener("mousedown", collapsePanels, true);
        };
    }, [collapsePanels]);
    const mouseEnterRef = (0, core_react_1.useRefEffect)((instance) => {
        if (!instance || !autoCollapseUnpinnedPanels)
            return;
        instance.addEventListener("mouseenter", collapsePanels, true);
        return () => {
            instance.removeEventListener("mouseenter", collapsePanels, true);
        };
    }, [collapsePanels, autoCollapseUnpinnedPanels]);
    const ref = (0, core_react_1.useRefs)(mouseDownRef, mouseEnterRef);
    return ref;
}
exports.usePanelsAutoCollapse = usePanelsAutoCollapse;
//# sourceMappingURL=usePanelsAutoCollapse.js.map