/*---------------------------------------------------------------------------------------------
 * 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
 */
import "./Overflow.scss";
import classnames from "classnames";
import * as React from "react";
import { useRefs, useRefState, useResizeObserver } from "@itwin/core-react";
import { WidgetMenu } from "./Menu";
import { useLabel } from "../base/NineZone";
import { PanelSideContext } from "../widget-panels/Panel";
import { useLayout } from "../base/LayoutStore";
/** @internal */
export function WidgetOverflow(props) {
    const [open, setOpen] = React.useState(false);
    const ref = React.useRef(null);
    const [targetRef, target] = useRefState();
    const resizeObserverRef = useResizeObserver(props.onResize);
    const refs = useRefs(ref, resizeObserverRef);
    const handleClick = React.useCallback((e) => {
        e.preventDefault();
        setOpen((prev) => !prev);
    }, []);
    const handleClose = React.useCallback(() => {
        setOpen(false);
    }, []);
    usePanelPopup(handleClose);
    const className = classnames("nz-widget-overflow", props.hidden && "nz-hidden");
    const overflowContext = React.useMemo(() => {
        return {
            close: handleClose,
        };
    }, [handleClose]);
    const moreWidgetsTitle = 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(WidgetOverflowContext.Provider, { value: overflowContext },
            React.createElement(WidgetMenu, { children: props.children, open: open, onClose: handleClose, target: target }))));
}
/** @internal */
export const WidgetOverflowContext = React.createContext(undefined);
WidgetOverflowContext.displayName = "nz:WidgetOverflowContext";
function usePanelPopup(onClose) {
    const side = React.useContext(PanelSideContext);
    const collapsed = useLayout((state) => side ? state.panels[side].collapsed : false);
    React.useEffect(() => {
        if (collapsed) {
            onClose();
        }
    }, [collapsed, onClose]);
}
//# sourceMappingURL=Overflow.js.map