# Copyright (c) 2023, NVIDIA CORPORATION.  All rights reserved.
#
# NVIDIA CORPORATION and its licensors retain all intellectual property
# and proprietary rights in and to this software, related documentation
# and any modifications thereto.  Any use, reproduction, disclosure or
# distribution of this software and related documentation without an express
# license agreement from NVIDIA CORPORATION is strictly prohibited.
#
__all__ = ["run_open_action"]

import asyncio

def run_open_action(extension_id: str, action_id: str, *args, **kwargs):
    import omni.kit.actions.core
    action_registry = omni.kit.actions.core.get_action_registry()
    # Setup the viewport (and UI first)
    action_registry.execute_action("omni.app.setup", "enable_viewport_rendering")

    # Finally hide the Welcome window
    from omni.kit.welcome.window import show_window as show_welcome_window
    show_welcome_window(visible=False, stage_opened=True)

    # Run the action which will open or create a Usd.Stage
    async def __execute_action():
        import omni.kit.app
        # OM-90720: sometimes open stage will popup a prompt to ask if save current stage
        # But the prompt is a popup window which will hide immediately if the welcome window (modal) still there
        # As a result, the required stage will not be opened
        # So here wait a few frames to make sure welcome window is really closed
        for _ in range(4):
            await omni.kit.app.get_app().next_update_async()
        action_registry.execute_action(extension_id, action_id, *args, **kwargs)

    asyncio.ensure_future(__execute_action())
