"use strict";
/*---------------------------------------------------------------------------------------------
* 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
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.ToolSettingsWidgetContent = exports.ToolSettingsContent = exports.useToolSettingsNode = exports.ToolSettingsGrid = exports.useHorizontalToolSettingNodes = exports.ToolSettingsDockedContent = exports.WidgetPanelsToolSettings = void 0;
require("./ToolSettings.scss");
const React = require("react");
const core_frontend_1 = require("@itwin/core-frontend");
const appui_layout_react_1 = require("@itwin/appui-layout-react");
const Frontstage_1 = require("../frontstage/Frontstage");
const UiFramework_1 = require("../UiFramework");
const InternalFrontstageManager_1 = require("../frontstage/InternalFrontstageManager");
function EmptyToolSettingsEntry() {
    const labelString = core_frontend_1.IModelApp.localization.getLocalizedString("UiFramework:tools.noToolSettings");
    const labelNode = React.createElement("div", { className: "uif-toolsetting-label-docked-horizontal-empty" }, labelString);
    const editorNode = React.createElement("div", null);
    return { labelNode, editorNode };
}
/** @internal */
// istanbul ignore next - need to work on overflow unit testing
function TsLabel({ children }) {
    return React.createElement("div", { className: "uif-toolsetting-label-docked-horizontal" }, children);
}
/** @internal */
function WidgetPanelsToolSettings() {
    const frontstageDef = (0, Frontstage_1.useActiveFrontstageDef)();
    const toolSettings = React.useContext(appui_layout_react_1.ToolSettingsStateContext);
    const topCenterZone = frontstageDef === null || frontstageDef === void 0 ? void 0 : frontstageDef.topCenter; // eslint-disable-line deprecation/deprecation
    if (!topCenterZone || !topCenterZone.isToolSettings || toolSettings.type === "widget")
        return null;
    return (React.createElement(ToolSettingsDockedContent, null));
}
exports.WidgetPanelsToolSettings = WidgetPanelsToolSettings;
/** @internal */
function ToolSettingsDockedContent() {
    var _a, _b;
    const settings = useHorizontalToolSettingNodes();
    // for the overflow to work properly each setting in the DockedToolSettings should be wrapped by a DockedToolSetting component
    return (React.createElement(appui_layout_react_1.DockedToolSettings, { itemId: (_b = (_a = InternalFrontstageManager_1.InternalFrontstageManager.activeToolSettingsProvider) === null || _a === void 0 ? void 0 : _a.uniqueId) !== null && _b !== void 0 ? _b : "none", key: Date.now() }, settings && settings.map((entry, index) => React.createElement(appui_layout_react_1.DockedToolSetting, { key: index },
        React.createElement(TsLabel, null, entry.labelNode),
        entry.editorNode))));
}
exports.ToolSettingsDockedContent = ToolSettingsDockedContent;
/** @internal */
function useHorizontalToolSettingNodes() {
    var _a;
    const [settings, setSettings] = React.useState((_a = InternalFrontstageManager_1.InternalFrontstageManager.activeToolSettingsProvider) === null || _a === void 0 ? void 0 : _a.horizontalToolSettingNodes);
    React.useEffect(() => {
        const handleToolActivatedEvent = () => {
            var _a;
            const nodes = (_a = InternalFrontstageManager_1.InternalFrontstageManager.activeToolSettingsProvider) === null || _a === void 0 ? void 0 : _a.horizontalToolSettingNodes;
            if (!nodes || nodes.length === 0)
                setSettings([EmptyToolSettingsEntry()]);
            else
                setSettings(nodes);
        };
        UiFramework_1.UiFramework.frontstages.onToolActivatedEvent.addListener(handleToolActivatedEvent);
        return () => {
            UiFramework_1.UiFramework.frontstages.onToolActivatedEvent.removeListener(handleToolActivatedEvent);
        };
    }, [setSettings]);
    React.useEffect(() => {
        const handleToolSettingsReloadEvent = () => {
            var _a;
            const nodes = (_a = InternalFrontstageManager_1.InternalFrontstageManager.activeToolSettingsProvider) === null || _a === void 0 ? void 0 : _a.horizontalToolSettingNodes;
            if (!nodes || nodes.length === 0)
                setSettings([EmptyToolSettingsEntry()]);
            else
                setSettings(nodes);
        };
        UiFramework_1.UiFramework.frontstages.onToolSettingsReloadEvent.addListener(handleToolSettingsReloadEvent);
        return () => {
            UiFramework_1.UiFramework.frontstages.onToolSettingsReloadEvent.removeListener(handleToolSettingsReloadEvent);
        };
    }, [setSettings]);
    return settings;
}
exports.useHorizontalToolSettingNodes = useHorizontalToolSettingNodes;
/** Component that arranges an array of ToolSettingsEntry items into a two column grid layout.
 * The left column is considered the label column, the right column is considered the property
 * editor column.
 * @internal
 */
