import {StyleProp, StyleSheet, TextStyle, Platform} from 'react-native';

import {TypographyWeight, WeightValues, GetTextStyleParams} from './interfaces';

// Do not replace this for global "isIos" constant. This is to avoid cycle dependency
const isIos = Platform.OS === 'ios';

export const getTextStyles = ({
  style,
  weight,
  fontStyle,
}: GetTextStyleParams): StyleProp<TextStyle> => {
  const flattenStyle = StyleSheet.flatten(style);
  const fontWeight = flattenStyle?.fontWeight || weight;
  const targetFontStyle = flattenStyle?.fontStyle || fontStyle;

  return [
    flattenStyle,
    getFontStyle(fontWeight as WeightValues, targetFontStyle),
  ];
};

/**
 * Get the correct text styles to apply fontWeight and fontStyle in Android and iOS
 */
export const getFontStyle = (
  weight: WeightValues,
  fontStyle?: TextStyle['fontStyle'],
): TextStyle => {
  const fontDecorator = !isIos && fontStyle === 'italic' ? 'Italic' : '';

  switch (weight) {
    case 'semiBold':
      return isIos
        ? {
            fontWeight: TypographyWeight.semiBold,
            fontStyle: isIos ? fontStyle : undefined,
          }
        : {
            fontFamily: `Roboto-SemiBold${fontDecorator}`,
          };
    case 'regular':
    default:
      return isIos
        ? {
            fontWeight: TypographyWeight.regular,
            fontStyle: isIos ? fontStyle : undefined,
          }
        : {
            fontFamily: fontDecorator ? 'Roboto-Italic' : 'Roboto-Regular',
          };
  }
};
