package com.reactnativenavigation.react;

import android.app.Application;

import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.reactnativenavigation.NavigationApplication;

import java.util.ArrayList;
import java.util.List;

/**
 * Default implementation of {@link ReactNativeHost} that includes {@link NavigationPackage}
 * and user-defined additional packages.
 */
public class NavigationReactNativeHost extends ReactNativeHost {

	private final boolean isDebug;
	private final List<ReactPackage> additionalReactPackages;

    public NavigationReactNativeHost(NavigationApplication application) {
        this(application, application.isDebug(), application.createAdditionalReactPackages());
    }

	@SuppressWarnings("WeakerAccess")
    public NavigationReactNativeHost(Application application, boolean isDebug, final List<ReactPackage> additionalReactPackages) {
		super(application);
		this.isDebug = isDebug;
		this.additionalReactPackages = additionalReactPackages;
	}

	@Override
	public boolean getUseDeveloperSupport() {
		return isDebug;
	}

    @Override
	protected List<ReactPackage> getPackages() {
		List<ReactPackage> packages = new ArrayList<>();
		packages.add(new MainReactPackage());
		packages.add(new NavigationPackage(this));
		if (additionalReactPackages != null) {
			for (ReactPackage p : additionalReactPackages) {
				if (!(p instanceof MainReactPackage || p instanceof NavigationPackage)) {
					packages.add(p);
				}
			}
		}
		return packages;
	}
}

@@ -1251,33 +1251,27 @@
 %3C%3E();%0A%09%09
-packages.add(new 
+boolean has
 MainReac
@@ -1278,19 +1278,24 @@
 tPackage
-())
+ = false
 ;%0A%09%09pack
@@ -1453,98 +1453,228 @@
 eof 
-MainReactPackage %7C%7C p instanceof NavigationPackage)) %7B%0A%09%09%09%09%09packages.add(p);%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09
+NavigationPackage)) %7B%0A%09%09%09%09%09packages.add(p);%0A%09%09%09%09%7D%0A%09%09%09%09if (p instanceof MainReactPackage) hasMainReactPackage = true;%0A%09%09%09%7D%0A%09%09%7D%0A        if (!hasMainReactPackage) %7B%0A            packages.add(new MainReactPackage());%0A        
 %7D%0A%09%09

Add MainReactPackage only if wasn't added by the user