import React from "react";
import { View, Text, TextStyle, StyleSheet } from "react-native";
import { toNumberWithDefault } from "@applicaster/zapp-react-native-utils/numberUtils";
import { isNilOrEmpty } from "@applicaster/zapp-react-native-utils/reactUtils/helpers";

type Props = {
  config: {
    titleColor: string;
    summaryColor: string;
    backgroundColor: string;
    backgroundImage: string;
    isRTL: boolean;
    titleFontFamily: string;
    titleFontSize: number;
  };
  title: string | number;
};

const styles = StyleSheet.create({
  container: {
    width: 600,
    marginBottom: 12,
  },
});

const textStyles = ({ titleColor, isRTL, titleFontFamily, titleFontSize }) => ({
  textAlign: (isRTL ? "right" : "left") as TextStyle["textAlign"],
  fontSize: toNumberWithDefault(38, titleFontSize),
  fontWeight: "600" as TextStyle["fontWeight"],
  color: titleColor,
  fontFamily: titleFontFamily || null,
});

export function Title({ title, config }: Props) {
  if (isNilOrEmpty(title)) {
    return null;
  }

  return (
    <View style={styles.container}>
      <Text style={textStyles(config)} numberOfLines={3}>
        {title}
      </Text>
    </View>
  );
}
