/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module StatusBar
 */
import "./SectionsField.scss";
import classnames from "classnames";
import * as React from "react";
import { IModelApp, ViewClipClearTool, ViewClipDecoration, ViewClipDecorationProvider } from "@itwin/core-frontend";
import { FooterPopup } from "@itwin/appui-layout-react";
import { Button, ToggleSwitch } from "@itwin/itwinui-react";
import { useActiveViewport } from "../hooks/useActiveViewport";
import { UiFramework } from "../UiFramework";
import { Indicator } from "./Indicator";
import { StatusBarDialog } from "../statusbar/dialog/Dialog";
/** Status Field for showing section extra tools for clearing and showing manipulators
 * @beta
 */
export function SectionsStatusField(props) {
    var _a;
    const [toolTip] = React.useState(UiFramework.translate("tools.sectionTools"));
    const [clearLabel] = React.useState(UiFramework.translate("tools.sectionClear"));
    const [showHandlesLabel] = React.useState(UiFramework.translate("tools.sectionShowHandles"));
    const activeViewport = useActiveViewport();
    const [showIndicator, setShowIndicator] = React.useState(false);
    const [isPopupOpen, setPopupOpen] = React.useState(false);
    const targetDiv = React.useRef(null);
    const classes = (showIndicator) ? "uifw-indicator-fade-in" : "uifw-indicator-fade-out";
    const [hasManipulatorsShown, setHasManipulatorsShown] = React.useState(false);
    React.useEffect(() => {
        // istanbul ignore next
        const onClipChanged = (viewport, _eventType, _provider) => {
            if (viewport !== activeViewport)
                return;
            setHasManipulatorsShown(!!ViewClipDecoration.get(activeViewport));
            const isClipActive = !!activeViewport.view.getViewClip();
            setShowIndicator(isClipActive || !props.hideWhenUnused);
        };
        const clipActive = !!activeViewport && /* istanbul ignore next */ !!activeViewport.view.getViewClip();
        setShowIndicator(clipActive || !props.hideWhenUnused);
        setHasManipulatorsShown(clipActive && /* istanbul ignore next */ !!activeViewport && /* istanbul ignore next */ !!ViewClipDecoration.get(activeViewport));
        ViewClipDecorationProvider.create().onActiveClipChanged.addListener(onClipChanged);
        return () => {
            // Get or create static ViewClipDecorationProvider
            ViewClipDecorationProvider.create().onActiveClipChanged.removeListener(onClipChanged);
        };
    }, [activeViewport, props.hideWhenUnused, isPopupOpen]);
    // istanbul ignore next
    const toggleManipulators = async (e) => {
        if (activeViewport) {
            setHasManipulatorsShown(e.target.checked);
            await ViewClipDecorationProvider.create().toggleDecoration(activeViewport);
        }
    };
    // istanbul ignore next
    const handleClear = async () => {
        await IModelApp.tools.run(ViewClipClearTool.toolId, ViewClipDecorationProvider.create());
        setPopupOpen(false);
    };
    return (React.createElement("div", { className: "uifw-section-footer-popup-container" }, showIndicator &&
        React.createElement(React.Fragment, null,
            React.createElement("div", { ref: targetDiv, title: toolTip },
                React.createElement(Indicator, { className: classes, iconName: "icon-section-tool", onClick: () => setPopupOpen(!isPopupOpen), opened: isPopupOpen, 
                    // eslint-disable-next-line deprecation/deprecation
                    isInFooterMode: (_a = props.isInFooterMode) !== null && _a !== void 0 ? _a : true })),
            React.createElement(FooterPopup // eslint-disable-line deprecation/deprecation
            , { target: targetDiv.current, onClose: () => setPopupOpen(false), isOpen: isPopupOpen },
                React.createElement(StatusBarDialog, { titleBar: React.createElement(StatusBarDialog.TitleBar, { title: toolTip }) },
                    React.createElement("div", { className: "uifw-sections-footer-contents" },
                        React.createElement(Button, { onClick: handleClear }, clearLabel),
                        React.createElement("div", { className: "uifw-uifw-sections-toggle-container" },
                            React.createElement("div", { className: classnames("uifw-sections-label") }, showHandlesLabel),
                            React.createElement(ToggleSwitch, { className: "uifw-sections-toggle", onChange: toggleManipulators, checked: hasManipulatorsShown }))))))));
}
//# sourceMappingURL=SectionsField.js.map