"use strict";
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// @ts-nocheck
const prop_types_1 = __importDefault(require("prop-types"));
const react_1 = __importDefault(require("react"));
const react_native_1 = require("react-native");
const Color_1 = __importDefault(require("./Color"));
const utils_1 = require("./utils");
const { carrot, emerald, peterRiver, wisteria, alizarin, turquoise, midnightBlue, } = Color_1.default;
const styles = react_native_1.StyleSheet.create({
    avatarStyle: {
        justifyContent: 'center',
        alignItems: 'center',
        width: 40,
        height: 40,
        borderRadius: 20,
    },
    avatarTransparent: {
        backgroundColor: Color_1.default.backgroundTransparent,
    },
    textStyle: {
        color: Color_1.default.white,
        fontSize: 16,
        backgroundColor: Color_1.default.backgroundTransparent,
        fontWeight: '100',
    },
});
class GiftedAvatar extends react_1.default.Component {
    constructor() {
        super(...arguments);
        this.avatarName = undefined;
        this.avatarColor = undefined;
        this.handleOnPress = () => {
            const _a = this.props, { onPress } = _a, other = __rest(_a, ["onPress"]);
            if (this.props.onPress) {
                this.props.onPress(other);
            }
        };
        this.handleOnLongPress = () => { };
    }
    setAvatarColor() {
        const userName = (this.props.user && this.props.user.name) || '';
        const name = userName.toUpperCase().split(' ');
        if (name.length === 1) {
            this.avatarName = `${name[0].charAt(0)}`;
        }
        else if (name.length > 1) {
            this.avatarName = `${name[0].charAt(0)}${name[1].charAt(0)}`;
        }
        else {
            this.avatarName = '';
        }
        let sumChars = 0;
        for (let i = 0; i < userName.length; i += 1) {
            sumChars += userName.charCodeAt(i);
        }
        // inspired by https://github.com/wbinnssmith/react-user-avatar
        // colors from https://flatuicolors.com/
        const colors = [
            carrot,
            emerald,
            peterRiver,
            wisteria,
            alizarin,
            turquoise,
            midnightBlue,
        ];
        this.avatarColor = colors[sumChars % colors.length];
    }
    renderAvatar() {
        const { user } = this.props;
        if (user) {
            if (typeof user.avatar === 'function') {
                return user.avatar([styles.avatarStyle, this.props.avatarStyle]);
            }
            if (typeof user.avatar === 'string') {
                return (react_1.default.createElement(react_native_1.Image, { source: { uri: user.avatar }, style: [styles.avatarStyle, this.props.avatarStyle] }));
            }
            if (typeof user.avatar === 'number') {
                return (react_1.default.createElement(react_native_1.Image, { source: user.avatar, style: [styles.avatarStyle, this.props.avatarStyle] }));
            }
        }
        return null;
    }
    renderInitials() {
        return (react_1.default.createElement(react_native_1.Text, { style: [styles.textStyle, this.props.textStyle] }, this.avatarName));
    }
    render() {
        if (!this.props.user
            || (!this.props.user.name && !this.props.user.avatar)) {
            // render placeholder
            return (react_1.default.createElement(react_native_1.View, { style: [
                    styles.avatarStyle,
                    styles.avatarTransparent,
                    this.props.avatarStyle,
                ], accessibilityTraits: "image" }));
        }
        if (this.props.user.avatar) {
            return (react_1.default.createElement(react_native_1.TouchableOpacity, { disabled: !this.props.onPress, onPress: this.props.onPress, onLongPress: this.props.onLongPress, accessibilityTraits: "image" }, this.renderAvatar()));
        }
        this.setAvatarColor();
        return (react_1.default.createElement(react_native_1.TouchableOpacity, { disabled: !this.props.onPress, onPress: this.props.onPress, onLongPress: this.props.onLongPress, style: [
                styles.avatarStyle,
                { backgroundColor: this.avatarColor },
                this.props.avatarStyle,
            ], accessibilityTraits: "image" }, this.renderInitials()));
    }
}
exports.default = GiftedAvatar;
GiftedAvatar.defaultProps = {
    user: {
        name: null,
        avatar: null,
    },
    onPress: undefined,
    onLongPress: undefined,
    avatarStyle: {},
    textStyle: {},
};
GiftedAvatar.propTypes = {
    user: prop_types_1.default.object,
    onPress: prop_types_1.default.func,
    onLongPress: prop_types_1.default.func,
    avatarStyle: utils_1.StylePropType,
    textStyle: utils_1.StylePropType,
};
//# sourceMappingURL=GiftedAvatar.js.map