import { Pipe, PipeTransform } from '@angular/core';
import { KeyValue } from '@angular/common';
import { ProcessingStatusNameMap } from '../constants/processing-status-name.map';

@Pipe({
  name: 'processingStatusOptionsPipe',
})
export class ProcessingStatusOptionsPipe implements PipeTransform {
  transform(states: number[]): number[] {
    return states
      .filter((state) => {
        const result = ProcessingStatusNameMap.get(state);

        if (!result || result.disabled) {
          return false;
        }

        return true;
      })
      .sort((a, b) => {
        const aValue = ProcessingStatusNameMap.get(a);
        const bValue = ProcessingStatusNameMap.get(b);
        return aValue.value.toUpperCase() === bValue.value.toUpperCase()
          ? 0
          : aValue.value.toUpperCase() > bValue.value.toUpperCase()
          ? 1
          : -1;
      });
  }
}

@Pipe({
  name: 'processingStatusOptionsKeyValuePipe',
})
export class ProcessingStatusOptionsKeyValuePipe implements PipeTransform {
  transform(states: KeyValue<number, { value: string; disabled: boolean }>[]): KeyValue<number, { value: string; disabled: boolean }>[] {
    return states
      .filter((state) => !state.value.disabled)
      .sort((a, b) => {
        return a.value.value.toUpperCase() === b.value.value.toUpperCase()
          ? 0
          : a.value.value.toUpperCase() > b.value.value.toUpperCase()
          ? 1
          : -1;
      });
  }
}
