import { Class, Annotation } from '../reflect';
import { Type } from '../type';

export abstract class AnnotationUtils {
  public static hasAnnotation<T extends Annotation>(type: Type<any>, annotationClass: Type<T>): boolean {
    const annotation = AnnotationUtils.getAnnotation(type, annotationClass);
    if (undefined !== annotation) {
      return true;
    }
    return false;
  }

  public static getAnnotation<T extends Annotation>(type: Type<any>, annotationClass: Type<T>): T | undefined {
    const clazz = Class.forType(type);
    if (undefined === clazz) {
      return undefined;
    }

    const annotations = clazz.getAnnotations();
    if (0 === annotations.size) {
      return undefined;
    }

    for (const annonClassType of Array.from(annotations.keys())) {
      if (annonClassType === annotationClass) {
        return <T>annotations.get(annonClassType);
      }
      const annotation = AnnotationUtils.getAnnotation(annonClassType, annotationClass);
      if (undefined !== annotation) {
        return annotation;
      }
    }

    return undefined;
  }



}
