"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");
function ModalSettingsStage({ initialSettingsTabId }) {
    var _a, _b, _c, _d;
    const id = (_b = (_a = UiFramework_1.UiFramework.frontstages.activeFrontstageDef) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : "none";
    const stageUsage = (_d = (_c = UiFramework_1.UiFramework.frontstages.activeFrontstageDef) === null || _c === void 0 ? void 0 : _c.usage) !== null && _d !== void 0 ? _d : StageUsage_1.StageUsage.General;
    const tabEntries = UiFramework_1.UiFramework.settingsManager.getSettingEntries(id, stageUsage);
    const noSettingsAvailableLabel = React.useRef(UiFramework_1.UiFramework.translate("settings.noSettingsAvailable"));
    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 }) => {
            // istanbul ignore else
            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 }) :
        React.createElement(core_react_1.Centered, null, noSettingsAvailableLabel.current)));
}
/** 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 appui_abstract_1.BackstageItemUtilities.createActionItem(SettingsModalFrontstage.id, groupPriority, itemPriority, () => UiFramework_1.UiFramework.frontstages.openModalFrontstage(new SettingsModalFrontstage()), // eslint-disable-line deprecation/deprecation
        UiFramework_1.UiFramework.translate("settings.settingsStageLabel"), undefined, appui_abstract_1.IconSpecUtilities.createWebComponentIconSpec(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
            // istanbul ignore else
            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 briefMessage = UiFramework_1.UiFramework.translate("settings.noSettingsAvailable");
            const detailedMessage = UiFramework_1.UiFramework.translate("settings.noSettingsProvidersRegistered");
            const info = new core_frontend_1.NotifyMessageDetails(core_frontend_1.OutputMessagePriority.Info, briefMessage, 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