import { Component, Input, OnChanges, SimpleChanges } from '@angular/core';
import { airlines } from '@app/shared/airlines';

@Component({
    selector: 'partners-redirect-note',
    templateUrl: './partners-redirect-note.component.html',
    styleUrls: ['./partners-redirect-note.component.scss'],
})
export class PartnersRedirectNoteComponent implements OnChanges {
    @Input() partners?: string[];
    urls?: string[];

    ngOnChanges(changes: SimpleChanges) {
        if (this.partners?.length > 0 && 'partners' in changes) {
            this.urls = this.getPartnerUrls();
        }
    }

    private getPartnerUrls() {
        const partners = new Set(this.partners);

        return airlines
            .filter((a) => a.canRedirect)
            .filter((a) => partners.has(a.code))
            .map((a) => a.url);
    }
}

export function partnersWithRedirect(partners: string[]) {
    const hash = new Set(airlines.filter((a) => a.canRedirect).map((a) => a.code));
    return partners.filter((p) => hash.has(p));
}
