Source: app/util/groupSubscriptions/groupSubscriptionHelper.js

import xpressWeighAccountStateValues from 'common/src/app/data/xpressWeighAccountStateValues';

export const actions = {
  updateCard: true,
  switchToOnline: true,
};

/**
 * Convert Group Membership information to determine which locales,
 * buttons or subsections to display on "membership status" account settings page and homepage
 * Note this function will not make any assumptions about a member type (Group or Online)
 *
 * @param   {Object} xpressWeighState an object from /account/api/v1/accounts/me/group-membership-details
 * @returns {String} locale
 * @returns {Object} actions, optional, used to display CTAs or not
 * @returns {Number} timeUnits, optional, weeks inactive
 * @returns {Bool} displayOnHomepage, optional
 *
 */

const groupSubscriptionHelper = (xpressWeighState, hasConsultantRole) => {
  if (hasConsultantRole) {
    return { locale: 'consultant', displayOnHomepage: false };
  }

  const { xpressWeighAccountState } = xpressWeighState || {};

  const isAtTarget = xpressWeighState?.xpressWeighGroupMember?.isAtTarget;
  const cardNumber = xpressWeighState?.xpressWeighProfile?.xpressWeighCardNumber;

  const weeksSinceLastAttendance =
    xpressWeighState?.xpressWeighExpiryInfo?.weeksSinceLastAttendance;

  // This group member has a xpressWeighAccountState set to 0
  // This is becuase they dont have an entry in the xpressWeighprofiles table
  // The only way to be in this state is when the member has registered as a consultant
  // Check for undefined to stop flash of this message before the
  // xpressWeighAccountState is resolved.
  if (
    xpressWeighAccountState !== undefined &&
    xpressWeighAccountState === xpressWeighAccountStateValues.NO_GROUP_ACCOUNT
  ) {
    return { locale: 'activeInAttendance', displayOnHomepage: false };
  }

  // No card number, so your account has been transferred over from group
  if (xpressWeighAccountState !== undefined && !cardNumber) {
    return { actions, locale: 'noCard', displayOnHomepage: true };
  }

  // Your account has expired. Different message if you're at target or not.
  // Check for undefined to stop flash of this message before the
  // xpressWeighAccountState is resolved.
  if (
    xpressWeighAccountState !== undefined &&
    xpressWeighAccountState !== xpressWeighAccountStateValues.ACCOUNT_ACTIVE
  ) {
    return {
      locale: `membershipExpired${isAtTarget ? '.target' : '.standard'}`,
      actions,
      displayOnHomepage: true,
    };
  }

  // User not at target, inactive for 2 - 3 weeks
  else if ((weeksSinceLastAttendance === 2 || weeksSinceLastAttendance === 3) && !isAtTarget) {
    return {
      locale: `week${weeksSinceLastAttendance}`,
      timeUnits: weeksSinceLastAttendance,
      actions,
      displayOnHomepage: true,
    };
  }

  // User at target, inactive for 4 - 6 weeks
  else if (weeksSinceLastAttendance >= 4 && weeksSinceLastAttendance < 7 && isAtTarget) {
    return {
      locale: 'targetWeeks4to6',
      timeUnits: weeksSinceLastAttendance,
      actions,
      displayOnHomepage: true,
    };
  }

  // User at target, inactive for 7 weeks
  else if (weeksSinceLastAttendance === 7 && isAtTarget) {
    return {
      locale: 'targetWeek7',
      timeUnits: weeksSinceLastAttendance,
      actions,
      displayOnHomepage: true,
    };
  }

  // Or your account is active, in regular attandance
  return { locale: 'activeInAttendance', displayOnHomepage: false };
};

export default groupSubscriptionHelper;