import { makePropDecorator } from '../../google-decorator-factories';
import { BindDecorator } from '../bind.decorator';

export interface BindLinkDecorator {
    (...relQueries: Array<Array<string>>): any;
    new(...relQueries: Array<Array<string>>): any;
}

export const Link: BindLinkDecorator = makePropDecorator('BindLink', (...relQueries: Array<Array<string>>) => {
    return ({
        binding: {
            priority: 1,
            bind: (({ links = [] }) => {
                return links.find(({ rel: existingRels = [] }) => {
                    return relQueries.some(relQuery => {
                        return relQuery.every(rel => existingRels.includes(rel));
                    });
                });
            })
        } as BindDecorator
    }) as any;
});
