import React from 'react';
import type { ReactNode } from 'react';
import type {
  TextProps as NativeTextProps,
  StyleProp,
  TextStyle,
} from 'react-native';
import { StyledLabel } from './StyledLabel';
import type { TypographyIntent } from '../types';
import { pickAccessibilityProps } from '../utils';
import GradientText from '../GradientText';

export interface LabelProps extends NativeTextProps {
  /**
   * Text content.
   */
  children: ReactNode;
  /**
   * Visual intent color to apply to the text.
   */
  intent?: TypographyIntent;
  /**
   * Additional style.
   */
  style?: StyleProp<TextStyle>;
  /**
   * Testing id of the component.
   */
  testID?: string;
  /**
   * Font style to apply to the text.
   */
  fontStyle?: 'normal' | 'italic';
  /**
   * Font weight. Only `'medium'` (500) is available in addition to the default `'regular'` (400).
   * Medium weight is supported for the neutral typeface only.
   */
  fontWeight?: 'regular' | 'medium';
}

const Label = ({
  children,
  intent = 'body',
  allowFontScaling = false,
  fontStyle = 'normal',
  fontWeight = 'regular',
  style,
  testID,
  ...nativeProps
}: LabelProps) => {
  const isAi = intent === 'ai';

  const styledText = (
    <StyledLabel
      {...nativeProps}
      themeIntent={isAi ? 'body' : intent}
      themeIsItalic={fontStyle === 'italic'}
      themeFontWeight={fontWeight}
      allowFontScaling={allowFontScaling}
      style={style}
      testID={testID}
    >
      {children}
    </StyledLabel>
  );

  if (isAi) {
    return (
      <GradientText {...pickAccessibilityProps(nativeProps)}>
        {styledText}
      </GradientText>
    );
  }

  return styledText;
};

export default Label;
