"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 Widget
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.WidgetOverflowContext = exports.WidgetOverflow = void 0;
require("./Overflow.scss");
const classnames_1 = require("classnames");
const React = require("react");
const core_react_1 = require("@itwin/core-react");
const Menu_1 = require("./Menu");
const NineZone_1 = require("../base/NineZone");
const Panel_1 = require("../widget-panels/Panel");
const LayoutStore_1 = require("../base/LayoutStore");
/** @internal */
function WidgetOverflow(props) {
    const [open, setOpen] = React.useState(false);
    const ref = React.useRef(null);
    const [targetRef, target] = (0, core_react_1.useRefState)();
    const resizeObserverRef = (0, core_react_1.useResizeObserver)(props.onResize);
    const refs = (0, core_react_1.useRefs)(ref, resizeObserverRef);
    const handleClick = React.useCallback((e) => {
        e.preventDefault();
        setOpen((prev) => !prev);
    }, []);
    const handleClose = React.useCallback(() => {
        setOpen(false);
    }, []);
    usePanelPopup(handleClose);
    const className = (0, classnames_1.default)("nz-widget-overflow", props.hidden && "nz-hidden");
    const overflowContext = React.useMemo(() => {
        return {
            close: handleClose,
        };
    }, [handleClose]);
    const moreWidgetsTitle = (0, NineZone_1.useLabel)("moreWidgetsTitle");
    return (React.createElement("div", { className: className, ref: refs },
        React.createElement("div", { className: "nz-button", onClick: handleClick, ref: targetRef, role: "button", tabIndex: -1, title: moreWidgetsTitle },
            React.createElement("div", { className: "nz-icon" })),
        React.createElement(exports.WidgetOverflowContext.Provider, { value: overflowContext },
            React.createElement(Menu_1.WidgetMenu, { children: props.children, open: open, onClose: handleClose, target: target }))));
}
exports.WidgetOverflow = WidgetOverflow;
/** @internal */
exports.WidgetOverflowContext = React.createContext(undefined);
exports.WidgetOverflowContext.displayName = "nz:WidgetOverflowContext";
function usePanelPopup(onClose) {
    const side = React.useContext(Panel_1.PanelSideContext);
    const collapsed = (0, LayoutStore_1.useLayout)((state) => side ? state.panels[side].collapsed : false);
    React.useEffect(() => {
        if (collapsed) {
            onClose();
        }
    }, [collapsed, onClose]);
}
//# sourceMappingURL=Overflow.js.map