export const formatScore = (val) => {
  let scores = val?.split('-');
  scores = scores?.map((score) => {
    const trimmedScore = score?.trim();

    if (/^0\d$/.test(trimmedScore)) {
      return trimmedScore.substring(1);
    }

    return trimmedScore;
  });

  return scores?.join(' - ');
};

export const formatLowerCase = (text) => {
  if (!text) {
    return '';
  }

  const stringsArr = text.split(' ');
  return stringsArr.reduce((acc, currentWord) => {
    acc += `${currentWord.charAt(0)} ${currentWord.slice(1, currentWord.length).toLowerCase()} `;
    return acc;
  }, '');
};

export const getLastWeekday = (weekday) => {
  const todaysDate = new Date();
  todaysDate.setDate(todaysDate.getDate() - 1);
  const currentDateObj = todaysDate;
  currentDateObj.setDate(currentDateObj.getDate() - ((currentDateObj.getDay() + weekday) % 7));
  return currentDateObj;
};

export function getApiQueryParams(moduleName) {
  let type = '';
  let lotteryName = '';
  let listType = '';
  let secondTableStart = 0;
  let firstTable = [0, 8];
  let secondTable = [8, 16];
  let title = '';

  switch (moduleName) {
    case 'sportstake8resultsmidweek':
      type = 'results';
      lotteryName = 'sportstake8';
      secondTableStart = 16;
      listType = 'MIDWEEK';
      title = 'Sportstake 8 Results Midweek';
      break;
    case 'sportstake8resultsweekend':
      lotteryName = 'sportstake8';
      type = 'results';
      secondTableStart = 16;
      listType = 'WEEKEND';
      title = 'Sportstake 8 Results Weekend';
      break;
    case 'sportstake8fixturesmidweek':
      lotteryName = 'sportstake8';
      type = 'fixtures';
      listType = 'MIDWEEK';
      title = 'Sportstake 8 Fixtures Midweek';
      break;
    case 'sportstake8fixturesweekend':
      lotteryName = 'sportstake8';
      type = 'fixtures';
      listType = 'WEEKEND';
      title = 'Sportstake 8 Fixtures Weekend';
      break;
    case 'sportstake13resultsmidweek':
      lotteryName = 'sportstake13';
      type = 'results';
      secondTableStart = 13;
      listType = 'MIDWEEK';
      title = 'Sportstake 13 Results Midweek';
      break;
    case 'sportstake13resultsweekend':
      lotteryName = 'sportstake13';
      type = 'results';
      secondTableStart = 13;
      listType = 'WEEKEND';
      title = 'Sportstake 13 Results Weekend';
      break;
    case 'sportstake13fixturesmidweek':
      lotteryName = 'sportstake13';
      type = 'fixtures';
      listType = 'MIDWEEK';
      title = 'Sportstake 13 Fixtures Midweek';
      break;
    case 'sportstake13fixturesweekend':
      lotteryName = 'sportstake13';
      type = 'fixtures';
      listType = 'WEEKEND';
      title = 'Sportstake 13 Fixtures Weekend';
      break;
    case 'sportstake4resultsmidweek':
      lotteryName = 'sportstake4';
      type = 'results';
      listType = 'MIDWEEK';
      secondTableStart = 8;
      firstTable = [0, 4];
      secondTable = [4, 8];
      title = 'Sportstake 4 Results Midweek';
      break;
    case 'sportstake4fixturesmidweek':
      lotteryName = 'sportstake4';
      type = 'fixtures';
      listType = 'MIDWEEK';
      firstTable = [0, 4];
      secondTable = [4, 8];
      title = 'Sportstake 4 Fixtures Midweek';
      break;
    case 'sportstakecricketresultsweekend':
      lotteryName = 'sportstake-cricket';
      type = 'results';
      listType = 'WEEKEND';
      secondTableStart = 10;
      firstTable = [0, 10];
      secondTable = [10, 20];
      title = 'Sportstake Cricket Results Weekend';
      break;
    case 'sportstakecricketfixturesweekend':
      lotteryName = 'sportstake-cricket';
      type = 'fixtures';
      listType = 'WEEKEND';
      firstTable = [0, 10];
      secondTable = [10, 20];
      title = 'Sportstake Cricket Fixtures Weekend';
      break;
    case 'sportstakerugbyresultsweekend':
      lotteryName = 'sportstake-rugby';
      type = 'results';
      listType = 'WEEKEND';
      secondTableStart = 10;
      firstTable = [0, 10];
      secondTable = [10, 20];
      title = 'Sportstake Rugby Fixtures Weekend';
      break;
    case 'sportstakerugbyfixturesweekend':
      lotteryName = 'sportstake-rugby';
      type = 'fixtures';
      listType = 'WEEKEND';
      firstTable = [0, 10];
      secondTable = [10, 20];
      title = 'Sportstake Rugby Fixtures Weekend';
      break;
    default:
      break;
  }

  return {
    type,
    lotteryName,
    listType,
    secondTableStart,
    firstTable,
    secondTable,
    title,
  };
}
