'use strict';
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const classnames_1 = __importDefault(require("classnames"));
const React = __importStar(require("react"));
const omit_empty_1 = __importDefault(require("omit-empty"));
const Actions_1 = __importDefault(require("./Actions"));
const Author_1 = __importDefault(require("./Author"));
const Footer_1 = __importDefault(require("./Footer"));
const Fields_1 = __importDefault(require("./Fields"));
const Title_1 = __importDefault(require("./Title"));
const Image_1 = __importDefault(require("./Image"));
const ImageCaret_1 = __importDefault(require("./ImageCaret"));
class Attachment extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            imageCaretOpen: true
        };
        this.toggleImageCaret = this.toggleImageCaret.bind(this);
        this.isAttachmentEmpty = this.isAttachmentEmpty.bind(this);
    }
    render() {
        const isEmpty = this.isAttachmentEmpty();
        if (isEmpty)
            return null;
        const { messageId, messageIdx, attachmentId, ephemeral, attachment, openMenuUpwards, unfurls, parser, showAttachment } = this.props;
        const imageSize = unfurls && unfurls[attachment.image_url] && unfurls[attachment.image_url].size;
        let showCaretInImage = false;
        let showCaretInTitle = false;
        let showCaretInFooter = false;
        let showCaretInAuthor = false;
        let showCaretInText = false;
        if (attachment.image_url) {
            showCaretInImage = true;
            showCaretInTitle = false;
            showCaretInFooter = false;
            showCaretInAuthor = false;
            showCaretInText = false;
        }
        if (attachment.author_name && attachment.fields.length === 0) {
            showCaretInImage = false;
            showCaretInTitle = false;
            showCaretInFooter = false;
            showCaretInAuthor = true;
            showCaretInText = false;
        }
        if (attachment.title && attachment.fields.length === 0) {
            showCaretInImage = false;
            showCaretInTitle = true;
            showCaretInFooter = false;
            showCaretInAuthor = false;
            showCaretInText = false;
        }
        if (attachment.text && attachment.fields.length === 0) {
            showCaretInImage = false;
            showCaretInTitle = false;
            showCaretInFooter = false;
            showCaretInAuthor = false;
            showCaretInText = true;
        }
        if (attachment.ts || attachment.footer) {
            showCaretInImage = false;
            showCaretInTitle = false;
            showCaretInFooter = true;
            showCaretInAuthor = false;
            showCaretInText = false;
        }
        if (!attachment.image_url) {
            showCaretInImage = false;
            showCaretInTitle = false;
            showCaretInFooter = false;
            showCaretInAuthor = false;
            showCaretInText = false;
        }
        const imageCaretComponent = (React.createElement(ImageCaret_1.default, { size: imageSize, onClick: this.toggleImageCaret, open: this.state.imageCaretOpen, block: showCaretInImage }));
        return (React.createElement("div", { ref: el => {
                this._attachment = el;
            }, className: classnames_1.default('message__attachment', {
                'message__attachment--ephemeral': ephemeral
            }) },
            attachment.pretext && (React.createElement("div", { className: "message__attachment-pretext", dangerouslySetInnerHTML: {
                    __html: attachment.pretext && parser(attachment.pretext)
                } })),
            React.createElement("div", { className: classnames_1.default('message__attachment-content', {
                    hidden: !showAttachment
                }) },
                React.createElement("span", { style: attachment.color.indexOf('#') > -1 ? { backgroundColor: attachment.color } : {}, className: classnames_1.default('message__attachment-spacer', {
                        'message__attachment-spacer--good': attachment.color === 'good',
                        'message__attachment-spacer--warning': attachment.color === 'warning',
                        'message__attachment-spacer--danger': attachment.color === 'danger'
                    }) }),
                React.createElement("div", { className: classnames_1.default('message__attachment-wrap', {
                        'message__attachment-wrap--flush-text': attachment.text.length === 0 && attachment.author_name.length === 0 && attachment.fields.length === 0
                    }) },
                    !!attachment.author_name && (React.createElement(Author_1.default, { name: attachment.author_name, subname: attachment.author_subname, link: attachment.author_link, icon: attachment.author_icon, caret: showCaretInAuthor && imageCaretComponent })),
                    !!attachment.title && (React.createElement(Title_1.default, { title: attachment.title, link: attachment.title_link, caret: showCaretInTitle && imageCaretComponent })),
                    !!attachment.text && (React.createElement("div", { className: "message__attachment-text" },
                        React.createElement("span", { dangerouslySetInnerHTML: { __html: parser(attachment.text) } }),
                        ' ',
                        showCaretInText && imageCaretComponent)),
                    attachment.fields.length > 0 && React.createElement(Fields_1.default, { fields: attachment.fields, parser: parser }),
                    (attachment.ts || attachment.footer) && (React.createElement(Footer_1.default, { icon: attachment.footer_icon, footer: attachment.footer, ts: attachment.ts, parser: parser, caret: showCaretInFooter && imageCaretComponent })),
                    attachment.image_url && (React.createElement(Image_1.default, { url: attachment.image_url, hidden: !this.state.imageCaretOpen, caret: showCaretInImage && imageCaretComponent })),
                    attachment.actions.length > 0 && (React.createElement(Actions_1.default, { attachmentIdx: attachmentId, messageId: messageId, messageIdx: messageIdx, openMenuUpwards: !!openMenuUpwards, 
                        // toggleMessageMenu={this.props.toggleMessageMenu}
                        cancelMessageMenu: false, 
                        // openActionModal={openActionModal}
                        // openDialogModal={openDialogModal}
                        actions: attachment.actions }))),
                attachment.thumb_url ? (React.createElement("div", { className: classnames_1.default('message__attachment-image message__attachment-image--thumb', {
                        hidden: !!attachment.image_url
                    }) }, attachment.title_link ? (React.createElement("a", { href: attachment.title_link, target: "_blank", style: { backgroundImage: `url(${attachment.thumb_url})` }, className: "message__attachment-image-wrap" },
                    React.createElement("img", { src: attachment.thumb_url }))) : (React.createElement("div", { className: "message__attachment-image-wrap", style: { backgroundImage: `url(${attachment.thumb_url})` } },
                    React.createElement("img", { src: attachment.thumb_url }))))) : null)));
    }
    toggleImageCaret(e) {
        this.setState({ imageCaretOpen: !this.state.imageCaretOpen });
    }
    isAttachmentEmpty() {
        // Check to see if an attachment is empty, but removing all "empty-ish"
        // properties, then remove the base ones and see if there are any left.
        const emptyAttachmentCheck = omit_empty_1.default(this.props.attachment);
        delete emptyAttachmentCheck.callback_id;
        delete emptyAttachmentCheck.attachmentId;
        delete emptyAttachmentCheck.attachment_type;
        delete emptyAttachmentCheck.color;
        delete emptyAttachmentCheck.lastUpdated;
        delete emptyAttachmentCheck.mrkdwn_in;
        const isEmpty = !Object.keys(emptyAttachmentCheck).length;
        return isEmpty;
    }
}
exports.default = Attachment;
//# sourceMappingURL=Attachment.js.map