import { Component, Input } from '@angular/core';
import { IFilterOption } from '@shared/ui-api/interfaces/route-config/filter-option';
import { FilteringService } from '@shared/ui-api/filtering.service';
import { NotificationService } from '@shared/ui-api/notification.service';
import { AuthService } from '@shared/auth/auth.service';

@Component({
  selector: 'app-objects-list-filtering-current-user',
  templateUrl: './objects-list-filtering-current-user.component.html',
  styleUrls: ['./objects-list-filtering-current-user.component.scss']
})
export class ObjectsListFilteringCurrentUserComponent {
  @Input() filterOption: IFilterOption;
  filteringValue: boolean;

  constructor(
    private filterService: FilteringService,
    private notificationService: NotificationService,
    private authService: AuthService,
  ) {
  }

  applyFiltering() {
    if (typeof this.filteringValue !== 'boolean') {
      return this.notificationService.showMessage($localize`Please enter a valid value`);
    }
    let field = this.filterOption.field;
    if (!this.filteringValue) {
      field += `__ne`;
    }
    this.filterService.addFilter(
      field,
      this.filteringValue ? this.authService.userData.user.id.toString() : this.authService.userData.user.id.toString(),
      this.filterOption?.resetQueryParams,
    );
  }
}
