"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 });
const prop_types_1 = __importDefault(require("prop-types"));
const react_1 = __importDefault(require("react"));
const react_native_1 = require("react-native");
const Avatar_1 = __importDefault(require("./Avatar"));
const Bubble_1 = __importDefault(require("./Bubble"));
const SystemMessage_1 = __importDefault(require("./SystemMessage"));
const Day_1 = __importDefault(require("./Day"));
const utils_1 = require("./utils");
const styles = {
    left: react_native_1.StyleSheet.create({
        container: {
            flexDirection: 'row',
            alignItems: 'flex-end',
            justifyContent: 'flex-start',
            marginLeft: 8,
            marginRight: 0,
        },
    }),
    right: react_native_1.StyleSheet.create({
        container: {
            flexDirection: 'row',
            alignItems: 'flex-end',
            justifyContent: 'flex-end',
            marginLeft: 0,
            marginRight: 8,
        },
    }),
};
class Message extends react_1.default.Component {
    shouldComponentUpdate(nextProps) {
        const next = nextProps.currentMessage;
        const current = this.props.currentMessage;
        const { previousMessage, nextMessage } = this.props;
        const nextPropsMessage = nextProps.nextMessage;
        const nextPropsPreviousMessage = nextProps.previousMessage;
        const shouldUpdate = (this.props.shouldUpdateMessage
            && this.props.shouldUpdateMessage(this.props, nextProps))
            || false;
        return (next.sent !== current.sent
            || next.received !== current.received
            || next.pending !== current.pending
            || next.createdAt !== current.createdAt
            || next.text !== current.text
            || next.image !== current.image
            || next.video !== current.video
            || next.audio !== current.audio
            || previousMessage !== nextPropsPreviousMessage
            || nextMessage !== nextPropsMessage
            || shouldUpdate);
    }
    renderDay() {
        if (this.props.currentMessage && this.props.currentMessage.createdAt) {
            const _a = this.props, { containerStyle, onMessageLayout } = _a, props = __rest(_a, ["containerStyle", "onMessageLayout"]);
            if (this.props.renderDay) {
                return this.props.renderDay(props);
            }
            return react_1.default.createElement(Day_1.default, Object.assign({}, props));
        }
        return null;
    }
    renderBubble() {
        const _a = this.props, { containerStyle, onMessageLayout } = _a, props = __rest(_a, ["containerStyle", "onMessageLayout"]);
        if (this.props.renderBubble) {
            return this.props.renderBubble(props);
        }
        // @ts-ignore
        return react_1.default.createElement(Bubble_1.default, Object.assign({}, props));
    }
    renderSystemMessage() {
        const _a = this.props, { containerStyle, onMessageLayout } = _a, props = __rest(_a, ["containerStyle", "onMessageLayout"]);
        if (this.props.renderSystemMessage) {
            return this.props.renderSystemMessage(props);
        }
        return react_1.default.createElement(SystemMessage_1.default, Object.assign({}, props));
    }
    renderAvatar() {
        const { user, currentMessage, showUserAvatar } = this.props;
        if (user
            && user._id
            && currentMessage
            && currentMessage.user
            && user._id === currentMessage.user._id
            && !showUserAvatar) {
            return null;
        }
        if (currentMessage
            && currentMessage.user
            && currentMessage.user.avatar === null) {
            return null;
        }
        const _a = this.props, { containerStyle, onMessageLayout } = _a, props = __rest(_a, ["containerStyle", "onMessageLayout"]);
        return react_1.default.createElement(Avatar_1.default, Object.assign({}, props));
    }
    render() {
        const { currentMessage, onMessageLayout, nextMessage, position, containerStyle, } = this.props;
        if (currentMessage) {
            const sameUser = utils_1.isSameUser(currentMessage, nextMessage);
            return (react_1.default.createElement(react_native_1.View, { onLayout: onMessageLayout },
                this.renderDay(),
                currentMessage.system ? (this.renderSystemMessage()) : (react_1.default.createElement(react_native_1.View, { style: [
                        styles[position].container,
                        { marginBottom: sameUser ? 2 : 10 },
                        !this.props.inverted && { marginBottom: 2 },
                        containerStyle && containerStyle[position],
                    ] },
                    this.props.position === 'left' ? this.renderAvatar() : null,
                    this.renderBubble(),
                    this.props.position === 'right' ? this.renderAvatar() : null))));
        }
        return null;
    }
}
exports.default = Message;
Message.defaultProps = {
    renderAvatar: undefined,
    renderBubble: null,
    renderDay: null,
    renderSystemMessage: null,
    position: 'left',
    currentMessage: {},
    nextMessage: {},
    previousMessage: {},
    user: {},
    containerStyle: {},
    showUserAvatar: false,
    inverted: true,
    shouldUpdateMessage: undefined,
    onMessageLayout: undefined,
};
Message.propTypes = {
    renderAvatar: prop_types_1.default.func,
    showUserAvatar: prop_types_1.default.bool,
    renderBubble: prop_types_1.default.func,
    renderDay: prop_types_1.default.func,
    renderSystemMessage: prop_types_1.default.func,
    position: prop_types_1.default.oneOf(['left', 'right']),
    currentMessage: prop_types_1.default.object,
    nextMessage: prop_types_1.default.object,
    previousMessage: prop_types_1.default.object,
    user: prop_types_1.default.object,
    inverted: prop_types_1.default.bool,
    containerStyle: prop_types_1.default.shape({
        left: utils_1.StylePropType,
        right: utils_1.StylePropType,
    }),
    shouldUpdateMessage: prop_types_1.default.func,
    onMessageLayout: prop_types_1.default.func,
};
//# sourceMappingURL=Message.js.map