/*---------------------------------------------------------------------------------------------
 * 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 { IModelApp, NotifyMessageDetails, OutputMessagePriority, OutputMessageType, } from "@itwin/core-frontend";
import { Logger } from "@itwin/core-bentley";
import { Centered, SettingsContainer } from "@itwin/core-react";
import { SvgSettings } from "@itwin/itwinui-icons-react";
import { UiFramework } from "../UiFramework";
import { SyncUiEventId } from "../syncui/SyncUiEventDispatcher";
import { StageUsage } from "./StageUsage";
import { BackstageItemUtilities } from "../backstage/BackstageItemUtilities";
import { useTranslation } from "../hooks/useTranslation";
function ModalSettingsStage({ initialSettingsTabId, }) {
    const { translate } = useTranslation();
    const id = UiFramework.frontstages.activeFrontstageDef?.id ?? "none";
    const stageUsage = UiFramework.frontstages.activeFrontstageDef?.usage ?? StageUsage.General;
    const tabEntries = 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(() => {
        return UiFramework.frontstages.onCloseModalFrontstageRequestedEvent.addListener(({ modalFrontstage, stageCloseFunc }) => {
            if (modalFrontstage instanceof SettingsModalFrontstage &&
                stageCloseFunc) {
                UiFramework.settingsManager.closeSettingsContainer(stageCloseFunc);
            }
        });
    }, [tabEntries]);
    return (React.createElement("div", { className: "uifw-settings-container" }, tabEntries.length ? (
    // eslint-disable-next-line deprecation/deprecation
    React.createElement(SettingsContainer, { tabs: tabEntries, currentSettingsTab: currentSettingsTab(), settingsManager: UiFramework.settingsManager })) : (
    // eslint-disable-next-line deprecation/deprecation
    React.createElement(Centered, null, translate("settings.noSettingsAvailable")))));
}
/** Modal frontstage displaying and editing settings from registered settings providers. See [SettingsManager]($core-react)
 * and [SettingsContainer]($core-react).
 * @beta
 */
export 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({
            id: SettingsModalFrontstage.id,
            groupPriority,
            itemPriority,
            execute: () => UiFramework.frontstages.openModalFrontstage(new SettingsModalFrontstage()),
            label: UiFramework.translate("settings.settingsStageLabel"),
            icon: React.createElement(SvgSettings, null),
            isHidden: SettingsModalFrontstage.noSettingsAvailable(),
        });
    }
    static showSettingsStage(initialSettingsTab) {
        if (UiFramework.settingsManager.providers.length) {
            // Check to see if it is already open
            if (UiFramework.frontstages.activeModalFrontstage &&
                UiFramework.frontstages.activeModalFrontstage instanceof
                    SettingsModalFrontstage) {
                if (initialSettingsTab) {
                    UiFramework.settingsManager.activateSettingsTab(initialSettingsTab);
                    return;
                }
            }
            UiFramework.frontstages.openModalFrontstage(new SettingsModalFrontstage(initialSettingsTab));
        }
        else {
            const detailedMessage = UiFramework.translate("settings.noSettingsProvidersRegistered");
            const info = new NotifyMessageDetails(OutputMessagePriority.Info, UiFramework.translate("settings.noSettingsAvailable"), 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]);
//# sourceMappingURL=ModalSettingsStage.js.map