// Class: lime.system.System

var $global = typeof window != "undefined" ? window : typeof global != "undefined" ? global : typeof self != "undefined" ? self : this

$global.Object.defineProperty(exports, "__esModule", {value: true});

var __map_reserved = {};

// Imports

var $hxClasses = require("./../../hxClasses_stub").default;
var $import = require("./../../import_stub").default;
function haxe_ds_IntMap() {return require("./../../haxe/ds/IntMap");}
function js_Browser() {return require("./../../js/Browser");}
function Std() {return require("./../../Std");}
function StringTools() {return require("./../../StringTools");}
function Reflect() {return require("./../../Reflect");}
function lime_system_Display() {return require("./../../lime/system/Display");}
function lime_system_DisplayMode() {return require("./../../lime/system/DisplayMode");}
function lime_math_Rectangle() {return require("./../../lime/math/Rectangle");}
function lime_system_CFFI() {return require("./../../lime/system/CFFI");}
function haxe_ds_StringMap() {return require("./../../haxe/ds/StringMap");}
function lime_system_Endian() {return require("./../../lime/system/Endian");}

// Constructor

var System = function(){}

// Meta

System.__name__ = ["lime","system","System"];
System.prototype = {
	
};
System.prototype.__class__ = $hxClasses["lime.system.System"] = System;

// Init



// Statics

