"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.useDropdownFeatures = exports.WidgetActionDropdownContext = exports.MoreButton = void 0;
const React = require("react");
const core_bentley_1 = require("@itwin/core-bentley");
const itwinui_icons_react_1 = require("@itwin/itwinui-icons-react");
const itwinui_react_1 = require("@itwin/itwinui-react");
const Button_1 = require("../../layout/widget/Button");
const PreviewFeatures_1 = require("../PreviewFeatures");
const LayoutStore_1 = require("../../layout/base/LayoutStore");
const Panel_1 = require("../../layout/widget-panels/Panel");
/** @internal */
function MoreButton(props) {
    return (React.createElement(itwinui_react_1.DropdownMenu, { placement: "bottom-end", menuItems: (onClose) => [
            React.createElement(exports.WidgetActionDropdownContext.Provider, { key: 0, value: { onClose } },
                React.createElement(CloseOnPanelCollapse, null),
                props.children),
        ] },
        React.createElement("div", { style: { height: "100%", display: "flex", marginInline: "0.25em" } },
            React.createElement(Button_1.TabBarButton, { label: "More actions" },
                React.createElement(itwinui_icons_react_1.SvgMoreVertical, null)))));
}
exports.MoreButton = MoreButton;
/** @internal */
exports.WidgetActionDropdownContext = React.createContext(undefined);
function CloseOnPanelCollapse() {
    const context = React.useContext(exports.WidgetActionDropdownContext);
    (0, core_bentley_1.assert)(!!context);
    const side = React.useContext(Panel_1.PanelSideContext);
    const { onClose } = context;
    const collapsed = (0, LayoutStore_1.useLayout)((state) => {
        if (!side)
            return false;
        return state.panels[side].collapsed;
    });
    React.useEffect(() => {
        if (collapsed)
            onClose();
    }, [collapsed, onClose]);
    return null;
}
const order = [
    "pin",
    "maximize",
    "popout",
    "horizontalAlign",
    "dock",
    "sendBack",
];
/** @internal */
function useDropdownFeatures(features) {
    const { widgetActionDropdown } = (0, PreviewFeatures_1.usePreviewFeatures)();
    const threshold = widgetActionDropdown?.threshold ?? Infinity;
    const isDropdown = features.length > threshold;
    if (!isDropdown)
        return [features, false];
    const sorted = [...features].sort((a, b) => order.indexOf(a) - order.indexOf(b));
    return [sorted, true];
}
exports.useDropdownFeatures = useDropdownFeatures;
//# sourceMappingURL=MoreButton.js.map