
module.exports = new Class({
	basePath: null,
	path: null,
	moduleName: null,
	resolvers: null,
	status: null,

	initialize(basePath, path) {
		const aux = path.split('/');
		const moduleName = aux[aux.length - 1];

		this.basePath = basePath;
		this.path = path;
		this.moduleName = moduleName;
	},

	async load() {
		switch(this.status) {
			case 'ready':
				return;
			case 'loading':
				return new Promise((resolve, reject) => {
					this.resolvers.push(status => {
						if (status == 'error')
							return reject(new Error(`Module could not be loaded`));
						resolve();
					});
				});
		}

		this.status = 'loading';
		this.resolvers = [];
		const absPath = `${this.basePath}/${this.path}`;
		const requests = [
			Vn.includeJs(`${absPath}/${this.moduleName}.js`),
			Vn.loadXml(`${absPath}/ui.xml`)
		];

		try {
			await Vn.Locale.load(absPath);
		} catch(err) {
			console.err(err);
		}

		try {
			await Promise.all(requests);
			const klassName = this.toCamelCase(this.moduleName);
			this.klass = Hedera[klassName];
			this.status = 'ready';
		} catch (err) {
			this.status = 'error';
			console.error(err);
		}

		for (const resolver of this.resolvers)
			resolver(this.status);

		this.resolvers = null;
	}
	
	,toCamelCase(dashedName) {
		var camelCase = dashedName.charAt(0).toUpperCase();
		camelCase += dashedName.substr(1).replace(/\w-\w/g, function(token) {
			return token.charAt(0) + token.charAt(2).toUpperCase();
		});
		return camelCase;
	}
});

