/*---------------------------------------------------------------------------------------------
 * 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 svgSectionTool from "@bentley/icons-generic/icons/section-tool.svg";
import { IModelApp, ViewClipClearTool, ViewClipDecoration, ViewClipDecorationProvider, } from "@itwin/core-frontend";
import { Icon } from "@itwin/core-react";
import { Button, IconButton, ToggleSwitch } from "@itwin/itwinui-react";
import classnames from "classnames";
import * as React from "react";
import { useActiveViewport } from "../hooks/useActiveViewport";
import { StatusBarDialog } from "../statusbar/dialog/Dialog";
import "./SectionsField.scss";
import { useTranslation } from "../hooks/useTranslation";
import { StatusBarPopover } from "../statusbar/popup/StatusBarPopover";
/** Status Field for showing section extra tools for clearing and showing manipulators
 * @beta
 */
export function SectionsStatusField(props) {
    const { translate } = useTranslation();
    const tooltip = translate("tools.sectionTools");
    const activeViewport = useActiveViewport();
    const [showIndicator, setShowIndicator] = React.useState(false);
    const [isPopupOpen, setPopupOpen] = React.useState(false);
    const [hasManipulatorsShown, setHasManipulatorsShown] = React.useState(false);
    React.useEffect(() => {
        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 && !!activeViewport.view.getViewClip();
        setShowIndicator(clipActive || !props.hideWhenUnused);
        setHasManipulatorsShown(clipActive && !!activeViewport && !!ViewClipDecoration.get(activeViewport));
        ViewClipDecorationProvider.create().onActiveClipChanged.addListener(onClipChanged);
        return () => {
            // Get or create static ViewClipDecorationProvider
            ViewClipDecorationProvider.create().onActiveClipChanged.removeListener(onClipChanged);
        };
    }, [activeViewport, props.hideWhenUnused, isPopupOpen]);
    const toggleManipulators = async (e) => {
        if (activeViewport) {
            setHasManipulatorsShown(e.target.checked);
            await ViewClipDecorationProvider.create().toggleDecoration(activeViewport);
        }
    };
    const handleClear = async () => {
        await IModelApp.tools.run(ViewClipClearTool.toolId, ViewClipDecorationProvider.create());
        setPopupOpen(false);
    };
    return (React.createElement(React.Fragment, null, showIndicator && (React.createElement(StatusBarPopover, { visible: isPopupOpen, onVisibleChange: setPopupOpen, content: React.createElement(StatusBarDialog, { titleBar: React.createElement(StatusBarDialog.TitleBar, { title: tooltip }) },
            React.createElement("div", { className: "uifw-sections-footer-contents" },
                React.createElement(Button, { onClick: handleClear }, translate("tools.sectionClear")),
                React.createElement("div", { className: "uifw-uifw-sections-toggle-container" },
                    React.createElement("div", { className: classnames("uifw-sections-label") }, translate("tools.sectionShowHandles")),
                    React.createElement(ToggleSwitch, { className: "uifw-sections-toggle", onChange: toggleManipulators, checked: hasManipulatorsShown })))) },
        React.createElement(IconButton, { title: tooltip, styleType: "borderless" },
            React.createElement(Icon, { iconSpec: svgSectionTool }),
            React.createElement(StatusBarPopover.ExpandIndicator, null))))));
}
//# sourceMappingURL=SectionsField.js.map