import { Injectable, inject } from '@angular/core'
import { NavigationEnd, Router } from '@angular/router'
import { UntilDestroy } from '@ngneat/until-destroy'
import { BehaviorSubject, filter, startWith } from 'rxjs'

const DEFAULT_BG_CLASS = 'bg-white'

@UntilDestroy({ checkProperties: true })
@Injectable({
	providedIn: 'root',
})
export class RouterUtilsService {
	private readonly _router = inject(Router)

	private readonly bgClassSubject = new BehaviorSubject<string>(DEFAULT_BG_CLASS)
	readonly bgClass$ = this.bgClassSubject.asObservable()

	constructor() {
		this.subBgClassEvents()
	}

	private subBgClassEvents(): void {
		this._router.events
			.pipe(
				filter((event) => event instanceof NavigationEnd),
				startWith(this._router),
			)
			.subscribe((event) => {
				if (event instanceof Router || event instanceof NavigationEnd) {
					const bgColorClass = event.url.includes('settings') ? 'bg-[#f1f3f5]' : DEFAULT_BG_CLASS
					this.bgClassSubject.next(bgColorClass)
				}
			})
	}
}
