Source: app/util/awardListCTA/awardListCTAHelper.js

import {
  AwardType,
  BodyMagicGoalTypeToAwardMap,
  awardCollection,
} from 'common/src/app/data/enum/AwardType';

/**
 * Returns if we should display the start body magic award call to action
 * @param isPrivate
 * @param awards
 * @param activeBodyMagicAwardLength
 * @param awardType
 */
export function noBodyMagicAwards({ isPrivate, awards, activeBodyMagicAwardLength, awardType }) {
  if (
    !isPrivate &&
    awardType === AwardType.BODY_MAGIC &&
    activeBodyMagicAwardLength === 0 &&
    getAwardsByType(AwardType.BODY_MAGIC, awards).length === 0
  ) {
    return true;
  }
  return false;
}

/**
 * Returns if we should display the start weight loss award call to action
 * @param isPrivate
 * @param isOnJourney
 * @param awards
 * @param upcomingAwardLength
 * @param awardType
 */
export function noWeightLossAwards({
  isPrivate,
  isOnJourney,
  awards,
  upcomingAwardLength,
  awardType,
}) {
  if (
    !isPrivate &&
    !isOnJourney &&
    awardType === AwardType.WEIGHT &&
    upcomingAwardLength === 0 &&
    getAwardsByType(AwardType.WEIGHT, awards).length === 0
  ) {
    return true;
  }
  return false;
}

/**
 * Check to see if a the user has a active body magic award
 * @param awardType
 * @param activeBodyMagicAward
 * @param isPrivate
 */

export const hasActiveBodyMagicAward = ({ isPrivate, awardType, activeBodyMagicAwardLength }) => {
  if (!isPrivate && awardType === AwardType.BODY_MAGIC && activeBodyMagicAwardLength > 0) {
    return true;
  }
  return false;
};

/**
 * Returns achieved awards by passed type
 * @param awardType
 * @param myAwards
 */
export const getAwardsByType = (awardType, myAwards) =>
  myAwards
    .reduce((accumulator, awardData) => {
      if (
        awardCollection[awardType].indexOf(awardData && awardData.type && awardData.type.id) > -1
      ) {
        accumulator.push(awardData);
      }
      return accumulator;
    }, [])
    .sort(awardComparer)
    .filter(x => !!x);

/**
 * Sort awards by datetime
 * @param a
 * @param b
 * @returns {number}
 */
export function awardComparer(a, b) {
  if (a.achievedDateUTC < b.achievedDateUTC) {
    return 1;
  }
  if (a.achievedDateUTC > b.achievedDateUTC) {
    return -1;
  }
  if (a.type.id < b.type.id) {
    return -1;
  }
  if (a.type.id > b.type.id) {
    return 1;
  }
  return 0;
}

/**
 * Returns if a member has has a past body magic award and not got one in
 * progress in order to display the next award CTA
 * @param awardType
 * @param myAwards
 */
export const canStartBodyMagicAward = ({ awardType, awards, activeBodyMagicAwardLength }) => {
  if (
    !awards?.some(({ type: { id } }) => Object.values(BodyMagicGoalTypeToAwardMap).includes(id))
  ) {
    return false;
  }

  if (awardType !== AwardType.BODY_MAGIC) {
    return false;
  }

  if (awards.length <= 1 || activeBodyMagicAwardLength) {
    return false;
  }
  return true;
};

export const AwardInformationHide = {
  DESCRIPTION: 'description',
  LABEL: 'label',
};