// Path: brjs-core/src/main/java/org/bladerunnerjs/api/memoization/Getter.java
// public interface Getter<E extends Exception> {
// 	Object get() throws E;
// }


import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;
import org.bladerunnerjs.api.memoization.Getter;
import org.bladerunnerjs.api.memoization.MemoizedFile;
import org.bladerunnerjs.api.memoization.MemoizedValue;
import org.bladerunnerjs.api.model.exception.request.ContentFileProcessingException;
import org.bladerunnerjs.model.AssetContainer;
import org.bladerunnerjs.model.engine.Node;
import org.bladerunnerjs.model.engine.NodeProperties;
import org.bladerunnerjs.utility.UnicodeReader;
import org.bladerunnerjs.api.App;
import org.bladerunnerjs.api.BundlableNode;
package org.bladerunnerjs.plugin.bundlers.aliasing;



public class AliasingUtility
{
	
	public static final String BR_UNKNOWN_CLASS_NAME = "br.UnknownClass";
	
	public static boolean usesLegacySchema(MemoizedFile aliaseFile, String defaultCharEncoding) throws IOException {
		LineIterator it = IOUtils.lineIterator( new UnicodeReader(aliaseFile, defaultCharEncoding) );
		for (int lineNumber = 0; it.hasNext() && lineNumber < 3; lineNumber++) {
			if (it.nextLine().contains("schema.caplin.com")) {
				return true;
			}
		}
		return false;
	}
	
	/* Memoization Utilities */
	
	public static AliasesFile aliasesFile(AssetContainer assetContainer) {
		return getNodeProperty(assetContainer, AliasesFile.class.getSimpleName(), AliasesFile.class, 
				() -> { return new AliasesFile(assetContainer); });
	}
	
	public static AliasesFile aliasesFile(App app) {
		return getNodeProperty(app, AliasesFile.class.getSimpleName(), AliasesFile.class, 
				() -> { return new AliasesFile(app); });
	}
	
	public static AliasDefinitionsFile aliasDefinitionsFile(AssetContainer assetContainer, String path) {
		return getNodeProperty(assetContainer, AliasDefinitionsFile.class.getSimpleName()+"_"+path, AliasDefinitionsFile.class, 
				() -> { return new AliasDefinitionsFile(assetContainer, assetContainer.file(path)); });
	}
	
	@SuppressWarnings("unchecked")
// Path: brjs-core/src/main/java/org/bladerunnerjs/api/memoization/Getter.java
// public interface Getter<E extends Exception> {
// 	Object get() throws E;
// }


// Path: plugins/brjs-plugins/src/main/java/org/bladerunnerjs/plugin/bundlers/aliasing/AliasingUtility.java
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;
import org.bladerunnerjs.api.memoization.Getter;
import org.bladerunnerjs.api.memoization.MemoizedFile;
import org.bladerunnerjs.api.memoization.MemoizedValue;
import org.bladerunnerjs.api.model.exception.request.ContentFileProcessingException;
import org.bladerunnerjs.model.AssetContainer;
import org.bladerunnerjs.model.engine.Node;
import org.bladerunnerjs.model.engine.NodeProperties;
import org.bladerunnerjs.utility.UnicodeReader;
import org.bladerunnerjs.api.App;
import org.bladerunnerjs.api.BundlableNode;

package org.bladerunnerjs.plugin.bundlers.aliasing;



public class AliasingUtility
{
	
	public static final String BR_UNKNOWN_CLASS_NAME = "br.UnknownClass";
	
	public static boolean usesLegacySchema(MemoizedFile aliaseFile, String defaultCharEncoding) throws IOException {
		LineIterator it = IOUtils.lineIterator( new UnicodeReader(aliaseFile, defaultCharEncoding) );
		for (int lineNumber = 0; it.hasNext() && lineNumber < 3; lineNumber++) {
			if (it.nextLine().contains("schema.caplin.com")) {
				return true;
			}
		}
		return false;
	}
	
	/* Memoization Utilities */
	
	public static AliasesFile aliasesFile(AssetContainer assetContainer) {
		return getNodeProperty(assetContainer, AliasesFile.class.getSimpleName(), AliasesFile.class, 
				() -> { return new AliasesFile(assetContainer); });
	}
	
	public static AliasesFile aliasesFile(App app) {
		return getNodeProperty(app, AliasesFile.class.getSimpleName(), AliasesFile.class, 
				() -> { return new AliasesFile(app); });
	}
	
	public static AliasDefinitionsFile aliasDefinitionsFile(AssetContainer assetContainer, String path) {
		return getNodeProperty(assetContainer, AliasDefinitionsFile.class.getSimpleName()+"_"+path, AliasDefinitionsFile.class, 
				() -> { return new AliasDefinitionsFile(assetContainer, assetContainer.file(path)); });
	}
	
	@SuppressWarnings("unchecked")
	static <OT extends Object> OT getNodeProperty(Node node, String propertyKey, Class<? extends OT> valueType, Getter<Exception> valueGetter) {