/*---------------------------------------------------------------------------------------------
 * 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
 */
import "./ModalSettingsStage.scss";
import * as React from "react";
import { ConditionalBooleanValue } from "@itwin/appui-abstract";
import settingsIconSvg from "@bentley/icons-generic/icons/settings.svg";
import { IModelApp, NotifyMessageDetails, OutputMessagePriority, OutputMessageType, } from "@itwin/core-frontend";
import { Logger } from "@itwin/core-bentley";
import { Centered, SettingsContainer } from "@itwin/core-react";
import { UiFramework } from "../UiFramework";
import { SyncUiEventId } from "../syncui/SyncUiEventDispatcher";
import { StageUsage } from "./StageUsage";
import { BackstageItemUtilities } from "../backstage/BackstageItemUtilities";
function ModalSettingsStage({ initialSettingsTabId, }) {
    var _a, _b, _c, _d;
    const id = (_b = (_a = UiFramework.frontstages.activeFrontstageDef) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : "none";
    const stageUsage = (_d = (_c = UiFramework.frontstages.activeFrontstageDef) === null || _c === void 0 ? void 0 : _c.usage) !== null && _d !== void 0 ? _d : StageUsage.General;
    const tabEntries = UiFramework.settingsManager.getSettingEntries(id, stageUsage);
    const noSettingsAvailableLabel = React.useRef(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.settingsManager.closeSettingsContainer(stageCloseFunc);
            }
        };
        return UiFramework.frontstages.onCloseModalFrontstageRequestedEvent.addListener(handleFrontstageCloseRequested);
    }, [tabEntries]);
    return (React.createElement("div", { className: "uifw-settings-container" }, tabEntries.length ? (React.createElement(SettingsContainer, { tabs: tabEntries, currentSettingsTab: currentSettingsTab(), settingsManager: UiFramework.settingsManager })) : (React.createElement(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.translate("settings.settingsStageLabel");
        this.notifyCloseRequest = true;
    }
    get content() {
        return (React.createElement(ModalSettingsStage, { initialSettingsTabId: this.initialSettingsTabId }));
    }
    static getBackstageActionItem(groupPriority, itemPriority) {
        return BackstageItemUtilities.createActionItem(SettingsModalFrontstage.id, groupPriority, itemPriority, () => UiFramework.frontstages.openModalFrontstage(new SettingsModalFrontstage()), UiFramework.translate("settings.settingsStageLabel"), undefined, settingsIconSvg, { isHidden: SettingsModalFrontstage.noSettingsAvailable() });
    }
    static showSettingsStage(initialSettingsTab) {
        if (UiFramework.settingsManager.providers.length) {
            // Check to see if it is already open
            // istanbul ignore else
            if (UiFramework.frontstages.activeModalFrontstage &&
                UiFramework.frontstages.activeModalFrontstage instanceof
                    SettingsModalFrontstage) {
                if (initialSettingsTab) {
                    UiFramework.settingsManager.activateSettingsTab(initialSettingsTab);
                    return;
                }
            }
            UiFramework.frontstages.openModalFrontstage(new SettingsModalFrontstage(initialSettingsTab));
        }
        else {
            const briefMessage = UiFramework.translate("settings.noSettingsAvailable");
            const detailedMessage = UiFramework.translate("settings.noSettingsProvidersRegistered");
            const info = new NotifyMessageDetails(OutputMessagePriority.Info, briefMessage, detailedMessage, OutputMessageType.Toast);
            IModelApp.notifications.outputMessage(info);
            Logger.logInfo(UiFramework.loggerCategory(this), detailedMessage);
        }
    }
}
SettingsModalFrontstage.id = "appui-react.modalSettingsStage";
SettingsModalFrontstage.noSettingsAvailable = () => new ConditionalBooleanValue(() => 0 === UiFramework.settingsManager.providers.length, [SyncUiEventId.SettingsProvidersChanged]);
export { SettingsModalFrontstage };
//# sourceMappingURL=ModalSettingsStage.js.map