"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SendMessageArea = void 0;
var react_1 = __importStar(require("react"));
var ui_i18n_1 = __importDefault(require("ui-i18n"));
var classnames_1 = __importDefault(require("classnames"));
var ramda_1 = require("ramda");
var Box_1 = __importDefault(require("@mui/material/Box"));
var Button_1 = __importDefault(require("@mui/material/Button"));
var MdmModuleContext_1 = require("../../../contexts/MdmModuleContext");
var CollaborationContext_1 = require("../../../contexts/CollaborationContext");
var helpers_1 = require("../../helpers");
var TextFieldWithMentions_1 = require("../TextFieldWithMentions");
var Avatar_1 = require("../Avatar");
var styles_1 = require("./styles");
var getPlaceholder = function (status) {
    switch (status) {
        case 'open':
            return ui_i18n_1.default.text('Reply...');
        case 'resolved':
            return ui_i18n_1.default.text('Adding a comment will re-open this');
        default:
            return '';
    }
};
var getButtonText = function (status, isEditingComment, isEditingReply) {
    if (isEditingComment || isEditingReply) {
        return ui_i18n_1.default.text('Edit');
    }
    if (status === 'open' || status === 'resolved') {
        return ui_i18n_1.default.text('Reply');
    }
    return ui_i18n_1.default.text('Comment');
};
var getDefaultCommentState = function (_a) {
    var comment = _a.comment, reply = _a.reply, isEditingComment = _a.isEditingComment, isEditingReply = _a.isEditingReply;
    var commentOrReply = isEditingComment ? comment : isEditingReply ? reply : null;
    var _b = commentOrReply || {}, _c = _b.content, plainTextValue = _c === void 0 ? '' : _c, _d = _b.namedUsers, mentions = _d === void 0 ? [] : _d;
    return {
        value: (0, helpers_1.transformPlainTextValueToValueWithMentions)(plainTextValue, mentions),
        plainTextValue: plainTextValue,
        mentions: mentions
    };
};
var SendMessageArea = function (_a) {
    var inputRef = _a.inputRef, className = _a.className, uri = _a.uri, relatedObjectUris = _a.relatedObjectUris, objectType = _a.objectType, popperRef = _a.popperRef, comment = _a.comment, reply = _a.reply, isEditingComment = _a.isEditingComment, isEditingReply = _a.isEditingReply, onCancel = _a.onCancel;
    var isEditing = isEditingComment || isEditingReply;
    var styles = (0, styles_1.useStyles)({ isEditing: isEditing });
    var _b = (0, react_1.useState)(false), focused = _b[0], setFocused = _b[1];
    var _c = (0, react_1.useContext)(CollaborationContext_1.CollaborationContext), createReply = _c.createReply, createComment = _c.createComment, sending = _c.sending, editComment = _c.editComment, editReply = _c.editReply, getCommentState = _c.getCommentState, updateCommentState = _c.updateCommentState, clearCommentState = _c.clearCommentState;
    var username = (0, MdmModuleContext_1.useMdmUsername)();
    var commentUri = (0, helpers_1.createCommentUri)({ comment: comment, reply: reply, isEditingComment: isEditingComment, isEditingReply: isEditingReply });
    var defaultCommentState = getDefaultCommentState({ comment: comment, reply: reply, isEditingComment: isEditingComment, isEditingReply: isEditingReply });
    var _d = getCommentState(uri, commentUri), _e = _d.value, value = _e === void 0 ? defaultCommentState.value : _e, _f = _d.plainTextValue, plainTextValue = _f === void 0 ? defaultCommentState.plainTextValue : _f, _g = _d.mentions, mentions = _g === void 0 ? defaultCommentState.mentions : _g;
    var hasDefaultValue = defaultCommentState.value.trim() !== '';
    var hasValue = value.trim() !== '';
    var shouldShowButtons = focused || hasValue || hasDefaultValue;
    var status = comment === null || comment === void 0 ? void 0 : comment.status;
    var clearValue = function () {
        onCancel === null || onCancel === void 0 ? void 0 : onCancel();
        clearCommentState(uri, commentUri);
    };
    var handleChange = function (value, plainTextValue, mentions) {
        return updateCommentState(uri, commentUri, { value: value, plainTextValue: plainTextValue, mentions: mentions });
    };
    var handleCancel = function () { return clearValue(); };
    var handleFocus = function () { return setFocused(true); };
    var handleBlur = function () { return setFocused(false); };
    var handleSubmit = function (e) {
        e.preventDefault();
        var request = (0, ramda_1.cond)([
            [
                (0, ramda_1.always)(!comment),
                function () {
                    return createComment({
                        content: plainTextValue,
                        namedUsers: mentions,
                        objectType: objectType,
                        uri: uri,
                        relatedObjectUris: relatedObjectUris
                    });
                }
            ],
            [
                (0, ramda_1.always)(isEditingComment),
                function () {
                    return editComment({
                        content: plainTextValue,
                        namedUsers: mentions,
                        commentId: comment.commentId,
                        objectType: objectType,
                        replies: comment.replies,
                        relatedObjectUris: relatedObjectUris,
                        uri: uri
                    });
                }
            ],
            [
                (0, ramda_1.always)(isEditingReply),
                function () {
                    return editReply({
                        content: plainTextValue,
                        namedUsers: mentions,
                        commentId: comment.commentId,
                        replyId: reply.replyId
                    });
                }
            ],
            [
                ramda_1.T,
                function () {
                    return createReply({
                        content: plainTextValue,
                        namedUsers: mentions,
                        commentId: comment.commentId,
                        action: comment.status === 'open' ? 'none' : 'reopen',
                        uri: uri
                    });
                }
            ]
        ]);
        return request().then(clearValue).catch(console.log); //eslint-disable-line no-console
    };
    (0, react_1.useEffect)(function () {
        if (popperRef) {
            popperRef.current.forceUpdate();
        }
    }, [popperRef, shouldShowButtons]);
    var buttonSize = isEditing ? 'small' : 'medium';
    return (react_1.default.createElement(Box_1.default, { className: (0, classnames_1.default)(styles.root, className) },
        react_1.default.createElement("form", { onSubmit: handleSubmit, className: styles.form },
            react_1.default.createElement(Box_1.default, { className: styles.main },
                !isEditing && react_1.default.createElement(Avatar_1.Avatar, { classes: { root: styles.avatar } }, username),
                react_1.default.createElement(TextFieldWithMentions_1.TextFieldWithMentions, { className: styles.textField, disabled: sending, onChange: handleChange, inputRef: inputRef, value: value, onFocus: handleFocus, onBlur: handleBlur, placeholder: getPlaceholder(status) })),
            shouldShowButtons && (react_1.default.createElement(Box_1.default, { className: styles.buttons },
                react_1.default.createElement(Button_1.default, { className: styles.button, variant: "contained", color: "primary", disabled: !hasValue || sending, type: "submit", size: buttonSize }, getButtonText(status, isEditingComment, isEditingReply)),
                react_1.default.createElement(Button_1.default, { disabled: sending, onClick: handleCancel, className: styles.button, variant: "outlined", color: "primary", size: buttonSize }, ui_i18n_1.default.text('Cancel')))))));
};
exports.SendMessageArea = SendMessageArea;
