/*---------------------------------------------------------------------------------------------
* 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 Zone
 */
import * as React from "react";
import { WidgetState } from "@itwin/appui-abstract";
import { ToolSettingsWidgetMode, } from "@itwin/appui-layout-react";
import { ConfigurableUiControlType } from "../configurableui/ConfigurableUiControl";
import { WidgetDef, WidgetType } from "../widgets/WidgetDef";
import { FrameworkZone } from "./FrameworkZone";
import { StatusBarZone } from "./StatusBarZone";
import { ToolSettingsZone } from "./toolsettings/ToolSettingsZone";
import { UiFramework } from "../UiFramework";
/** Enum for [[Zone]] Location.
 * @public @deprecated in 3.0. UI 1.0 will be removed in AppUi 4.0.
 */
export var ZoneLocation;
(function (ZoneLocation) {
    ZoneLocation[ZoneLocation["TopLeft"] = 1] = "TopLeft";
    ZoneLocation[ZoneLocation["TopCenter"] = 2] = "TopCenter";
    ZoneLocation[ZoneLocation["TopRight"] = 3] = "TopRight";
    ZoneLocation[ZoneLocation["CenterLeft"] = 4] = "CenterLeft";
    ZoneLocation[ZoneLocation["CenterRight"] = 6] = "CenterRight";
    ZoneLocation[ZoneLocation["BottomLeft"] = 7] = "BottomLeft";
    ZoneLocation[ZoneLocation["BottomCenter"] = 8] = "BottomCenter";
    ZoneLocation[ZoneLocation["BottomRight"] = 9] = "BottomRight";
})(ZoneLocation || (ZoneLocation = {}));
/** @internal */
export function getStableWidgetProps(widgetProps, stableId) {
    let props = widgetProps;
    if (props.id === undefined)
        props = {
            ...props,
            id: stableId,
        };
    return props;
}
/** Zone React component.
 * A Zone is a standard area on the screen for users to read and interact with data applicable to the current task. Each Zone has a defined purpose.
 * @deprecated in 3.6. Use [[WidgetConfig]] or [[StagePanelConfig]] instead.
 * @public
 */
