import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/internal/Observable';
import { Router, RouterEvent, NavigationStart, ActivationStart, ActivatedRoute } from '@angular/router';
import { map, distinctUntilChanged, filter, switchMap, tap } from 'rxjs/operators';
import { isNullOrUndefined } from 'util';

@Injectable({ providedIn: 'root' })
export class LocationService {
  public currentPath$: Observable<string>;

  constructor(public router: Router) {
    this.currentPath$ = this.router.events.pipe(
      map((routerEvent: RouterEvent) => routerEvent.url),
      filter((path) => !isNullOrUndefined(path)),
      distinctUntilChanged()
    );
  }

  get url() {
    return this.router.url;
  }

  get url$() {
    return this.router.events.pipe(
      map((routerEvent: RouterEvent) => routerEvent.url),
      filter((url) => !isNullOrUndefined(url)),
      distinctUntilChanged()
    );
  }
}
