package click.kamil.springstatemachineexporter.ast.common;

import org.eclipse.jdt.core.dom.ArrayType;
import org.eclipse.jdt.core.dom.Name;
import org.eclipse.jdt.core.dom.NameQualifiedType;
import org.eclipse.jdt.core.dom.ParameterizedType;
import org.eclipse.jdt.core.dom.PrimitiveType;
import org.eclipse.jdt.core.dom.QualifiedName;
import org.eclipse.jdt.core.dom.QualifiedType;
import org.eclipse.jdt.core.dom.SimpleType;
import org.eclipse.jdt.core.dom.Type;

public final class AstUtils {
    private AstUtils() {
    }

    public static String extractSimpleTypeName(Type type) {
        if (type.isSimpleType()) {
            Name name = ((SimpleType) type).getName();
            if (name instanceof QualifiedName qn) {
                return qn.getName().getIdentifier();
            }
            return name.toString();
        } else if (type.isParameterizedType()) {
            Type rawType = ((ParameterizedType) type).getType();
            return extractSimpleTypeName(rawType);
        } else if (type.isQualifiedType()) {
            return ((QualifiedType) type).getName().getIdentifier();
        } else if (type.isNameQualifiedType()) {
            return ((NameQualifiedType) type).getName().getIdentifier();
        } else if (type.isArrayType()) {
            return extractSimpleTypeName(((ArrayType) type).getElementType());
        } else if (type.isPrimitiveType()) {
            return ((PrimitiveType) type).getPrimitiveTypeCode().toString();
        } else {
            return type.toString(); // fallback
        }
    }
}
