import { Injectable, Type } from '@angular/core';
import { QueryDecorator } from '../queries/classes/query.decorator';
import { getAnnotations } from '../google-decorator-factories';

class ReflectiveQueryMatcher<T> {
    constraints;

    constructor(type: Type<T>) {
        this.constraints = this.getTypeQuery(type);
    }

    getTypeQuery(type: Type<T>): QueryDecorator {
        return getAnnotations(type)[0];
    }

    matches(hypermedia): boolean {
        if (hypermedia === undefined) {
            return;
        }

        return this.constraints.class.every(cls => hypermedia.class && hypermedia.class.includes(cls));
    }
}

@Injectable()
export class ReflectiveQueryMatcherFactory {
    make<T>(type: Type<T>): ReflectiveQueryMatcher<T> {
        return new ReflectiveQueryMatcher<T>(type);
    }
}
