/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/* eslint-disable deprecation/deprecation */
/** @packageDocumentation
 * @module Frontstage
 */
import "./FrameworkStagePanel.scss";
import classnames from "classnames";
import * as React from "react";
import { SplitterPaneTarget as NZ_SplitterPaneTarget, StagePanel as NZ_StagePanel, SafeAreaInsets, Splitter, SplitterTarget, StagePanelTarget, StagePanelTypeHelpers, } from "@itwin/appui-layout-react";
import { SafeAreaContext } from "../safearea/SafeAreaContext";
import { WidgetStack } from "../widgets/WidgetStack";
import { getNestedStagePanelKey, getStagePanelType } from "./StagePanel";
import { StagePanelState } from "./StagePanelDef";
import { InternalFrontstageManager } from "../frontstage/InternalFrontstageManager";
/** Stage Panel React component.
 * @internal
 */
export class FrameworkStagePanel extends React.PureComponent {
    constructor() {
        super(...arguments);
        this._measurer = React.createRef();
        this._handleResize = (resizeBy) => {
            this.props.changeHandler.handlePanelResize(this.props.location, resizeBy);
        };
        this._handleTargetChanged = (isTargeted) => {
            this.props.changeHandler.handlePanelTargetChange(isTargeted ? this.props.location : undefined);
        };
        this._handleToggleCollapse = () => {
            this.props.changeHandler.handleTogglePanelCollapse(this.props.location);
        };
        this._handlePaneTargetChanged = (paneIndex) => {
            this.props.changeHandler.handlePanelPaneTargetChange(this.props.location, paneIndex);
        };
    }
    componentDidMount() {
        this.initializeSize();
        this.setMinMaxSize();
    }
    componentDidUpdate() {
        this.initializeSize();
        this.setMinMaxSize();
    }
    render() {
        const panelStateClassName = classnames(this.props.panelState === StagePanelState.Off && /* istanbul ignore next */ "uifw-stagepanel-off");
        const className = classnames("uifw-stagepanel", panelStateClassName);
        const paneCount = this.props.stagePanelWidgets.length + this.props.panel.panes.length;
        const type = getStagePanelType(this.props.location);
        const isTargetVisible = !!this.props.draggedWidgetId && this.props.allowedZones && this.props.allowedZones.some((z) => this.props.draggedWidgetId === z);
        if (paneCount === 0) {
            if (!isTargetVisible)
                return null;
            return (React.createElement(SafeAreaContext.Consumer, null, (safeAreaInsets) => (React.createElement(StagePanelTarget, { className: panelStateClassName, onTargetChanged: this._handleTargetChanged, safeAreaInsets: safeAreaInsets, type: type }))));
        }
        const isSplitterTargetVisible = isTargetVisible && !this.props.isTargeted;
        const isVertical = StagePanelTypeHelpers.isVertical(type);
        return (React.createElement(SafeAreaContext.Consumer, null, (safeAreaInsets) => {
            if (this.props.isInFooterMode)
                safeAreaInsets &= ~SafeAreaInsets.Bottom;
            return (React.createElement(NZ_StagePanel, { className: className, onResize: this.props.resizable ? this._handleResize : undefined, onToggleCollapse: this._handleToggleCollapse, safeAreaInsets: safeAreaInsets, size: this.props.panel.isCollapsed ? undefined : this.props.panel.size, type: type },
                React.createElement("div", { ref: this._measurer, style: { width: "100%", height: "100%", position: "absolute", zIndex: -1 } }),
                React.createElement("div", { className: "uifw-content" },
                    this.props.panel.isCollapsed ? undefined : this.props.header,
                    React.createElement("div", { className: "uifw-widgets" },
                        React.createElement(SplitterTarget, { isVertical: isVertical, onTargetChanged: this._handleTargetChanged, paneCount: paneCount, style: {
                                ...isTargetVisible ? {} : { display: "none" },
                            } }),
                        React.createElement(Splitter, { isGripHidden: this.props.panel.isCollapsed, isVertical: isVertical },
                            this.props.stagePanelWidgets.map((widgetId) => {
                                return (React.createElement(React.Fragment, { key: `wd-${widgetId}` }, this.props.renderPane(widgetId)));
                            }),
                            this.props.panel.panes.map((pane, index) => {
                                const openWidgetId = pane.widgets.find((wId) => this.props.widgets[wId].tabIndex >= 0);
                                const activeTabIndex = openWidgetId ? this.props.widgets[openWidgetId].tabIndex : 0;
                                const firstWidget = this.props.widgets[pane.widgets[0]];
                                return (React.createElement("div", { key: `w-${index}`, style: { height: "100%", position: "relative" } },
                                    React.createElement(WidgetStack, { activeTabIndex: activeTabIndex, disabledResizeHandles: undefined, draggedWidget: undefined, fillZone: true, getWidgetContentRef: this.props.getWidgetContentRef, horizontalAnchor: firstWidget.horizontalAnchor, isCollapsed: this.props.panel.isCollapsed, isFloating: false, isInStagePanel: true, openWidgetId: openWidgetId, verticalAnchor: firstWidget.verticalAnchor, widgets: pane.widgets, widgetTabs: this.props.widgetTabs, widgetChangeHandler: this.props.widgetChangeHandler }),
                                    isSplitterTargetVisible && React.createElement(SplitterPaneTarget, { onTargetChanged: this._handlePaneTargetChanged, paneIndex: index })));
                            }))))));
        }));
    }
    setMinMaxSize() {
        const panel = getNestedStagePanelKey(this.props.location);
        const nestedPanelsManager = InternalFrontstageManager.NineZoneManager.getNestedPanelsManager();
        this.props.minSize && ( /* istanbul ignore next */nestedPanelsManager.getPanelsManager(panel.id).getPanelManager(panel.type).minSize = this.props.minSize);
        this.props.maxSize && ( /* istanbul ignore next */nestedPanelsManager.getPanelsManager(panel.id).getPanelManager(panel.type).maxSize = this.props.maxSize);
    }
    initializeSize() {
        if (this.props.panel.size !== undefined || !this._measurer.current)
            return;
        const location = this.props.location;
        if (this.props.initialSize) {
            this.props.changeHandler.handlePanelInitialize(location, this.props.initialSize);
            return;
        }
        const clientRect = this._measurer.current.getBoundingClientRect();
        const type = getStagePanelType(location);
        const size = StagePanelTypeHelpers.isVertical(type) ? clientRect.width : clientRect.height;
        this.props.changeHandler.handlePanelInitialize(location, size);
    }
}
/** @internal */
export class SplitterPaneTarget extends React.PureComponent {
    constructor() {
        super(...arguments);
        this._handleTargetChanged = (isTargeted) => {
            const target = isTargeted ? this.props.paneIndex : undefined;
            this.props.onTargetChanged(target);
        };
    }
    render() {
        return (React.createElement(NZ_SplitterPaneTarget, { onTargetChanged: this._handleTargetChanged }));
    }
}
//# sourceMappingURL=FrameworkStagePanel.js.map