/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module ToolSettings
 */
import * as React from "react";
import { TitleBarButton, ToolSettings, ToolSettingsTab, Zone, } from "@itwin/appui-layout-react";
import { SafeAreaContext } from "../../safearea/SafeAreaContext";
import { UiFramework } from "../../UiFramework";
import { getFloatingZoneBounds, getFloatingZoneStyle } from "../FrameworkZone";
import { Outline } from "../Outline";
import { onEscapeSetFocusToHome } from "../../hooks/useEscapeSetFocusToHome";
import { ZoneTargets } from "../../dragdrop/ZoneTargets";
// cSpell:ignore safearea
/** State for the ToolSettingsZone content.
 */
var ToolSettingsZoneContent;
(function (ToolSettingsZoneContent) {
    ToolSettingsZoneContent[ToolSettingsZoneContent["Closed"] = 0] = "Closed";
    ToolSettingsZoneContent[ToolSettingsZoneContent["ToolSettings"] = 1] = "ToolSettings";
})(ToolSettingsZoneContent || (ToolSettingsZoneContent = {}));
/** Tool Settings Zone React component.
 * @internal
 */
export class ToolSettingsZone extends React.PureComponent {
    constructor(props) {
        super(props);
        this._hiddenVisibility = {
            visibility: "hidden",
        };
        this._widget = React.createRef();
        this._handleToolActivatedEvent = () => {
            // Update tool settings title when active tool changes.
            const title = `${UiFramework.toolSettings.activeToolLabel}`;
            this.setState({ title });
        };
        this._processClick = () => {
            this.setState((prevState) => {
                let toolSettingsZoneContent = ToolSettingsZoneContent.Closed;
                if (prevState.toolSettingsZoneContent === ToolSettingsZoneContent.Closed)
                    toolSettingsZoneContent = ToolSettingsZoneContent.ToolSettings;
                return {
                    toolSettingsZoneContent,
                };
            });
        };
        this._handleDragStart = (initialPosition) => {
            if (!this._widget.current)
                return;
            const bounds = this._widget.current.getBounds();
            this.props.widgetChangeHandler.handleTabDragStart(this.props.zone.id, 0, initialPosition, bounds);
        };
        this._handleResize = (resizeBy, handle) => {
            this.props.widgetChangeHandler.handleResize(this.props.zone.id, resizeBy, handle, 0);
        };
        const title = `${UiFramework.toolSettings.activeToolLabel}`;
        this.state = {
            toolSettingsZoneContent: this.props.isClosed ? ToolSettingsZoneContent.Closed : ToolSettingsZoneContent.ToolSettings,
            title,
        };
    }
    componentDidMount() {
        UiFramework.frontstages.onToolActivatedEvent.addListener(this._handleToolActivatedEvent);
    }
    componentWillUnmount() {
        UiFramework.frontstages.onToolActivatedEvent.removeListener(this._handleToolActivatedEvent);
    }
    render() {
        const bounds = getFloatingZoneBounds(this.props.zone);
        const zIndexStyle = getFloatingZoneStyle(this.props.zone);
        return (React.createElement(SafeAreaContext.Consumer, null, (safeAreaInsets) => (React.createElement("span", { style: zIndexStyle },
            React.createElement(Zone, { bounds: bounds, className: this.props.className, id: this.props.zone.id, isFloating: !!this.props.zone.floating, isHidden: this.props.isHidden, safeAreaInsets: safeAreaInsets, style: this.props.style }, this.getToolSettingsWidget()),
            React.createElement(Zone, { bounds: this.props.zone.bounds, id: this.props.zone.id, safeAreaInsets: safeAreaInsets },
                React.createElement(ZoneTargets, { zoneId: this.props.zone.id, dropTarget: this.props.dropTarget, targetChangeHandler: this.props.targetChangeHandler })),
            React.createElement(Outline, { bounds: this.props.targetedBounds })))));
    }
    getToolSettingsWidget() {
        if (this.state.toolSettingsZoneContent === ToolSettingsZoneContent.Closed) {
            return (React.createElement(ToolSettingsTab, { onClick: this._processClick, onKeyDown: onEscapeSetFocusToHome, title: this.state.title, onMouseEnter: UiFramework.visibility.handleWidgetMouseEnter },
                React.createElement("i", { className: "icon icon-settings" })));
        }
        return (React.createElement(ToolSettings, { buttons: React.createElement("div", { style: this.props.zone.floating && this._hiddenVisibility },
                React.createElement(TitleBarButton, { onClick: this._processClick, title: UiFramework.translate("general.minimize") },
                    React.createElement("i", { className: "icon icon-chevron-up" }))), contentRef: this.props.getWidgetContentRef(this.props.zone.id), fillZone: this.props.zone.isLayoutChanged && !!this.props.zone.floating, lastPosition: this.props.lastPosition, onDrag: this.props.zone.allowsMerging ? this.props.widgetChangeHandler.handleTabDrag : undefined, onDragEnd: this.props.widgetChangeHandler.handleTabDragEnd, onDragStart: this._handleDragStart, onResize: this.props.zone.floating && this._handleResize, onMouseEnter: UiFramework.visibility.handleWidgetMouseEnter, ref: this._widget, title: this.state.title }));
    }
}
//# sourceMappingURL=ToolSettingsZone.js.map