import {CoachmarkStep} from '@components';
import i18n from '@config/i18n';
import {
  CHATS_FEATURES,
  CHATS_LOCAL_STORAGE_KEYS,
  FeatureKey,
} from '@modules/chats/constants';
import {getItemFromStorageSync} from '@services/localStorage';
import {isFeatureFlagEnabled} from '@stores/featureFlags';

const getFeatureModalStep = (feature: FeatureKey): Nullable<CoachmarkStep> => {
  switch (feature) {
    case FeatureKey.MessageInfo:
      return {
        key: feature,
        title: i18n.t('chat.features.info_message_title'),
        description: i18n.t('chat.features.info_message_subtitle'),
        image: require('@modules/chats/assets/features/messageInfo.webp'),
      };
    case FeatureKey.MentionAll:
      return {
        key: feature,
        title: i18n.t('chat.features.tag_all_title'),
        description: i18n.t('chat.features.tag_all_subtitle'),
        image: require('@modules/chats/assets/features/mentionAll.webp'),
      };
    case FeatureKey.SearchMessage:
      return {
        key: feature,
        title: i18n.t('chat.features.search_message_title'),
        description: i18n.t('chat.features.search_message_subtitle'),
        image: require('@modules/chats/assets/features/searchAMessage.webp'),
      };
    case FeatureKey.VoiceMessage:
      return {
        key: feature,
        title: i18n.t('chat.features.voice_message_title'),
        description: i18n.t('chat.features.voice_message_subtitle'),
        image: require('@modules/chats/assets/features/voiceNoteInEar.webp'),
      };
    case FeatureKey.NewCallsV2:
      return {
        key: feature,
        title: i18n.t('chat.features.calls_v2_title'),
        description: i18n.t('chat.features.calls_v2_subtitle'),
        image: require('@modules/chats/assets/features/callsV2.webp'),
      };
    case FeatureKey.MessagePin:
      if (!isFeatureFlagEnabled('CHATS_V2_PIN_MESSAGE')) {
        return null;
      }
      return {
        key: feature,
        title: i18n.t('chat.features.pin_message_title'),
        description: i18n.t('chat.features.pin_message_description'),
        image: require('@modules/chats/assets/features/pinChat.webp'),
      };
    default:
      return null;
  }
};

/**
 * Get the steps to show in the feature modal.
 * If the last feature shown is not in the list of features, we will show all features.
 * @returns Array of steps to show in the feature modal
 */
export const getFeatureModalSteps = (): CoachmarkStep[] => {
  const lastFeatureShown = getItemFromStorageSync({
    key: CHATS_LOCAL_STORAGE_KEYS.LastFeatureShown,
    isObject: false,
  }) as Nullable<string>;

  const lastIndexShown = CHATS_FEATURES.findIndex(
    feature => feature === lastFeatureShown,
  );

  const featuresToShow =
    lastIndexShown === -1
      ? CHATS_FEATURES
      : CHATS_FEATURES.slice(lastIndexShown + 1);

  return featuresToShow
    .map(feature => getFeatureModalStep(feature))
    .filter(feature => !!feature);
};
