import React from 'react';
import { Text, TextStyle, StyleSheet, I18nManager } from 'react-native';
import { colors, typography } from '../../theme/tokens';

interface TypographyProps {
  children: React.ReactNode;
  style?: TextStyle | TextStyle[];
  color?: string;
  align?: 'left' | 'center' | 'right';
  numberOfLines?: number;
}

function makeComponent(baseStyle: TextStyle) {
  return function TypographyComponent({
    children,
    style,
    color,
    align,
    numberOfLines,
  }: TypographyProps) {
    return (
      <Text
        style={[
          baseStyle,
          { writingDirection: I18nManager.isRTL ? 'rtl' : 'ltr' },
          color ? { color } : undefined,
          align ? { textAlign: align } : undefined,
          style,
        ]}
        numberOfLines={numberOfLines}
        accessibilityRole="text"
      >
        {children}
      </Text>
    );
  };
}

export const H1 = makeComponent({
  fontFamily: typography.fontFamily.bold,
  fontSize: typography.fontSize['3xl'],
  lineHeight: typography.fontSize['3xl'] * typography.lineHeight.tight,
  color: colors.neutral[900],
  letterSpacing: -0.5,
});

export const H2 = makeComponent({
  fontFamily: typography.fontFamily.bold,
  fontSize: typography.fontSize['2xl'],
  lineHeight: typography.fontSize['2xl'] * typography.lineHeight.tight,
  color: colors.neutral[900],
  letterSpacing: -0.3,
});

export const H3 = makeComponent({
  fontFamily: typography.fontFamily.semibold,
  fontSize: typography.fontSize.xl,
  lineHeight: typography.fontSize.xl * typography.lineHeight.tight,
  color: colors.neutral[900],
});

export const Body = makeComponent({
  fontFamily: typography.fontFamily.regular,
  fontSize: typography.fontSize.base,
  lineHeight: typography.fontSize.base * typography.lineHeight.normal,
  color: colors.neutral[700],
});

export const BodyMedium = makeComponent({
  fontFamily: typography.fontFamily.medium,
  fontSize: typography.fontSize.base,
  lineHeight: typography.fontSize.base * typography.lineHeight.normal,
  color: colors.neutral[800],
});

export const Caption = makeComponent({
  fontFamily: typography.fontFamily.regular,
  fontSize: typography.fontSize.sm,
  lineHeight: typography.fontSize.sm * typography.lineHeight.normal,
  color: colors.neutral[500],
});

export const Label = makeComponent({
  fontFamily: typography.fontFamily.medium,
  fontSize: typography.fontSize.sm,
  lineHeight: typography.fontSize.sm * typography.lineHeight.normal,
  color: colors.neutral[600],
});
