"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 Frontstage
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.SettingsModalFrontstage = void 0;
require("./ModalSettingsStage.scss");
const React = require("react");
const appui_abstract_1 = require("@itwin/appui-abstract");
const settings_svg_1 = require("@bentley/icons-generic/icons/settings.svg");
const core_frontend_1 = require("@itwin/core-frontend");
const core_bentley_1 = require("@itwin/core-bentley");
const core_react_1 = require("@itwin/core-react");
const UiFramework_1 = require("../UiFramework");
const SyncUiEventDispatcher_1 = require("../syncui/SyncUiEventDispatcher");
const StageUsage_1 = require("./StageUsage");
const BackstageItemUtilities_1 = require("../backstage/BackstageItemUtilities");
const useTranslation_1 = require("../hooks/useTranslation");
function ModalSettingsStage({ initialSettingsTabId, }) {
    const { translate } = (0, useTranslation_1.useTranslation)();
    const id = UiFramework_1.UiFramework.frontstages.activeFrontstageDef?.id ?? "none";
    const stageUsage = UiFramework_1.UiFramework.frontstages.activeFrontstageDef?.usage ?? StageUsage_1.StageUsage.General;
    const tabEntries = UiFramework_1.UiFramework.settingsManager.getSettingEntries(id, stageUsage);
    const currentSettingsTab = React.useCallback(() => {
        const categoryToFind = initialSettingsTabId
            ? initialSettingsTabId.toLowerCase()
            : tabEntries[0].tabId.toLowerCase();
        let foundTab = tabEntries.find((entry) => entry.tabId.toLowerCase() === categoryToFind);
        if (!foundTab)
            foundTab = tabEntries.find((entry) => entry.label.toLowerCase() === categoryToFind);
        return foundTab;
    }, [initialSettingsTabId, tabEntries]);
    React.useEffect(() => {
        const handleFrontstageCloseRequested = ({ modalFrontstage, stageCloseFunc, } // eslint-disable-line deprecation/deprecation
        ) => {
            if (modalFrontstage instanceof SettingsModalFrontstage &&
                stageCloseFunc) {
                UiFramework_1.UiFramework.settingsManager.closeSettingsContainer(stageCloseFunc);
            }
        };
        return UiFramework_1.UiFramework.frontstages.onCloseModalFrontstageRequestedEvent.addListener(handleFrontstageCloseRequested);
    }, [tabEntries]);
    return (React.createElement("div", { className: "uifw-settings-container" }, tabEntries.length ? (React.createElement(core_react_1.SettingsContainer, { tabs: tabEntries, currentSettingsTab: currentSettingsTab(), settingsManager: UiFramework_1.UiFramework.settingsManager })) : (
    // eslint-disable-next-line deprecation/deprecation
    React.createElement(core_react_1.Centered, null, translate("settings.noSettingsAvailable")))));
}
/** Modal frontstage displaying and editing settings from registered settings providers. See [SettingsManager]($core-react)
 * and [SettingsContainer]($core-react).
 * @beta
 */
class SettingsModalFrontstage {
    constructor(initialSettingsTabId) {
        this.initialSettingsTabId = initialSettingsTabId;
        this.title = UiFramework_1.UiFramework.translate("settings.settingsStageLabel");
        this.notifyCloseRequest = true;
    }
    get content() {
        return (React.createElement(ModalSettingsStage, { initialSettingsTabId: this.initialSettingsTabId }));
    }
    static getBackstageActionItem(groupPriority, itemPriority) {
        return BackstageItemUtilities_1.BackstageItemUtilities.createActionItem(SettingsModalFrontstage.id, groupPriority, itemPriority, () => UiFramework_1.UiFramework.frontstages.openModalFrontstage(new SettingsModalFrontstage()), UiFramework_1.UiFramework.translate("settings.settingsStageLabel"), undefined, settings_svg_1.default, { isHidden: SettingsModalFrontstage.noSettingsAvailable() });
    }
    static showSettingsStage(initialSettingsTab) {
        if (UiFramework_1.UiFramework.settingsManager.providers.length) {
            // Check to see if it is already open
            if (UiFramework_1.UiFramework.frontstages.activeModalFrontstage &&
                UiFramework_1.UiFramework.frontstages.activeModalFrontstage instanceof
                    SettingsModalFrontstage) {
                if (initialSettingsTab) {
                    UiFramework_1.UiFramework.settingsManager.activateSettingsTab(initialSettingsTab);
                    return;
                }
            }
            UiFramework_1.UiFramework.frontstages.openModalFrontstage(new SettingsModalFrontstage(initialSettingsTab));
        }
        else {
            const detailedMessage = UiFramework_1.UiFramework.translate("settings.noSettingsProvidersRegistered");
            const info = new core_frontend_1.NotifyMessageDetails(core_frontend_1.OutputMessagePriority.Info, UiFramework_1.UiFramework.translate("settings.noSettingsAvailable"), detailedMessage, core_frontend_1.OutputMessageType.Toast);
            core_frontend_1.IModelApp.notifications.outputMessage(info);
            core_bentley_1.Logger.logInfo(UiFramework_1.UiFramework.loggerCategory(this), detailedMessage);
        }
    }
}
exports.SettingsModalFrontstage = SettingsModalFrontstage;
SettingsModalFrontstage.id = "appui-react.modalSettingsStage";
SettingsModalFrontstage.noSettingsAvailable = () => new appui_abstract_1.ConditionalBooleanValue(() => 0 === UiFramework_1.UiFramework.settingsManager.providers.length, [SyncUiEventDispatcher_1.SyncUiEventId.SettingsProvidersChanged]);
//# sourceMappingURL=ModalSettingsStage.js.map