import React, { useContext, useEffect, useState } from "react";
import { View, Text, Linking } from "react-native";
import { CometChatContext } from "../../CometChatContext";
import { emailPattern, phoneNumPattern, urlPattern } from "../../constants/UIKitConstants";
import { TextBubbleStyle } from "./TextBubbleStyle";
const Link = ({ text, url, style }) => {
    return <Text style={{ ...style, textDecorationLine: "underline" }} onPress={() => {
            let finalUrl = url.startsWith("http") ? url : `http://${url}`;
            Linking.canOpenURL(finalUrl)
                .then(res => {
                if (res) {
                    Linking.openURL(finalUrl);
                    return;
                }
                console.log("Can not open link", finalUrl);
            })
                .catch(err => {
                console.log({ url });
                console.log("Error:", err);
            });
        }}>{text}</Text>;
};
const getPatternGroup = (str) => {
    let result = {};
    if (str.match(phoneNumPattern))
        result['phone'] = str;
    if (str.match(emailPattern))
        result['email'] = str;
    if (str.match(urlPattern))
        result['url'] = str;
    return result;
};
export const CometChatTextBubble = (props) => {
    const { text, textContainerStyle, textFormatters } = props;
    const { theme } = useContext(CometChatContext);
    const [formattedText, setFormattedText] = useState();
    const _style = new TextBubbleStyle({
        backgroundColor: theme?.palette.getBackgroundColor(),
        textColor: theme?.palette.getAccent(),
        textFont: theme?.typography.body,
        ...props.style
    });
    const { backgroundColor, border, borderRadius, textColor, textFont, height, width } = _style;
    useEffect(() => {
        let finalText = text;
        if (textFormatters && textFormatters.length) {
            if (textFormatters) {
                for (let i = 0; i < textFormatters.length; i++) {
                    finalText = textFormatters[i].getFormattedText(finalText);
                }
            }
        }
        setFormattedText(finalText);
    }, []);
    return <View style={[
            {
                alignSelf: "flex-start",
            },
            {
                backgroundColor, borderRadius, maxHeight: height, maxWidth: width,
                overflow: "hidden", padding: 8, paddingBottom: 0
            },
            border,
            textContainerStyle
        ]}>
        <Text style={[{ color: textColor }, textFont]}>{formattedText}</Text>

    </View>;
};
CometChatTextBubble.defaultProps = {
    text: ""
};
//# sourceMappingURL=CometChatTextBubble.js.map