/*---------------------------------------------------------------------------------------------
* 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 * as React from "react";
import { BadgeUtilities, IconHelper, Rectangle } from "@itwin/core-react";
import { HandleMode, Stacked as NZ_WidgetStack, Tab, TabGroup, TabMode, TabSeparator, VerticalAnchor, VerticalAnchorHelpers, } from "@itwin/appui-layout-react";
import { UiFramework } from "../UiFramework";
/** Widget stack React component.
 * @internal
 */
export class WidgetStack extends React.PureComponent {
    constructor() {
        super(...arguments);
        this._widgetStack = React.createRef();
        this._handleOnWidgetResize = (resizeBy, handle, filledHeightDiff) => {
            this.props.widgetChangeHandler.handleResize(this.props.widgets[0], resizeBy, handle, filledHeightDiff);
        };
        // eslint-disable-next-line deprecation/deprecation
        this._handleTabDragStart = (widgetId, tabIndex, initialPosition, firstTabBounds) => {
            if (!this._widgetStack.current)
                return;
            const tabBounds = Rectangle.create(firstTabBounds);
            const stackedWidgetBounds = Rectangle.create(this._widgetStack.current.getBounds());
            const offsetToFirstTab = stackedWidgetBounds.topLeft().getOffsetTo(tabBounds.topLeft());
            const isHorizontal = this.props.verticalAnchor === VerticalAnchor.BottomPanel || this.props.verticalAnchor === VerticalAnchor.TopPanel;
            let widgetBounds;
            if (isHorizontal)
                widgetBounds = stackedWidgetBounds.offsetX(offsetToFirstTab.x);
            else
                widgetBounds = stackedWidgetBounds.offsetY(offsetToFirstTab.y);
            this.props.widgetChangeHandler.handleTabDragStart(widgetId, tabIndex, initialPosition, widgetBounds);
        };
        this._handleTabDragEnd = () => {
            this.props.widgetChangeHandler.handleTabDragEnd();
        };
        // eslint-disable-next-line deprecation/deprecation
        this._handleTabClick = (widgetId, tabIndex) => {
            this.props.widgetChangeHandler.handleTabClick(widgetId, tabIndex);
        };
        this._handleTabDrag = (dragged) => {
            this.props.widgetChangeHandler.handleTabDrag(dragged);
        };
    }
    render() {
        const tabCount = this.props.widgets.reduce((acc, widgetId) => {
            const tabs = this.props.widgetTabs[widgetId];
            return acc + tabs.length;
        }, 0);
        if (tabCount === 0)
            return null;
        return (React.createElement(NZ_WidgetStack, { className: this.props.className, contentRef: this.props.openWidgetId ? this.props.getWidgetContentRef(this.props.openWidgetId) : undefined, disabledResizeHandles: this.props.disabledResizeHandles, fillZone: this.props.fillZone || this.props.isInStagePanel, horizontalAnchor: this.props.horizontalAnchor, isCollapsed: this.props.isCollapsed, isTabBarVisible: this.props.isInStagePanel, isDragged: !!this.props.draggedWidget, isFloating: this.props.isFloating, isOpen: !!this.props.openWidgetId, onMouseEnter: UiFramework.visibility.handleWidgetMouseEnter, onResize: this.props.isInStagePanel ? undefined : this._handleOnWidgetResize, ref: this._widgetStack, style: this.props.style, tabs: React.createElement(WidgetStackTabs, { activeTabIndex: this.props.activeTabIndex, draggedWidget: this.props.draggedWidget, horizontalAnchor: this.props.horizontalAnchor, isCollapsed: this.props.isCollapsed, isProtruding: !this.props.isInStagePanel, onTabClick: this._handleTabClick, onTabDrag: this._handleTabDrag, onTabDragEnd: this._handleTabDragEnd, onTabDragStart: this._handleTabDragStart, openWidgetId: this.props.openWidgetId, verticalAnchor: this.props.verticalAnchor, widgets: this.props.widgets, widgetTabs: this.props.widgetTabs }), verticalAnchor: this.props.verticalAnchor }));
    }
}
/** Tabs of [[WidgetStack]] component.
 * @internal
 */
