import { Component, DestroyRef, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { Event, Router, RouterOutlet, NavigationStart, NavigationEnd } from '@angular/router';
import { PageLoaderComponent } from './layout/page-loader/page-loader.component';

@Component({
	selector: 'app-root',
	templateUrl: './app.component.html',
	styleUrls: ['./app.component.scss'],
	standalone: true,
	imports: [RouterOutlet, PageLoaderComponent],
})
export class AppComponent {
	currentUrl!: string;
	private readonly destroyRef = inject(DestroyRef);

	constructor(public _router: Router) {
		this._router.events.pipe(takeUntilDestroyed(this.destroyRef)).subscribe((routerEvent: Event) => {
			if (routerEvent instanceof NavigationStart) {
				this.currentUrl = routerEvent.url.substring(
					routerEvent.url.lastIndexOf('/') + 1
				);
			}
			if (routerEvent instanceof NavigationEnd) {
				/* empty */
			}
			window.scrollTo(0, 0);
		});
	}
}
