package com.tns;

import java.io.File;
import org.json.JSONObject;

class V8Config
{
	private static final String AndroidKey = "android";
	private static final String V8FlagsKey = "v8Flags";
	private static final String CodeCacheKey = "codeCache";
	private static final String HeapSnapshotKey = "heapSnapshot";
	
	public static Object[] fromPackageJSON(File appDir)
	{
		Object[] result = makeDefaultOptions();
		File packageInfo = new File (appDir, "/app/package.json");
		if(!packageInfo.exists())
		{
			return result;
		}
		
		JSONObject rootObject;
		try
		{
			rootObject = FileSystem.readJSONFile(packageInfo);
			if (rootObject != null && rootObject.has(AndroidKey))
			{
				JSONObject androidObject = rootObject.getJSONObject(AndroidKey);
				if(androidObject.has(V8FlagsKey))
				{
					result[0] = androidObject.getString(V8FlagsKey); 
				}
				if(androidObject.has(CodeCacheKey))
				{
					result[1] = androidObject.getBoolean(CodeCacheKey);
				}
				if(androidObject.has(HeapSnapshotKey))
				{
					result[2] = androidObject.getBoolean(HeapSnapshotKey);
				}
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		return result;
	}
	
	private static Object[] makeDefaultOptions()
	{
		Object[] result = new Object[2];
		// v8 startup flags, defaults to --expose_gc due to tns_modules requirement 
		result[0] = "--expose_gc";
		// enable v8 code caching, false by default
		result[1] = false;
		// enable v8 heap snapshot, false by default
		result[2] = false;
		
		return result;
	}
}

@@ -1243,12 +1243,13 @@
 ect%5B
-2%5D;%0A
+%5D %7B%0A%09
 %09%09//
@@ -1325,28 +1325,17 @@
 ment %0A%09%09
-result%5B0%5D = 
+%09
 %22--expos
@@ -1339,18 +1339,19 @@
 pose_gc%22
-;%0A
+,%0A%09
 %09%09// ena
@@ -1394,27 +1394,17 @@
 t%0A%09%09
-result%5B1%5D = 
+%09
 false
-;%0A
+,%0A%09
 %09%09//
@@ -1452,25 +1452,18 @@
 t%0A%09%09
-result%5B2%5D = 
+%09
 false
+%0A%09%09%7D
 ;%0A%09%09

use array initialization list