import { AmplitudeEventBase } from '../models/amplitude/AmplitudeEventBase';
import { IAmplitudeEventWithSource } from '../models/amplitude/IAmplitudeEventWithSource';
import { Source } from '../models/nonstophome/source';
import { BaseService } from './abstract/baseService';

class AmplitudeEventModificationService extends BaseService {
    public static sharedInstance = new AmplitudeEventModificationService();

    protected logTag: string = 'AmplitudeEventModificationService';

    private nonstopHomeArrivalUrl: string;

    public setNonstopArrivalUrl(nonstopHomeArrivalUrl: string) {
        this.nonstopHomeArrivalUrl = nonstopHomeArrivalUrl;
    }

    public modifyEvent(event: AmplitudeEventBase) {
        this.addSource(event);
    }

    public onRouteChange() {
        this.nonstopHomeArrivalUrl = '';
    }

    private addSource(event: AmplitudeEventBase) {
        if (this.hasSource(event)) {
            const deeplinkPath = (event as IAmplitudeEventWithSource).GetDeeplink();
            const isDeeplink = deeplinkPath === this.nonstopHomeArrivalUrl;
            (event as IAmplitudeEventWithSource).Source = isDeeplink ? Source.DeepLink : Source.Manual;
        }
    }

    private hasSource(event: any): event is IAmplitudeEventWithSource {
        return event.GetDeeplink !== undefined;
    }
}

export default AmplitudeEventModificationService.sharedInstance as AmplitudeEventModificationService;