"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.useBorders = exports.useMode = exports.PanelWidget = void 0;
require("./PanelWidget.scss");
const classnames_1 = require("classnames");
const React = require("react");
const core_bentley_1 = require("@itwin/core-bentley");
const Panel_1 = require("../widget-panels/Panel");
const ContentContainer_1 = require("./ContentContainer");
const TabBar_1 = require("./TabBar");
const Widget_1 = require("./Widget");
const WidgetOutline_1 = require("../outline/WidgetOutline");
const WidgetTarget_1 = require("../target/WidgetTarget");
const PanelState_1 = require("../state/PanelState");
const LayoutStore_1 = require("../base/LayoutStore");
const WidgetStateHelpers_1 = require("../state/internal/WidgetStateHelpers");
/** @internal */
exports.PanelWidget = React.forwardRef(function PanelWidget({ widgetId }, ref) {
    const side = React.useContext(Panel_1.PanelSideContext);
    (0, core_bentley_1.assert)(!!side);
    const widgetsLength = (0, LayoutStore_1.useLayout)((state) => {
        const panel = state.panels[side];
        return panel.widgets.length;
    });
    const minimized = (0, LayoutStore_1.useLayout)((state) => {
        const widget = (0, WidgetStateHelpers_1.getWidgetState)(state, widgetId);
        return widget.minimized;
    });
    const horizontal = (0, Panel_1.isHorizontalPanelSide)(side);
    const mode = useMode(widgetId);
    const borders = useBorders(widgetId);
    const showTarget = widgetsLength !== 1;
    const className = (0, classnames_1.default)("nz-widget-panelWidget", horizontal && "nz-horizontal", `nz-${mode}`, borders);
    const content = React.useMemo(() => (React.createElement(ContentContainer_1.WidgetContentContainer, null,
        showTarget && React.createElement(WidgetTarget_1.WidgetTarget, null),
        React.createElement(WidgetOutline_1.WidgetOutline, null))), [showTarget]);
    return (React.createElement(Widget_1.WidgetProvider, { id: widgetId },
        React.createElement(Widget_1.Widget, { className: className, ref: ref },
            React.createElement(TabBar_1.WidgetTabBar, { separator: (0, Panel_1.isHorizontalPanelSide)(side) ? true : !minimized }),
            content)));
});
function findFillWidget(state, side) {
    const panelWidgets = state.panels[side].widgets;
    return panelWidgets.find((widgetId) => {
        const widget = (0, WidgetStateHelpers_1.getWidgetState)(state, widgetId);
        if (widget.minimized)
            return false;
        const tabId = widget.activeTabId;
        const tab = state.tabs[tabId];
        if (!tab.preferredPanelWidgetSize)
            return true;
        return false;
    });
}
/** @internal */
function useMode(widgetId) {
    const side = React.useContext(Panel_1.PanelSideContext);
    (0, core_bentley_1.assert)(!!side);
    return (0, LayoutStore_1.useLayout)((state) => {
        const fillWidget = findFillWidget(state, side);
        // Force `fill` for last panel widget that is not minimized.
        if (!fillWidget) {
            const panel = state.panels[side];
            for (let i = panel.widgets.length - 1; i >= 0; i--) {
                const wId = panel.widgets[i];
                const w = (0, WidgetStateHelpers_1.getWidgetState)(state, wId);
                if (w.minimized)
                    continue;
                if (wId === widgetId)
                    return "fill";
                break;
            }
        }
        const widget = (0, WidgetStateHelpers_1.getWidgetState)(state, widgetId);
        if (widget.minimized)
            return "minimized";
        const tabId = widget.activeTabId;
        const tab = state.tabs[tabId];
        return tab.preferredPanelWidgetSize ? "fit" : "fill";
    });
}
exports.useMode = useMode;
/** @internal */
function useBorders(widgetId) {
    const side = React.useContext(Panel_1.PanelSideContext);
    (0, core_bentley_1.assert)(!!side);
    return (0, LayoutStore_1.useLayout)((state) => {
        const panels = state.panels;
        const panel = panels[side];
        const toolSettings = state.toolSettings;
        const isHorizontal = (0, Panel_1.isHorizontalPanelSide)(panel.side);
        const isVertical = !isHorizontal;
        const isFirst = panel.widgets[0] === widgetId;
        const isLast = panel.widgets[panel.widgets.length - 1] === widgetId;
        const isTopMostPanelBorder = panel.side === "top" ||
            (isVertical && !panels.top.span) ||
            (isVertical && panels.top.span && panels.top.collapsed) ||
            (isVertical && panels.top.widgets.length === 0);
        let top = true;
        let bottom = true;
        let left = true;
        let right = true;
        if (panel.side === "bottom") {
            bottom = false;
        }
        if (isVertical && isLast) {
            bottom = false;
        }
        if (isTopMostPanelBorder && toolSettings?.type === "docked") {
            top = false;
        }
        if (isVertical && !isFirst) {
            top = false;
        }
        if (isVertical &&
            panels.top.span &&
            !panels.top.collapsed &&
            panels.top.widgets.length > 0) {
            top = false;
        }
        if (isHorizontal && !isFirst) {
            left = false;
        }
        if ((0, PanelState_1.isHorizontalPanelState)(panel) &&
            !panel.span &&
            isFirst &&
            !panels.left.collapsed &&
            panels.left.widgets.length > 0) {
            left = false;
        }
        if ((0, PanelState_1.isHorizontalPanelState)(panel) &&
            !panel.span &&
            isLast &&
            !panels.right.collapsed &&
            panels.right.widgets.length > 0) {
            right = false;
        }
        return {
            "nz-border-top": top,
            "nz-border-bottom": bottom,
            "nz-border-left": left,
            "nz-border-right": right,
        };
    }, true);
}
exports.useBorders = useBorders;
//# sourceMappingURL=PanelWidget.js.map