import bpy
import os

from .utils_web_project import startLocalServer

from .utils_debug import debug_log

from .settings_scene import tryGetMainSceneSettings
from .utils_cli import cli_project_path

def __applyCliArgs():
    path = cli_project_path()
    settings = tryGetMainSceneSettings()
    if settings and path:
        if not os.path.exists(path):
            debug_log("Project path does not exist at \"" + path + "\"")
        else:
            debug_log("Applying project path \"" + path + "\"")
            # this assumes that we have a package.json
            if os.path.exists(path + "/package.json"):
                settings.projectPath = path
                startLocalServer(path)
            else:
                debug_log("No package.json found at \"" + path + "\"")
            

def register():
    bpy.app.timers.register(__applyCliArgs, first_interval=1)
    pass

def unregister():
    pass
