import { TranslatePipe } from '@ngx-translate/core';
import { IArrivalStation, IDepartureStation } from '../models-legacy';
import { TransferTimePipe } from './transfer-time.pipe';

describe('TransferTimePipe', () => {
    let translate: TranslatePipe;
    let pipe: TransferTimePipe;
    const arrival: IArrivalStation = {
        times: {
            scheduled: {
                utc: new Date(2021, 10, 18, 17, 10),
            },
            latest: {
                utc: new Date(2021, 10, 18, 20, 5),
            },
        },
    } as any;

    const departure: IDepartureStation = {
        times: {
            scheduled: {
                utc: new Date(2021, 10, 18, 20, 20),
            },
            latest: {
                utc: new Date(2021, 10, 18, 21, 30),
            },
        },
    } as any;

    beforeEach(() => {
        translate = {
            transform: jasmine
                .createSpy('transform')
                .withArgs('SHARED.SHORT-HOUR')
                .and.returnValue('h')
                .withArgs('SHARED.SHORT-MIN')
                .and.returnValue('m')
                .withArgs('FLIGHT-STATUSES.Unknown')
                .and.returnValue('Unknown'),
        } as any;
        pipe = new TransferTimePipe(translate);
    });

    it('should return empty on empty input', () => {
        expect(pipe.transform({ arrival: undefined, departure: undefined }, null)).toBe('');
        expect(pipe.transform({ arrival: null, departure: null }, undefined)).toBe('');

        expect(pipe.transform({ arrival, departure: undefined }, null)).toBe('');
        expect(pipe.transform({ arrival: undefined, departure }, null)).toBe('');
    });

    it('should return valid transfer time based on requested time when both actual and scheduled times given', () => {
        expect(pipe.transform({ arrival, departure }, 'actual')).toBe('1h 25m');
        expect(pipe.transform({ arrival, departure }, 'scheduled')).toBe('3h 10m');
    });

    it('should return 0h 0m on broken input', () => {
        expect(pipe.transform({} as any, null)).toBe('');
    });

    it('should return unknown on less than 15min or negative', () => {
        expect(pipe.transform({ arrival: departure, departure: arrival } as any, null)).toBe('Unknown'); //negative
        expect(pipe.transform({ arrival, departure: arrival } as any, null)).toBe('Unknown'); //same (aka 0h 0m)
    });
});
