import { Component, EventEmitter, Input, Output } from '@angular/core';
import { Store } from '@ngrx/store';

import { links, loggedLinks } from '../header.component';
import { Router } from '@angular/router';
import { Observable } from 'rxjs';
import { UserStore } from 'src/app/core/interfaces/user.interface';

@Component({
  selector: 'app-nav-mobile',
  templateUrl: './nav-mobile.component.html',
  styleUrls: ['./nav-mobile.component.scss'],
})
export class NavMobileComponent {
  @Input() isVisible = false;
  @Output() isVisibleChange = new EventEmitter<boolean>();

  user$: Observable<UserStore>;
  links = links;

  constructor(
    private route: Router,
    private store: Store<{ user: UserStore }>,
  ) {
    this.user$ = this.store.select('user');
  }

  ngOnInit() {
    this.user$.subscribe({
      next: (data) => (this.links = data.id ? loggedLinks : links),
    });
  }

  redirectUser(url: string) {
    this.isVisibleChange.emit(false);
    this.route.navigate([url]);
  }
}
