package utils;

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

public class Navigate {

	public static final List<String> emptyargs = new ArrayList<String>(0);

	public static String constructUrl(String pagename) {
		return constructUrl(pagename,emptyargs);
	}

	public static String constructUrl(String pagename, List<String> args, String... argtypes) {
		StringBuilder sb = new StringBuilder(256);
		String applicationURL = ThreadLocalPage.get().getAbsoluteLocation();

		List<String> routingapplied = Routing.processNavigate(applicationURL, pagename, args);
		if(routingapplied == null){
			sb
			.append(applicationURL)
			.append("/")
			.append(pagename);
			removeTrailingDefaultValues(args,argtypes);
			
			for(String arg : args){
				sb.append("/").append(arg);				
			}
		}
		else{
			boolean first = true;
			for(String arg : routingapplied){
				if(!first){	sb.append("/");	} else { first = false;	}
				sb.append(arg);				
			}
		}
		
		return sb.toString();
	}

	public static void removeTrailingDefaultValues(List<String> args, String... argtypes){
        for(int i = args.size() - 1; i >= 0; i--){
			if(   (args.get(i).equals("") && utils.TypesInfo.getStringCompatibleTypes().contains(argtypes[i]))
			   || (args.get(i).equals("0") && argtypes[i].equals("Int"))
			   || (args.get(i).equals("0.0") && argtypes[i].equals("Float"))
			){
				args.remove(i); 
			}
			else{
				break; // stop when non-default value is at the end
			}
		}
	}

}

@@ -90,81 +90,8 @@
  %7B%0A%0A
-%09public static final List%3CString%3E emptyargs = new ArrayList%3CString%3E(0);%0A%0A
 %09pub
@@ -175,17 +175,33 @@
 ame,
-emptyargs
+ new ArrayList%3CString%3E(0)
 );%0A%09

Fix issue where empty page arguments collection was modified and shared between requests