function ToolSettingsGrid({ settings }) {
    return (React.createElement("div", { className: "uifw-standard-toolsettings-two-column-grid" }, settings && settings.map((setting, index) => {
        return (React.createElement(React.Fragment, { key: index },
            React.createElement("span", { className: "uifw-standard-toolsettings-label-entry" }, setting.labelNode),
            setting.editorNode));
    })));
}
exports.ToolSettingsGrid = ToolSettingsGrid;
/** @internal */
function useToolSettingsNode() {
    var _a;
    const [settings, setSettings] = React.useState((_a = InternalFrontstageManager_1.InternalFrontstageManager.activeToolSettingsProvider) === null || _a === void 0 ? void 0 : _a.toolSettingsNode);
    React.useEffect(() => {
        const handleToolActivatedEvent = () => {
            var _a;
            const nodes = (_a = InternalFrontstageManager_1.InternalFrontstageManager.activeToolSettingsProvider) === null || _a === void 0 ? void 0 : _a.toolSettingsNode;
            setSettings(nodes);
        };
        UiFramework_1.UiFramework.frontstages.onToolActivatedEvent.addListener(handleToolActivatedEvent);
        return () => {
            UiFramework_1.UiFramework.frontstages.onToolActivatedEvent.removeListener(handleToolActivatedEvent);
        };
    }, [setSettings]);
    React.useEffect(() => {
        const handleToolSettingsReloadEvent = () => {
            var _a;
            const nodes = (_a = InternalFrontstageManager_1.InternalFrontstageManager.activeToolSettingsProvider) === null || _a === void 0 ? void 0 : _a.toolSettingsNode;
            setSettings(nodes);
        };
        UiFramework_1.UiFramework.frontstages.onToolSettingsReloadEvent.addListener(handleToolSettingsReloadEvent);
        return () => {
            UiFramework_1.UiFramework.frontstages.onToolSettingsReloadEvent.removeListener(handleToolSettingsReloadEvent);
        };
    }, [setSettings]);
    return settings;
}
exports.useToolSettingsNode = useToolSettingsNode;
/** @internal */
function ToolSettingsContent() {
    const toolSettings = React.useContext(appui_layout_react_1.ToolSettingsStateContext);
    // This is needed to remount underlying components tree when going into widget state.
    if (toolSettings.type === "docked")
        return null;
    return React.createElement(ToolSettingsWidgetContent, null);
}
exports.ToolSettingsContent = ToolSettingsContent;
/** @internal */
function ToolSettingsWidgetContent() {
    var _a, _b;
    const floatingToolSettingsContainerRef = React.useRef(null);
    const node = useToolSettingsNode();
    // if no tool settings hide the floating widgets tab
    React.useEffect(() => {
        // istanbul ignore else
        if (floatingToolSettingsContainerRef.current) {
            const floatingWidgetTab = floatingToolSettingsContainerRef.current.closest(".nz-floating-toolsettings");
            // istanbul ignore else
            if (floatingWidgetTab) {
                floatingWidgetTab.style.visibility = !!node ? "visible" : /* istanbul ignore next */ "hidden";
            }
        }
    }, [node]);
    // istanbul ignore next
    const providerId = (_b = (_a = InternalFrontstageManager_1.InternalFrontstageManager.activeToolSettingsProvider) === null || _a === void 0 ? void 0 : _a.uniqueId) !== null && _b !== void 0 ? _b : "none";
    return (React.createElement("div", { "data-toolsettings-provider": providerId, className: "uifw-floating-toolsettings-container", ref: floatingToolSettingsContainerRef },
        React.createElement(appui_layout_react_1.ScrollableWidgetContent, null, node)));
}
exports.ToolSettingsWidgetContent = ToolSettingsWidgetContent;
//# sourceMappingURL=ToolSettings.js.map