/*
 * main.m — entry point for native-hello-ios.
 *
 * iOS UIKit lifecycle:
 *   UIApplicationMain never returns. The compiled el program's boot body
 *   (native_init → window_from_manifest → app_build → window_show) must
 *   run inside applicationDidFinishLaunchingWithOptions on the main thread.
 *
 * How it works:
 *   1. el_uikit_set_args() forwards argc/argv to UIApplicationMain.
 *   2. el_main_entry_fn is set to el_app_boot (defined below) before
 *      UIApplicationMain is entered.
 *   3. ElAppDelegate.didFinishLaunchingWithOptions calls el_main_entry_fn(),
 *      which runs the el program's widget setup at the correct lifecycle point.
 *   4. __native_run_loop() inside the compiled el main is a no-op on iOS —
 *      UIApplicationMain already owns the run loop.
 *
 * The generated native_hello.c exports el_app_main() (renamed from main by
 * the gen step in build.sh). We call it from el_app_boot so it runs after
 * UIApplicationMain sets up the application object.
 *
 * Compile with -fno-objc-arc (required by el_uikit.m).
 */

#import <UIKit/UIKit.h>

/* From el_uikit.m */
void el_uikit_set_args(int argc, char **argv);
extern void (*el_main_entry_fn)(void);

/* Forward declaration of the renamed compiled el main. */
int el_app_main(int argc, char **argv);

/* Boot function registered as el_main_entry_fn. Called by ElAppDelegate
 * inside didFinishLaunchingWithOptions on the main thread. */
static void el_app_boot(void) {
    char *argv[] = {"el-app", NULL};
    el_app_main(1, argv);
}

int main(int argc, char *argv[]) {
    el_uikit_set_args(argc, argv);
    /* Register the boot function so ElAppDelegate can call it. */
    el_main_entry_fn = el_app_boot;
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, @"ElAppDelegate");
    }
}