System.embed = function(projectName,element,width,height,windowConfig) {
	if(System.__applicationEntryPoint == null || System.__applicationConfig == null) {
		return;
	}
	if(System.__applicationEntryPoint.exists(projectName)) {
		var htmlElement = null;
		if(typeof(element) == "string") {
			htmlElement = (js_Browser().default).get_document().getElementById(element);
		} else if(element == null) {
			htmlElement = (js_Browser().default).get_document().createElement("div");
		} else {
			htmlElement = element;
		}
		if(htmlElement == null) {
			(js_Browser().default).get_window().console.log("[lime.embed] ERROR: Cannot find target element: " + (Std().default).string(element));
			return;
		}
		if(width == null) {
			width = 0;
		}
		if(height == null) {
			height = 0;
		}
		var defaultConfig = System.__applicationConfig.get(projectName);
		var config = { };
		System.__copyMissingFields(config,defaultConfig);
		if(windowConfig != null) {
			config.windows = [];
			if((windowConfig instanceof Array) && windowConfig.__enum__ == null) {
				config.windows = windowConfig;
			} else {
				config.windows[0] = windowConfig;
			}
			var _g1 = 0;
			var _g = config.windows.length;
			while(_g1 < _g) {
				var i = _g1++;
				if(i < defaultConfig.windows.length) {
					System.__copyMissingFields(config.windows[i],defaultConfig.windows[i]);
				}
				System.__copyMissingFields(config.windows[i].parameters,defaultConfig.windows[i].parameters);
				if(typeof(windowConfig.background) == "string") {
					var background = (StringTools().default).replace((Std().default).string(windowConfig.background),"#","");
					if(background.indexOf("0x") > -1) {
						windowConfig.background = (Std().default).parseInt(background);
					} else {
						windowConfig.background = (Std().default).parseInt("0x" + background);
					}
				}
			}
		}
		if((Reflect().default).field(config.windows[0],"rootPath")) {
			config.rootPath = (Reflect().default).field(config.windows[0],"rootPath");
			(Reflect().default).deleteField(config.windows[0],"rootPath");
		}
		config.windows[0].element = htmlElement;
		config.windows[0].width = width;
		config.windows[0].height = height;
		(System.__applicationEntryPoint.get(projectName))(config);
	}
}
System.exit = function(code) {
}
System.getDisplay = function(id) {
	if(id == 0) {
		var display = new (lime_system_Display().default)();
		display.id = 0;
		display.name = "Generic Display";
		display.dpi = 96 * (js_Browser().default).get_window().devicePixelRatio;
		display.currentMode = new (lime_system_DisplayMode().default)((js_Browser().default).get_window().screen.width,(js_Browser().default).get_window().screen.height,60,1);
		display.supportedModes = [display.currentMode];
		display.bounds = new (lime_math_Rectangle().default)(0,0,display.currentMode.width,display.currentMode.height);
		return display;
	}
	return null;
}
System.getTimer = function() {
	return (Std().default)["int"]((js_Browser().default).get_window().performance.now());
}
System.load = function(library,method,args,lazy) {
	if(lazy == null) {
		lazy = false;
	}
	if(args == null) {
		args = 0;
	}
	return (lime_system_CFFI().default).load(library,method,args,lazy);
}
System.openFile = function(path) {
	if(path != null) {
		(js_Browser().default).get_window().open(path,"_blank");
	}
}
System.openURL = function(url,target) {
	if(target == null) {
		target = "_blank";
	}
	if(url != null) {
		(js_Browser().default).get_window().open(url,target);
	}
}
System.__copyMissingFields = function(target,source) {
	if(source == null || target == null) {
		return;
	}
	var _g = 0;
	var _g1 = (Reflect().default).fields(source);
	while(_g < _g1.length) {
		var field = _g1[_g];
		++_g;
		if(!(Reflect().default).hasField(target,field)) {
			(Reflect().default).setField(target,field,(Reflect().default).field(source,field));
		}
	}
}
System.__getDirectory = function(type) {
	return null;
}
System.__registerEntryPoint = function(projectName,entryPoint,config) {
	if(System.__applicationConfig == null) {
		System.__applicationConfig = new (haxe_ds_StringMap().default)();
	}
	if(System.__applicationEntryPoint == null) {
		System.__applicationEntryPoint = new (haxe_ds_StringMap().default)();
	}
	System.__applicationEntryPoint.set(projectName,entryPoint);
	System.__applicationConfig.set(projectName,config);
}
System.__runProcess = function(command,args) {
	return null;
}
System.get_allowScreenTimeout = function() {
	return true;
}
System.set_allowScreenTimeout = function(value) {
	return true;
}
System.get_applicationDirectory = function() {
	if(System.__applicationDirectory == null) {
		System.__applicationDirectory = System.__getDirectory(0);
	}
	return System.__applicationDirectory;
}
System.get_applicationStorageDirectory = function() {
	if(System.__applicationStorageDirectory == null) {
		System.__applicationStorageDirectory = System.__getDirectory(1);
	}
	return System.__applicationStorageDirectory;
}
System.get_deviceModel = function() {
	var tmp = System.__deviceModel == null;
	return System.__deviceModel;
}
System.get_deviceVendor = function() {
	var tmp = System.__deviceVendor == null;
	return System.__deviceVendor;
}
System.get_desktopDirectory = function() {
	if(System.__desktopDirectory == null) {
		System.__desktopDirectory = System.__getDirectory(2);
	}
	return System.__desktopDirectory;
}
System.get_documentsDirectory = function() {
	if(System.__documentsDirectory == null) {
		System.__documentsDirectory = System.__getDirectory(3);
	}
	return System.__documentsDirectory;
}
System.get_endianness = function() {
	if(System.__endianness == null) {
		var arrayBuffer = new ArrayBuffer(2);
		var this1;
		if(arrayBuffer != null) {
			this1 = new Uint8Array(arrayBuffer,0);
		} else {
			this1 = null;
		}
		var uint8Array = this1;
		var this2;
		if(arrayBuffer != null) {
			this2 = new Uint16Array(arrayBuffer,0);
		} else {
			this2 = null;
		}
		var uint16array = this2;
		uint8Array[0] = 170;
		uint8Array[1] = 187;
		if(uint16array[0] == 43707) {
			System.__endianness = (lime_system_Endian().default).BIG_ENDIAN;
		} else {
			System.__endianness = (lime_system_Endian().default).LITTLE_ENDIAN;
		}
	}
	return System.__endianness;
}
System.get_fontsDirectory = function() {
	if(System.__fontsDirectory == null) {
		System.__fontsDirectory = System.__getDirectory(4);
	}
	return System.__fontsDirectory;
}
System.get_numDisplays = function() {
	return 1;
}
System.get_platformLabel = function() {
	if(System.__platformLabel == null) {
		var name = System.get_platformName();
		var version = System.get_platformVersion();
		if(name != null && version != null) {
			System.__platformLabel = name + " " + version;
		} else if(name != null) {
			System.__platformLabel = name;
		}
	}
	return System.__platformLabel;
}
System.get_platformName = function() {
	if(System.__platformName == null) {
		System.__platformName = "HTML5";
	}
	return System.__platformName;
}
System.get_platformVersion = function() {
	var tmp = System.__platformVersion == null;
	return System.__platformVersion;
}
System.get_userDirectory = function() {
	if(System.__userDirectory == null) {
		System.__userDirectory = System.__getDirectory(5);
	}
	return System.__userDirectory;
}
System.__directories = new (haxe_ds_IntMap().default)()

// Export

exports.default = System;