export class Zone extends React.Component {
    constructor(props) {
        super(props);
        this._handleWidgetStateChangedEvent = (args) => {
            const runtimeProps = this.props.runtimeProps;
            if (!runtimeProps)
                return;
            const widgetDef = args.widgetDef;
            const id = this.getWidgetIdForDef(widgetDef);
            if (!id)
                return;
            const zoneDef = runtimeProps.zoneDefProvider.getZoneDef(id);
            // istanbul ignore if
            if (!zoneDef)
                return;
            const visibleWidgets = zoneDef.widgetDefs.filter((wd) => wd.isVisible || wd === widgetDef);
            for (let index = 0; index < visibleWidgets.length; index++) {
                const wDef = visibleWidgets[index];
                if (wDef !== widgetDef)
                    continue;
                if (widgetDef.state === WidgetState.Hidden && index < runtimeProps.activeTabIndex && id === runtimeProps.openWidgetId) {
                    // Need to decrease active tab index, since removed tab was rendered before active tab and we want to maintain active tab.
                    runtimeProps.widgetChangeHandler.handleTabClick(id, runtimeProps.activeTabIndex - 1);
                    break;
                }
                runtimeProps.widgetChangeHandler.handleWidgetStateChange(id, index, widgetDef.state === WidgetState.Open);
                break;
            }
        };
    }
    static initializeZoneDef(zoneDef, props) {
        zoneDef.initializeFromProps(props);
        // istanbul ignore else
        if (props.widgets) {
            props.widgets.forEach((widgetNode, index) => {
                // istanbul ignore if
                if (!React.isValidElement(widgetNode))
                    return;
                const stableId = `uifw-z-${ZoneLocation[zoneDef.zoneLocation]}-${index}`;
                const stableProps = getStableWidgetProps(widgetNode.props, stableId);
                const widgetDef = new WidgetDef(stableProps);
                zoneDef.addWidgetDef(widgetDef);
            });
        }
    }
    componentDidMount() {
        UiFramework.frontstages.onWidgetStateChangedEvent.addListener(this._handleWidgetStateChangedEvent);
    }
    componentWillUnmount() {
        UiFramework.frontstages.onWidgetStateChangedEvent.removeListener(this._handleWidgetStateChangedEvent);
    }
    render() {
        const { runtimeProps } = this.props;
        if (!runtimeProps)
            return null;
        const { zoneDef } = runtimeProps;
        let widgetElement;
        // istanbul ignore else
        if (runtimeProps.zone.widgets.length === 1) {
            if (zoneDef.isToolSettings && isToolSettingsWidgetManagerProps(runtimeProps.widget) && runtimeProps.widget.mode === ToolSettingsWidgetMode.TitleBar) {
                const widgetDef = zoneDef.getSingleWidgetDef();
                const isClosed = widgetDef ? (widgetDef.state === WidgetState.Closed || widgetDef.state === WidgetState.Hidden)
                    : /* istanbul ignore next */ false;
                return (React.createElement(ToolSettingsZone, { className: this.props.className, dropTarget: runtimeProps.dropTarget, getWidgetContentRef: runtimeProps.getWidgetContentRef, isClosed: isClosed, isHidden: runtimeProps.isHidden, lastPosition: runtimeProps.draggedWidget && /* istanbul ignore next */ runtimeProps.draggedWidget.lastPosition, style: this.props.style, targetChangeHandler: runtimeProps.targetChangeHandler, targetedBounds: runtimeProps.ghostOutline, widgetChangeHandler: runtimeProps.widgetChangeHandler, zone: runtimeProps.zone }));
            }
            else if (zoneDef.isStatusBar) {
                // istanbul ignore next
                if (runtimeProps.zone.id !== 8)
                    throw new TypeError();
                let widgetControl;
                const widgetDef = zoneDef.getSingleWidgetDef();
                // istanbul ignore else
                if (widgetDef)
                    widgetControl = widgetDef.getWidgetControl(ConfigurableUiControlType.StatusBarWidget);
                return (React.createElement(StatusBarZone, { className: this.props.className, dropTarget: runtimeProps.dropTarget, isHidden: runtimeProps.isHidden, isInFooterMode: runtimeProps.isInFooterMode, style: this.props.style, targetChangeHandler: runtimeProps.targetChangeHandler, targetedBounds: runtimeProps.ghostOutline, widgetChangeHandler: runtimeProps.widgetChangeHandler, widgetControl: widgetControl, zoneProps: runtimeProps.zone }));
            }
            const zDef = runtimeProps.zoneDefProvider.getZoneDef(runtimeProps.zone.widgets[0]);
            // istanbul ignore if
            if (!zDef) {
                widgetElement = null;
            }
            else if (zDef.widgetCount === 1 && zDef.widgetDefs[0].widgetType !== WidgetType.Rectangular) {
                /** Return free-form nzWidgetProps */
                const widgetDef = zDef.widgetDefs[0];
                widgetElement = (widgetDef.isVisible) ? widgetDef.reactNode : /* istanbul ignore next */ null;
            }
        }
        return (React.createElement(FrameworkZone, { activeTabIndex: runtimeProps.activeTabIndex, className: this.props.className, disabledResizeHandles: runtimeProps.disabledResizeHandles, draggedWidget: runtimeProps.draggedWidget, dropTarget: runtimeProps.dropTarget, fillZone: zoneDef.shouldFillZone, getWidgetContentRef: runtimeProps.getWidgetContentRef, isHidden: runtimeProps.isHidden, isInFooterMode: runtimeProps.isInFooterMode, openWidgetId: runtimeProps.openWidgetId, style: this.props.style, targetedBounds: runtimeProps.ghostOutline, targetChangeHandler: runtimeProps.targetChangeHandler, widget: runtimeProps.widget, widgetElement: widgetElement, widgetTabs: runtimeProps.widgetTabs, widgetChangeHandler: runtimeProps.widgetChangeHandler, zone: runtimeProps.zone }));
    }
    getWidgetIdForDef(widgetDef) {
        // istanbul ignore if
        if (!this.props.runtimeProps)
            return undefined;
        for (const wId of this.props.runtimeProps.zone.widgets) {
            const zoneDef = this.props.runtimeProps.zoneDefProvider.getZoneDef(wId);
            if (zoneDef && zoneDef.widgetDefs.some((wDef) => wDef === widgetDef))
                return wId;
        }
        return undefined;
    }
}
/** @internal */
export const isToolSettingsWidgetManagerProps = (props) => {
    return !!props && props.id === 2;
};
//# sourceMappingURL=Zone.js.map