import { Component } from '@angular/core';
import { Config } from '@core/config';
import { isEqual } from 'lodash';
import { BehaviorSubject, combineLatest } from 'rxjs';
import { map, shareReplay } from 'rxjs/operators';
import { TaskCalendarStore } from './task-calendar.store';

@Component({
  selector: 'page-task-calendar',
  templateUrl: 'page-task-calendar.component.html',
  styleUrls: ['page-task-calendar.component.scss'],
  providers: [TaskCalendarStore],
})
export class PageTaskCalendarComponent {
  filterActive$ = new BehaviorSubject<boolean>(false);
  taskCalendarKey = this._config.get('process.ids.taskCalendar');

  hasFilter$ = combineLatest([
    this.taskCalendarStore.selectFetching,
    this.taskCalendarStore.selectFilter,
    this.taskCalendarStore.selectInitialFilter,
  ]).pipe(
    map(([_, filter, initialFilter]) => {
      return !isEqual(filter?.getQueryParams(), initialFilter?.getQueryParams());
    }),
    shareReplay()
  );

  constructor(private taskCalendarStore: TaskCalendarStore, private readonly _config: Config) {
    this.taskCalendarStore.loadFilter();
  }
}
