import { Ranking } from '..';
import { filterAgeType, filterCountryType, filterRankingType } from './types';

export const applyModalSort = (
  allRanking: Ranking[],
  age: filterAgeType,
  ranking: filterRankingType,
  country: filterCountryType
) => {
  let filteredLocalData = allRanking;
  let indexCounter = 0;

  if (age) {
    filteredLocalData = filteredLocalData.filter(
      (user) => user.age >= age.min && user.age <= age.max
    );
  }

  if (country) {
    filteredLocalData = filteredLocalData.filter(
      (user) => user.country === country.two.toLowerCase()
    );
  }

  if (ranking) {
    switch (ranking?.label) {
      case 'NM':
        filteredLocalData = filteredLocalData.sort(
          (a: Ranking, b: Ranking) =>
            b.score_nm - a.score_nm || b.score_un - a.score_un || a.age - b.age
        );
        break;
      case 'DARE':
        filteredLocalData = filteredLocalData.sort(
          (a: Ranking, b: Ranking) =>
            b.score_dare - a.score_dare ||
            b.score_nm - a.score_nm ||
            b.score_un - a.score_un ||
            a.age - b.age
        );
        break;
      case 'UN':
        filteredLocalData = filteredLocalData.sort(
          (a: Ranking, b: Ranking) =>
            b.score_un - a.score_un || b.score_nm - a.score_nm || a.age - b.age
        );
        break;
      case 'UN+':
        filteredLocalData = filteredLocalData.sort(
          (a: Ranking, b: Ranking) =>
            b.score_unp - a.score_unp ||
            b.score_nm - a.score_nm ||
            b.score_un - a.score_un ||
            a.age - b.age
        );
        break;
      case 'TCC':
        filteredLocalData = filteredLocalData.sort(
          (a: Ranking, b: Ranking) =>
            b.score_tcc - a.score_tcc ||
            b.score_nm - a.score_nm ||
            b.score_un - a.score_un ||
            a.age - b.age
        );
        break;
      case 'DEEP':
        filteredLocalData = filteredLocalData.sort(
          (a: Ranking, b: Ranking) =>
            b?.score_deep - a?.score_deep ||
            b.score_nm - a.score_nm ||
            b.score_un - a.score_un ||
            a.age - b.age
        );
        break;
      case 'YES':
        const YESFilteredUsers = filteredLocalData.filter((user) => user.score_yes !== 10000);
        filteredLocalData = YESFilteredUsers.sort(
          (a: Ranking, b: Ranking) =>
            a.score_yes - b.score_yes ||
            b.score_nm - a.score_nm ||
            b.score_un - a.score_un ||
            a.age - b.age
        );
        break;
      case 'SLOW':
        const SLOWFilteredUsers = filteredLocalData.filter(
          (user) => user.score_slow < 4500 && user.score_slow > 0
        );
        filteredLocalData = SLOWFilteredUsers.sort(
          (a: Ranking, b: Ranking) =>
            b.score_slow - a.score_slow ||
            b.score_nm - a.score_nm ||
            b.score_un - a.score_un ||
            a.age - b.age
        );
        break;
      case 'WHS':
        filteredLocalData = filteredLocalData.sort(
          (a: Ranking, b: Ranking) =>
            b.score_whs - a.score_whs ||
            b.score_nm - a.score_nm ||
            b.score_un - a.score_un ||
            a.age - b.age
        );
        break;
      case 'KYE':
        filteredLocalData = filteredLocalData.sort(
          (a: Ranking, b: Ranking) =>
            b.score_kye - a.score_kye ||
            b.score_nm - a.score_nm ||
            b.score_un - a.score_un ||
            a.age - b.age
        );
        break;
      case 'TBT':
        filteredLocalData = filteredLocalData.sort(
          (a: Ranking, b: Ranking) =>
            b.score_tbt - a.score_tbt ||
            b.score_nm - a.score_nm ||
            b.score_un - a.score_un ||
            a.age - b.age
        );
        break;
      default:
        filteredLocalData = filteredLocalData.sort(
          (a: Ranking, b: Ranking) =>
            b.score_nm - a.score_nm || b.score_un - a.score_un || a.age - b.age
        );
        break;
    }
  }

  filteredLocalData = filteredLocalData.map((item) => {
    return item.dod !== 1
      ? { ...item, displayIndex: indexCounter++ }
      : { ...item, displayIndex: -1 };
  });

  return filteredLocalData;
};
