"use strict";
/*---------------------------------------------------------------------------------------------
* 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
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.SplitterPaneTarget = exports.FrameworkStagePanel = void 0;
require("./FrameworkStagePanel.scss");
const classnames_1 = require("classnames");
const React = require("react");
const appui_layout_react_1 = require("@itwin/appui-layout-react");
const SafeAreaContext_1 = require("../safearea/SafeAreaContext");
const WidgetStack_1 = require("../widgets/WidgetStack");
const StagePanel_1 = require("./StagePanel");
const StagePanelDef_1 = require("./StagePanelDef");
const InternalFrontstageManager_1 = require("../frontstage/InternalFrontstageManager");
/** Stage Panel React component.
 * @internal
 */
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 = (0, classnames_1.default)(this.props.panelState === StagePanelDef_1.StagePanelState.Off && /* istanbul ignore next */ "uifw-stagepanel-off");
        const className = (0, classnames_1.default)("uifw-stagepanel", panelStateClassName);
        const paneCount = this.props.stagePanelWidgets.length + this.props.panel.panes.length;
        const type = (0, StagePanel_1.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_1.SafeAreaContext.Consumer, null, (safeAreaInsets) => (React.createElement(appui_layout_react_1.StagePanelTarget, { className: panelStateClassName, onTargetChanged: this._handleTargetChanged, safeAreaInsets: safeAreaInsets, type: type }))));
        }
        const isSplitterTargetVisible = isTargetVisible && !this.props.isTargeted;
        const isVertical = appui_layout_react_1.StagePanelTypeHelpers.isVertical(type);
        return (React.createElement(SafeAreaContext_1.SafeAreaContext.Consumer, null, (safeAreaInsets) => {
            if (this.props.isInFooterMode)
                safeAreaInsets &= ~appui_layout_react_1.SafeAreaInsets.Bottom;
            return (React.createElement(appui_layout_react_1.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(appui_layout_react_1.SplitterTarget, { isVertical: isVertical, onTargetChanged: this._handleTargetChanged, paneCount: paneCount, style: {
                                ...isTargetVisible ? {} : { display: "none" },
                            } }),
                        React.createElement(appui_layout_react_1.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_1.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 = (0, StagePanel_1.getNestedStagePanelKey)(this.props.location);
        const nestedPanelsManager = InternalFrontstageManager_1.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 = (0, StagePanel_1.getStagePanelType)(location);
        const size = appui_layout_react_1.StagePanelTypeHelpers.isVertical(type) ? clientRect.width : clientRect.height;
        this.props.changeHandler.handlePanelInitialize(location, size);
    }
}
exports.FrameworkStagePanel = FrameworkStagePanel;
/** @internal */
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(appui_layout_react_1.SplitterPaneTarget, { onTargetChanged: this._handleTargetChanged }));
    }
}
exports.SplitterPaneTarget = SplitterPaneTarget;
//# sourceMappingURL=FrameworkStagePanel.js.map