/* eslint-disable import/prefer-default-export */
import { getApiQueryParams } from './sportstake';
import { getApiBasedNames } from './lotto';
import { getFixtures, getLatestResults, getAvailableDrawDays } from './services';

export async function getAPIData(page) {
  let responseFixtures = {};
  let responseResults = {};
  let responseLotto = {};

  const containsFixtures = page.categories.filter((category) => category.short_name === 'fixtures');
  const containsResults = page.categories.filter((category) => category.short_name === 'results');
  const containsLotto = page.categories.filter((category) => category.short_name === 'lotto');

  if (containsFixtures.length >= 1) {
    const filteredArray = page?.sections?.main?.modules.filter(
      (x) => x.value_type === 'template_block'
    );

    const responseArray = await Promise.all(
      filteredArray.map(async (module) => {
        const moduleName = module.name;

        // return if results to avoid duplicates
        if (moduleName.includes('results')) return;

        const query = getApiQueryParams(moduleName);

        const res = await getFixtures(`${query.lotteryName}/${query.listType}`);
        return { name: moduleName, result: res };
      })
    );

    responseFixtures = {
      data: responseArray.filter((n) => n),
    };
  }

  if (containsResults.length >= 1) {
    const filteredArray = page?.sections?.main?.modules.filter(
      (x) => x.value_type === 'template_block'
    );

    const responseArray = await Promise.all(
      filteredArray.map(async (module) => {
        const moduleName = module.name;

        // return if fixtures to avoid duplicates
        if (moduleName.includes('fixtures')) return;

        const newSportStakes =
          moduleName.includes('cricket') ||
          moduleName.includes('sportstake4') ||
          moduleName.includes('rugby');
        const query = getApiQueryParams(moduleName);
        const endPoint = newSportStakes
          ? `${query.lotteryName}`
          : `${query.lotteryName}?list_type=${query.listType}`;

        const res = await getLatestResults(endPoint);
        return { name: moduleName, result: res };
      })
    );

    responseResults = {
      data: responseArray.filter((n) => n),
    };
  }

  if (containsLotto.length >= 1) {
    const filteredArray = page?.sections?.main?.modules.filter(
      (x) => x.value_type === 'template_block'
    );

    const responseArray = await Promise.all(
      filteredArray.map(async (module) => {
        const moduleName = module.name;
        const apiBaseNames = getApiBasedNames(moduleName);

        const getApiDrawDates = async (name) => {
          const data = await getAvailableDrawDays(
            name,
            'WEEKEND',
            new Date().getMonth() + 1,
            new Date().getFullYear()
          );
          return data;
        };

        // get all api calls available dates, and add them in an array.
        const availableDates = (
          await Promise.all(apiBaseNames.map((name) => getApiDrawDates(name)))
        ).reduce((acc, current) => [...acc, ...current]);

        const latestDrawResults = await Promise.all(
          apiBaseNames.map((name) => getLatestResults(name))
        );

        return { name: moduleName, result: { availableDates, latestDrawResults } };
      })
    );

    responseLotto = {
      data: responseArray.filter((n) => n),
    };
  }

  return {
    props: {
      fixtures: responseFixtures,
      results: responseResults,
      lotto: responseLotto,
    },
  };
}
