import { SelectFilter, SelectFilterOption, FilterType } from 'apps/sales/src/app/modules/filter';
import { InputDTO, OptionDTO, InputType } from '@swagger/oms';

export function inputDtoToSelectedFilters(inputDTO: InputDTO): SelectFilter {
  return {
    key: inputDTO.key,
    name: inputDTO.label,
    max: inputDTO.options && inputDTO.options.max,
    type: inputTypeToType(inputDTO.type),
    options: inputDTO.options && inputDTO.options.values.map(optionDtoToSelectFilterOption),
  } as SelectFilter;
}

export function optionDtoToSelectFilterOption(optionDto: OptionDTO): SelectFilterOption {
  return {
    name: optionDto.label,
    id: optionDto.key || optionDto.value,
    selected: optionDto.selected || false,
    initial_selected_state: optionDto.selected || false,
    expanded: false,
    options: optionDto.values && optionDto.values.map(optionDtoToSelectFilterOption),
  } as SelectFilterOption;
}

export function inputTypeToType(type: InputType): FilterType {
  switch (type) {
    case 1:
      return 'text';

    case 2:
      return 'select';

    case 4:
      return 'checkbox';

    case 8 || 16:
      return 'date';

    case 32 || 64:
      return 'number';

    default:
      return 'select';
  }
}
