"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.getNestedStagePanelKey = exports.getStagePanelType = exports.StagePanel = void 0;
const React = require("react");
const appui_abstract_1 = require("@itwin/appui-abstract");
const appui_layout_react_1 = require("@itwin/appui-layout-react");
const FrameworkStagePanel_1 = require("./FrameworkStagePanel");
const StagePanelDef_1 = require("./StagePanelDef");
const UiFramework_1 = require("../UiFramework");
/** Frontstage Panel React component.
 * @deprecated in 3.6. Use [[StagePanelConfig]] instead.
 * @public
 */
class StagePanel extends React.Component {
    constructor(props) {
        var _a;
        super(props);
        this._handleRenderPane = (widgetDefId) => {
            const runtimeProps = this.props.runtimeProps;
            if (!runtimeProps)
                return null;
            const widgetDef = runtimeProps.panelDef.findWidgetDef(widgetDefId);
            if (!widgetDef || !widgetDef.isVisible)
                return null;
            return (React.createElement("div", { style: {
                    height: "100%",
                    display: runtimeProps.panel.isCollapsed ? "none" : "block",
                } }, widgetDef.reactNode));
        };
        this._handlePanelStateChangedEvent = ({ panelDef, panelState }) => {
            var _a;
            // istanbul ignore else
            if (panelDef !== ((_a = this.props.runtimeProps) === null || _a === void 0 ? void 0 : _a.panelDef))
                return;
            // istanbul ignore next
            this.setState({
                panelState,
            });
        };
        this._handleWidgetStateChangedEvent = ({ widgetDef }) => {
            const runtimeProps = this.props.runtimeProps;
            if (!runtimeProps)
                return;
            if (!runtimeProps.panelDef.findWidgetDef(widgetDef.id))
                return;
            this.setState({
                stagePanelWidgets: this._getVisibleStagePanelWidgets(),
            });
        };
        const panelState = (_a = this.props.runtimeProps) === null || _a === void 0 ? void 0 : _a.panelDef.panelState;
        this.state = {
            panelState: panelState === undefined ? StagePanelDef_1.StagePanelState.Open : panelState,
            stagePanelWidgets: this._getVisibleStagePanelWidgets(),
        };
    }
    static initializeStagePanelDef(panelDef, props, panelLocation) {
        panelDef.initializeFromProps(props, panelLocation);
    }
    componentDidMount() {
        UiFramework_1.UiFramework.frontstages.onPanelStateChangedEvent.addListener(this._handlePanelStateChangedEvent);
        UiFramework_1.UiFramework.frontstages.onWidgetStateChangedEvent.addListener(this._handleWidgetStateChangedEvent);
    }
    componentDidUpdate(prevProps) {
        var _a, _b;
        if (((_a = prevProps.runtimeProps) === null || _a === void 0 ? void 0 : _a.panelDef) !== ((_b = this.props.runtimeProps) === null || _b === void 0 ? void 0 : _b.panelDef)) {
            this.setState({
                stagePanelWidgets: this._getVisibleStagePanelWidgets(),
            });
        }
    }
    componentWillUnmount() {
        UiFramework_1.UiFramework.frontstages.onPanelStateChangedEvent.removeListener(this._handlePanelStateChangedEvent);
        UiFramework_1.UiFramework.frontstages.onWidgetStateChangedEvent.removeListener(this._handleWidgetStateChangedEvent);
    }
    render() {
        const { applicationData, defaultState, runtimeProps, maxSize, size, ...props } = this.props; // eslint-disable-line @typescript-eslint/no-unused-vars
        if (!runtimeProps)
            return null;
        const { stagePanelChangeHandler, panelDef, ...otherRuntimeProps } = runtimeProps;
        return (React.createElement(FrameworkStagePanel_1.FrameworkStagePanel, { changeHandler: stagePanelChangeHandler, initialSize: size, location: panelDef.location, renderPane: this._handleRenderPane, stagePanelWidgets: this.state.stagePanelWidgets, panelState: this.state.panelState, maxSize: typeof maxSize === "number" ? maxSize : undefined, ...props, ...otherRuntimeProps }));
    }
    _getVisibleStagePanelWidgets() {
        var _a;
        const panelDef = (_a = this.props.runtimeProps) === null || _a === void 0 ? void 0 : _a.panelDef;
        if (!panelDef)
            return [];
        const visibleWidgets = panelDef.widgetDefs.filter((wd) => wd.isVisible);
        return visibleWidgets.map((widgetDef) => widgetDef.id);
    }
}
exports.StagePanel = StagePanel;
StagePanel.defaultProps = {
    resizable: true,
};
/** @internal */
const getStagePanelType = (location) => {
    switch (location) {
        case appui_abstract_1.StagePanelLocation.Bottom:
        case appui_abstract_1.StagePanelLocation.BottomMost:
            return appui_layout_react_1.StagePanelType.Bottom;
        case appui_abstract_1.StagePanelLocation.Left:
            return appui_layout_react_1.StagePanelType.Left;
        case appui_abstract_1.StagePanelLocation.Right:
            return appui_layout_react_1.StagePanelType.Right;
        case appui_abstract_1.StagePanelLocation.Top:
        case appui_abstract_1.StagePanelLocation.TopMost:
            return appui_layout_react_1.StagePanelType.Top;
    }
};
exports.getStagePanelType = getStagePanelType;
/** @internal */
const getNestedStagePanelKey = (location) => {
    switch (location) {
        case appui_abstract_1.StagePanelLocation.Bottom:
            return {
                id: "inner",
                type: appui_layout_react_1.StagePanelType.Bottom,
            };
        case appui_abstract_1.StagePanelLocation.BottomMost:
            return {
                id: "outer",
                type: appui_layout_react_1.StagePanelType.Bottom,
            };
        case appui_abstract_1.StagePanelLocation.Left:
            return {
                id: "inner",
                type: appui_layout_react_1.StagePanelType.Left,
            };
        case appui_abstract_1.StagePanelLocation.Right:
            return {
                id: "inner",
                type: appui_layout_react_1.StagePanelType.Right,
            };
        case appui_abstract_1.StagePanelLocation.Top:
            return {
                id: "inner",
                type: appui_layout_react_1.StagePanelType.Top,
            };
        case appui_abstract_1.StagePanelLocation.TopMost:
            return {
                id: "outer",
                type: appui_layout_react_1.StagePanelType.Top,
            };
    }
};
exports.getNestedStagePanelKey = getNestedStagePanelKey;
//# sourceMappingURL=StagePanel.js.map