/* eslint-disable import/no-extraneous-dependencies */

/* -------------------------------------------------------------------------- */
/*                             GET RESULTS                             */
/* -------------------------------------------------------------------------- */
export const getResultsById = async (lotteryId) => {
  const res = await fetch(`${process.env.LOTTERY_API_URL}results/${lotteryId}`).catch(() => null);

  const data = res ? await res.json() : { success: false };

  return data.data;
};

/* -------------------------------------------------------------------------- */
/*                             GET LATEST RESULTS                             */
/* -------------------------------------------------------------------------- */
export const getLatestResults = async (lotteryPath) => {
  const res = await fetch(`${process.env.LOTTERY_API_URL}results/latest/${lotteryPath}`).catch(
    () => null
  );

  const data = res ? await res.json() : { success: false };

  return data.data;
};

/* -------------------------------------------------------------------------- */
/*                             GET Fixtures                                   */
/* -------------------------------------------------------------------------- */
export const getFixtures = async (lotteryPath) => {
  const res = await fetch(`${process.env.LOTTERY_API_URL}fixtures/${lotteryPath}`).catch(
    () => null
  );

  const data = res ? await res.json() : { success: false };

  return data.data;
};

/* -------------------------------------------------------------------------- */
/*                             GET Calendar Data                              */
/* -------------------------------------------------------------------------- */
export const getAvailableDrawDays = async (
  lotteryName,
  listType,
  month,
  year,
  listTypeParam = 'list_type'
) => {
  const res = await fetch(
    `${process.env.LOTTERY_API_URL}calendar/${lotteryName}?${listTypeParam}=${listType}&month=${month}&year=${year}`
  ).catch(() => null);

  const data = res ? await res.json() : { success: false };

  return Array.isArray(data?.data) ? data.data : [];
};

/* -------------------------------------------------------------------------- */
/*                             GET Lotto UK 49 Data                              */
/* -------------------------------------------------------------------------- */
export const getLottoUKdata = async (historical = false, latest = true, type = null, id) => {
  
  const res = await fetch(
    `${process.env.LOTTERY_API_URL}results${historical ? '/historical' : ''}${
      latest ? '/latest' : ''
    }${type ? `/${type}`:''}${id ? `/${id}` : ''}`
  ).catch(() => null);
  const data = res ? await res.json() : { success: false };
  return data.data;
};

export const getCalendarData = async (type, month, year) => {

  const res = await fetch(
    `${process.env.LOTTERY_API_URL}calendar/${type}?month=${month}&year=${year}`
  ).catch(() => null);
  const data = res ? await res.json() : { success: false };
  return Array.isArray(data?.data) ? data.data : [];
};
