"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.useUserControlledHiddenTabs = exports.useAddTab = exports.AddWidgetButton = exports.label = void 0;
const React = require("react");
const PreviewFeatures_1 = require("../PreviewFeatures");
const itwinui_react_1 = require("@itwin/itwinui-react");
const Button_1 = require("../../layout/widget/Button");
const LayoutStore_1 = require("../../layout/base/LayoutStore");
const TabLocation_1 = require("../../layout/state/TabLocation");
const itwinui_icons_react_1 = require("@itwin/itwinui-icons-react");
const NineZone_1 = require("../../layout/base/NineZone");
const Widget_1 = require("../../layout/widget/Widget");
const MoreButton_1 = require("../widget-action-dropdown/MoreButton");
/** @internal */
exports.label = "Add widget";
/** @internal */
function AddWidgetButton() {
    const dispatch = React.useContext(NineZone_1.NineZoneDispatchContext);
    const widgetId = React.useContext(Widget_1.WidgetIdContext);
    const dropdownContext = React.useContext(MoreButton_1.WidgetActionDropdownContext);
    const tabs = useUserControlledHiddenTabs();
    if (!widgetId)
        return null;
    const getMenuItems = (close) => tabs.map((tab) => {
        return (React.createElement(itwinui_react_1.MenuItem, { key: tab.id, onClick: () => {
                dispatch({
                    type: "WIDGET_TAB_ADD_TO_WIDGET",
                    id: tab.id,
                    widgetId,
                });
                dispatch({
                    type: "WIDGET_TAB_OPEN",
                    id: tab.id,
                });
                close?.();
            } }, tab.label));
    });
    const icon = React.createElement(itwinui_icons_react_1.SvgAdd, null);
    if (dropdownContext !== undefined) {
        return (React.createElement(itwinui_react_1.MenuItem, { startIcon: icon, subMenuItems: getMenuItems(dropdownContext.onClose) }, exports.label));
    }
    return (React.createElement(itwinui_react_1.DropdownMenu, { menuItems: (close) => getMenuItems(close) },
        React.createElement(Button_1.TabBarButton, { label: exports.label }, icon)));
}
exports.AddWidgetButton = AddWidgetButton;
/** @internal */
function useAddTab() {
    const tabs = useUserControlledHiddenTabs();
    const { controlWidgetVisibility } = (0, PreviewFeatures_1.usePreviewFeatures)();
    if (tabs.length === 0)
        return false;
    return !!controlWidgetVisibility;
}
exports.useAddTab = useAddTab;
/** Returns hidden tabs whose visibility can be manipulated by the end user.
 * @internal
 */
function useUserControlledHiddenTabs() {
    const { controlWidgetVisibility } = (0, PreviewFeatures_1.usePreviewFeatures)();
    const hiddenTabs = (0, LayoutStore_1.useLayout)((state) => {
        if (!controlWidgetVisibility)
            return [];
        const tabs = Object.values(state.tabs);
        const toolSettingsTabId = state.toolSettings?.tabId;
        return tabs.filter((tab) => {
            if (tab.id === toolSettingsTabId)
                return false;
            const location = (0, TabLocation_1.getTabLocation)(state, tab.id);
            // Tab is visible.
            if (!!location)
                return false;
            // Case where only specific tabs are user-controlled.
            if (Array.isArray(controlWidgetVisibility)) {
                return controlWidgetVisibility.includes(tab.id);
            }
            return true;
        });
    }, true);
    return hiddenTabs;
}
exports.useUserControlledHiddenTabs = useUserControlledHiddenTabs;
//# sourceMappingURL=AddWidgetButton.js.map