import { SearchFilter, UrlQueryParams } from '@/recruits/models';

export class AccountsSearchFilter extends SearchFilter {
  roleId = '';
  organizationId = '';
  departmentId = '';

  public countFilters(): number {
    let filtersCount = super.countFilters();
    if (this.roleId) {
      filtersCount++;
    }
    if (this.organizationId) {
      filtersCount++;
    }
    if (this.departmentId) {
      filtersCount++;
    }
    return filtersCount;
  }

  public prepareRequestParams(): UrlQueryParams {
    return Object.assign(super.prepareRequestParams(), {
      role: this.roleId || undefined,
      organization: this.organizationId || undefined,
      department: this.departmentId || undefined,
      search: this.search || undefined,
    });
  }

  public updateParamsFromRoute(query: { [key: string]: string }): void {
    super.updateParamsFromRoute(query);
    if (query.role) {
      this.roleId = String(query.role);
    }
    if (query.organization) {
      this.organizationId = String(query.organization);
    }
    if (query.department) {
      this.departmentId = String(query.department);
    }
    if (query.search) {
      this.search = String(query.search);
    }
  }
}