export class WidgetStackTabs extends React.PureComponent {
    render() {
        let renderIndex = -1;
        return this.props.widgets.map((widgetId) => {
            const tabs = this.props.widgetTabs[widgetId];
            if (tabs.length <= 0)
                return null;
            renderIndex++;
            // istanbul ignore next
            return (React.createElement(React.Fragment, { key: widgetId },
                renderIndex < 1 ? undefined : React.createElement(TabSeparator, { isHorizontal: VerticalAnchorHelpers.isHorizontal(this.props.verticalAnchor) }),
                React.createElement(WidgetStackTabGroup, { activeTabIndex: this.props.activeTabIndex, draggedWidget: this.props.draggedWidget, horizontalAnchor: this.props.horizontalAnchor, isCollapsed: this.props.isCollapsed, isProtruding: this.props.isProtruding, isStacked: this.props.widgets.length > 1, onTabClick: this.props.onTabClick, onTabDrag: this.props.onTabDrag, onTabDragEnd: this.props.onTabDragEnd, onTabDragStart: this.props.onTabDragStart, openWidgetId: this.props.openWidgetId, tabs: tabs, verticalAnchor: this.props.verticalAnchor, widgetId: widgetId })));
        });
    }
}
/** Widget tab group used in [[WidgetStackTabs]] component.
 * @internal
 */
export class WidgetStackTabGroup extends React.PureComponent {
    constructor() {
        super(...arguments);
        this._firstTab = React.createRef();
        this._handleTabDragStart = (tabIndex, initialPosition) => {
            if (!this._firstTab.current)
                return;
            const firstTabBounds = Rectangle.create(this._firstTab.current.getBounds()).toProps();
            this.props.onTabDragStart(this.props.widgetId, tabIndex, initialPosition, firstTabBounds);
        };
        this._handleTabClick = (tabIndex) => {
            this.props.onTabClick(this.props.widgetId, tabIndex);
        };
    }
    render() {
        const lastPosition = this.props.draggedWidget ? this.props.draggedWidget.lastPosition : undefined;
        const isWidgetStackOpen = !!this.props.openWidgetId;
        const isWidgetOpen = this.props.openWidgetId === this.props.widgetId;
        const tabs = this.props.tabs.map((tab, index) => {
            const mode = !isWidgetStackOpen ? TabMode.Closed : isWidgetOpen && this.props.activeTabIndex === index ? TabMode.Active : TabMode.Open;
            return (React.createElement(WidgetStackTab, { horizontalAnchor: this.props.horizontalAnchor, iconSpec: tab.iconSpec, index: index, badgeType: tab.badgeType, isCollapsed: this.props.isCollapsed, isProtruding: this.props.isProtruding, key: `${this.props.widgetId}-${index}`, lastPosition: lastPosition, mode: mode, onClick: this._handleTabClick, onDrag: this.props.onTabDrag, onDragEnd: this.props.onTabDragEnd, onDragStart: this._handleTabDragStart, tabRef: index === 0 ? this._firstTab : undefined, title: tab.title, verticalAnchor: this.props.verticalAnchor }));
        });
        if (tabs.length > 1) {
            return (React.createElement(TabGroup, { handle: this.getTabHandleMode(), horizontalAnchor: this.props.horizontalAnchor, isCollapsed: this.props.isCollapsed, verticalAnchor: this.props.verticalAnchor }, tabs));
        }
        return tabs;
    }
    getTabHandleMode() {
        if (this.props.draggedWidget && this.props.draggedWidget.id === this.props.widgetId && this.props.draggedWidget.isUnmerge)
            return HandleMode.Visible;
        if (this.props.isStacked)
            return HandleMode.Hovered;
        return HandleMode.Timedout;
    }
}
/** Tab used in [[WidgetStackTabGroup]] component.
 * @internal
 */
export class WidgetStackTab extends React.PureComponent {
    constructor() {
        super(...arguments);
        this._handleDragStart = (initialPosition) => {
            this.props.onDragStart(this.props.index, initialPosition);
        };
        this._handleClick = () => {
            this.props.onClick(this.props.index);
        };
    }
    render() {
        return (React.createElement(Tab, { badge: BadgeUtilities.getComponentForBadgeType(this.props.badgeType), horizontalAnchor: this.props.horizontalAnchor, isCollapsed: this.props.isCollapsed, isProtruding: this.props.isProtruding, lastPosition: this.props.lastPosition, mode: this.props.mode, onClick: this._handleClick, onDrag: this.props.onDrag, onDragEnd: this.props.onDragEnd, onDragStart: this._handleDragStart, ref: this.props.tabRef, title: this.props.title, verticalAnchor: this.props.verticalAnchor }, IconHelper.getIconReactNode(this.props.iconSpec)));
    }
}
//# sourceMappingURL=WidgetStack.js.map