package joliex.java.impl;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import jolie.lang.NativeType;
import jolie.lang.parse.ast.types.TypeChoiceDefinition;
import jolie.lang.parse.ast.types.TypeDefinition;
import jolie.lang.parse.ast.types.TypeDefinitionLink;
import jolie.lang.parse.ast.types.TypeInlineDefinition;

public class Utils {
	public static boolean hasSubTypes( TypeDefinition t )
	{
		if ( t instanceof TypeDefinitionLink) {
			return hasSubTypes(((TypeDefinitionLink) t).linkedType());
		} else if ( t instanceof TypeInlineDefinition) {
			return ((TypeInlineDefinition) t).hasSubTypes();
		} else if ( t instanceof TypeChoiceDefinition) {
			return (hasSubTypes(((TypeChoiceDefinition) t).left()) || hasSubTypes(((TypeChoiceDefinition) t).right()));
		} else {
			throw new UnsupportedOperationException( "Unsupported type class: " + t.getClass().getName() );
		}
	}

	public static Set< Map.Entry< String, TypeDefinition > > subTypes( TypeDefinition t ) {
		if ( t instanceof TypeDefinitionLink) {
			return subTypes(((TypeDefinitionLink) t).linkedType());
		} else if ( t instanceof TypeInlineDefinition) {
			return ((TypeInlineDefinition) t).subTypes();
		} else {
			throw new UnsupportedOperationException( "Unsupported type class: " + t.getClass().getName() );
		}
	}

	public static NativeType nativeType(TypeDefinition t){
		if ( t instanceof TypeDefinitionLink) {
			return nativeType(((TypeDefinitionLink) t).linkedType());
		} else if ( t instanceof TypeInlineDefinition) {
			return ((TypeInlineDefinition) t).nativeType();
		} else if ( t instanceof TypeChoiceDefinition) {
			return NativeType.ANY;
		} else {
			throw new UnsupportedOperationException( "Unsupported type class: " + t.getClass().getName() );
		}

	}

	public static  Set<NativeType> getTypes(TypeDefinition typeDefinition){
		Set<NativeType> choiceTypes = new HashSet<>();
		if (typeDefinition instanceof TypeChoiceDefinition){
			choiceTypes = getTypes(((TypeChoiceDefinition) typeDefinition).left());
			Set<NativeType> right = getTypes(((TypeChoiceDefinition) typeDefinition).right());
			if (right!=null){
				choiceTypes.addAll(right);
			}
		} else if (typeDefinition instanceof TypeDefinitionLink){
			return getTypes(((TypeDefinitionLink) typeDefinition).linkedType());
		} else if (typeDefinition instanceof TypeInlineDefinition){
			choiceTypes.add(((TypeInlineDefinition) typeDefinition).nativeType());
		}
		return choiceTypes;
	}
}

@@ -385,33 +385,32 @@
 ean hasSubTypes(
- 
 TypeDefinition t
@@ -413,20 +413,18 @@
 on t
+)
  
-)%0A%09
 %7B%0A%09%09if (
  t i
@@ -411,33 +411,32 @@
 tion t) %7B%0A%09%09if (
- 
 t instanceof Typ
@@ -521,33 +521,32 @@
 );%0A%09%09%7D else if (
- 
 t instanceof Typ
@@ -623,33 +623,32 @@
 );%0A%09%09%7D else if (
- 
 t instanceof Typ
@@ -825,33 +825,32 @@
 rationException(
- 
 %22Unsupported typ
@@ -876,33 +876,32 @@
 lass().getName()
- 
 );%0A%09%09%7D%0A%09%7D%0A%0A%09publ
@@ -914,17 +914,16 @@
 tic Set%3C
- 
 Map.Entr
@@ -924,17 +924,16 @@
 p.Entry%3C
- 
 String, 
@@ -946,19 +946,17 @@
 finition
- %3E 
+%3E
 %3E subTyp
@@ -958,17 +958,16 @@
 ubTypes(
- 
 TypeDefi
@@ -974,17 +974,16 @@
 nition t
- 
 ) %7B%0A%09%09if
@@ -976,33 +976,32 @@
 tion t) %7B%0A%09%09if (
- 
 t instanceof Typ
@@ -1083,33 +1083,32 @@
 );%0A%09%09%7D else if (
- 
 t instanceof Typ
@@ -1223,33 +1223,32 @@
 rationException(
- 
 %22Unsupported typ
@@ -1278,25 +1278,24 @@
 ().getName()
- 
 );%0A%09%09%7D%0A%09%7D%0A%0A%09
@@ -1347,24 +1347,25 @@
 ition t)
+ 
 %7B%0A%09%09if (
  t insta
@@ -1352,25 +1352,24 @@
  t) %7B%0A%09%09if (
- 
 t instanceof
@@ -1457,33 +1457,32 @@
 );%0A%09%09%7D else if (
- 
 t instanceof Typ
@@ -1566,17 +1566,16 @@
 lse if (
- 
 t instan
@@ -1683,17 +1683,16 @@
 ception(
- 
 %22Unsuppo
@@ -1734,17 +1734,16 @@
 etName()
- 
 );%0A%09%09%7D%0A%0A
@@ -1761,17 +1761,16 @@
  static 
- 
 Set%3CNati
@@ -1812,24 +1812,25 @@
 eDefinition)
+ 
 %7B%0A%09%09Set%3CNati
@@ -1913,32 +1913,33 @@
 hoiceDefinition)
+ 
 %7B%0A%09%09%09choiceTypes
@@ -2101,15 +2101,18 @@
 ight
+ 
 !=
+ 
 null)
+ 
 %7B%0A%09%09
@@ -2203,16 +2203,17 @@
 ionLink)
+ 
 %7B%0A%09%09%09ret
@@ -2338,16 +2338,17 @@
 inition)
+ 
 %7B%0A%09%09%09cho

fix identation