/*---------------------------------------------------------------------------------------------
* 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 * as React from "react";
import { StagePanelLocation } from "@itwin/appui-abstract";
import { StagePanelType as NZ_StagePanelType, } from "@itwin/appui-layout-react";
import { FrameworkStagePanel } from "./FrameworkStagePanel";
import { StagePanelState as StagePanelState } from "./StagePanelDef";
import { UiFramework } from "../UiFramework";
/** Frontstage Panel React component.
 * @deprecated in 3.6. Use [[StagePanelConfig]] instead.
 * @public
 */
export 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 ? StagePanelState.Open : panelState,
            stagePanelWidgets: this._getVisibleStagePanelWidgets(),
        };
    }
    static initializeStagePanelDef(panelDef, props, panelLocation) {
        panelDef.initializeFromProps(props, panelLocation);
    }
    componentDidMount() {
        UiFramework.frontstages.onPanelStateChangedEvent.addListener(this._handlePanelStateChangedEvent);
        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.frontstages.onPanelStateChangedEvent.removeListener(this._handlePanelStateChangedEvent);
        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, { 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);
    }
}
StagePanel.defaultProps = {
    resizable: true,
};
/** @internal */
export const getStagePanelType = (location) => {
    switch (location) {
        case StagePanelLocation.Bottom:
        case StagePanelLocation.BottomMost:
            return NZ_StagePanelType.Bottom;
        case StagePanelLocation.Left:
            return NZ_StagePanelType.Left;
        case StagePanelLocation.Right:
            return NZ_StagePanelType.Right;
        case StagePanelLocation.Top:
        case StagePanelLocation.TopMost:
            return NZ_StagePanelType.Top;
    }
};
/** @internal */
export const getNestedStagePanelKey = (location) => {
    switch (location) {
        case StagePanelLocation.Bottom:
            return {
                id: "inner",
                type: NZ_StagePanelType.Bottom,
            };
        case StagePanelLocation.BottomMost:
            return {
                id: "outer",
                type: NZ_StagePanelType.Bottom,
            };
        case StagePanelLocation.Left:
            return {
                id: "inner",
                type: NZ_StagePanelType.Left,
            };
        case StagePanelLocation.Right:
            return {
                id: "inner",
                type: NZ_StagePanelType.Right,
            };
        case StagePanelLocation.Top:
            return {
                id: "inner",
                type: NZ_StagePanelType.Top,
            };
        case StagePanelLocation.TopMost:
            return {
                id: "outer",
                type: NZ_StagePanelType.Top,
            };
    }
};
//# sourceMappingURL=StagePanel.js.map