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

export const DEPARTMENTS_QUERY_TYPE = 'department';
export const ORGANIZATIONS_QUERY_TYPE = 'organization';
export const ALL_QUERY_TYPE = 'all';

export class OrganizationsSearchFilter extends SearchFilter {
  subjectType:
    | typeof DEPARTMENTS_QUERY_TYPE
    | typeof ORGANIZATIONS_QUERY_TYPE
    | typeof ALL_QUERY_TYPE = ALL_QUERY_TYPE;

  public prepareRequestParams(): UrlQueryParams {
    return Object.assign(super.prepareRequestParams(), {
      subjecttype: this.subjectType || undefined,
    });
  }

  public updateParamsFromRoute(query: { [key: string]: string }): void {
    super.updateParamsFromRoute(query);
    switch (query.subjecttype) {
      case DEPARTMENTS_QUERY_TYPE:
        this.subjectType = DEPARTMENTS_QUERY_TYPE;
        break;
      case ORGANIZATIONS_QUERY_TYPE:
        this.subjectType = ORGANIZATIONS_QUERY_TYPE;
        break;
      default:
        this.subjectType = ALL_QUERY_TYPE;
        break;
    }
  }
}
