
import { generateArray } from './generators.mjs';

export function reverseDate(date) {
  return date.split('-').reverse().join('-');
}

export function zeroPadding(num, places) {
  return String(num).padStart(places, '0');
}

export function filterScheduleByTournaments(schedule, relationIds) {
  let tournamentIds = relationIds;

  if (!Array.isArray(relationIds)) {
    tournamentIds = [relationIds];
  }

  const scheduleArray = generateArray(schedule);
  for (let i = 0; i < scheduleArray.length; i += 1) {
    const tournaments = generateArray(scheduleArray[i].tournaments);
    const filteredByTournament = tournaments.filter((tournament) =>
      tournamentIds.includes(tournament.id)
    );

    if (filteredByTournament.length) {
      scheduleArray[i].tournaments = filteredByTournament;
    } else {
      scheduleArray[i] = null;
    }
  }
  return scheduleArray.filter((item) => item);
}

export function findTournaments(tournaments, relationIds) {
  return relationIds.reduce((acc, current) => {
    const tournament = tournaments[current];

    if (tournament) {
      acc.push(tournament);
    }
    return acc;
  }, []);
}

export function getSortedEvents(tournaments, sportEvents) {
  if (!tournaments || !sportEvents) {
    return [];
  }

  const eventsArr = [];
  tournaments.forEach((tournament) => {
    const { events } = tournament || {};

    if (events) {
      events.forEach((eventId) => {
        if (sportEvents[eventId]) eventsArr.push(sportEvents[eventId]);
      });
    }
  });

  return eventsArr.sort((a, b) => {
    const validDateA = `${reverseDate(a.date.date)}T${a.date.time}`;
    const validDateB = `${reverseDate(b.date.date)}T${b.date.time}`;

    return new Date(validDateA) - new Date(validDateB);
  });
}

export function filterScheduleByTeam(schedule, eventsArray) {
  const scheduleArray = generateArray(schedule);
  for (let i = 0; i < scheduleArray.length; i += 1) {
    const tournaments = generateArray(scheduleArray[i].tournaments);

    for (let j = 0; j < tournaments.length; j += 1) {
      const filteredByTeam = tournaments[j].events.filter((event) =>
        eventsArray.includes(event.id)
      );
      if (filteredByTeam.length) {
        tournaments[j].events = filteredByTeam;
      } else {
        tournaments[j].events = null;
      }
    }

    scheduleArray[i].tournaments = tournaments.filter((item) => item.events !== null);
  }
  return scheduleArray.filter((item) => item.tournaments.length > 0);
}

export function filterScheduleByDate(schedule) {
  const today = new Date().setHours(0, 0, 0, 0);
  Object.keys(schedule).forEach((key) => {
    if (new Date(key) < today) {
      delete schedule[key];
    }
  });

  return schedule;
}

export function filterEvents(tournamentEvents, events) {
  const futureEvents = [];
  const today = new Date().setHours(0, 0, 0, 0);
  tournamentEvents.forEach((eventId) => {
    if (events[eventId]) {
      const eventDate = new Date(
        `${events[eventId].date.year_number}-${zeroPadding(
          events[eventId].date.month_number,
          2
        )}-${zeroPadding(events[eventId].date.day_number, 2)}`
      );

      if (eventDate >= today) {
        futureEvents.push(eventId);
      }
    }
  });

  return futureEvents;
}
