import { a as __spreadArray, _ as __assign, b as __awaiter, c as __generator } from '../../chunks/bundle-B8Grfnrx.js';
import React__default, { useState, useRef, useEffect } from 'react';
import { f as format } from '../../chunks/bundle-Dy1IZ181.js';
import { R as RemoveMessage } from '../../chunks/bundle-D6CBeFkE.js';
import ParentMessageInfoItem from './ParentMessageInfoItem.js';
import { f as getSenderName } from '../../chunks/bundle-CgLhYjm5.js';
import { g as getIsReactionEnabled } from '../../chunks/bundle-C6Hk5xTT.js';
import { u as useLocalization } from '../../chunks/bundle-l_QLKq1Y.js';
import { u as useUserProfileContext } from '../../chunks/bundle-BYqblmEM.js';
import { S as SuggestedMentionList } from '../../chunks/bundle-WJtiC3BL.js';
import { A as Avatar } from '../../chunks/bundle-DgFzcADJ.js';
import { L as Label, a as LabelTypography, b as LabelColors } from '../../chunks/bundle-HspRRkmy.js';
import FileViewer from '../../ui/FileViewer.js';
import { MessageEmojiMenu } from '../../ui/MessageItemReactionMenu.js';
import { d as MENU_ROOT_ID, E as EMOJI_MENU_ROOT_ID, g as getObservingId, e as MENU_OBSERVING_CLASS_NAME, C as ContextMenu, b as MenuItems } from '../../chunks/bundle-QCH8CZT8.js';
import UserProfile from '../../ui/UserProfile.js';
import MessageInput from '../../ui/MessageInput.js';
import { M as MessageInputKeys } from '../../chunks/bundle-DXneAdAp.js';
import { R as Role } from '../../chunks/bundle-BfEYq-V8.js';
import { u as useMediaQueryContext } from '../../chunks/bundle-yKqHFFfz.js';
import { u as useLongPress } from '../../chunks/bundle-CJmXP56m.js';
import { MobileMenu } from '../../ui/MobileMenu.js';
import { useDirtyGetMentions } from '../../Message/hooks/useDirtyGetMentions.js';
import { g as getCaseResolvedReplyType } from '../../chunks/bundle-B5_aP8LE.js';
import { c as classnames } from '../../chunks/bundle-CDcu1I4f.js';
import { M as MessageMenu } from '../../chunks/bundle-CQAiZVNS.js';
import { u as useElementObserver } from '../../chunks/bundle-CFXaZ31h.js';
import { u as useThread } from '../../chunks/bundle-CVS5Kfg_.js';
import { u as useSendbird } from '../../chunks/bundle-I25ah_Bv.js';
import '../../chunks/bundle-DYBTopZK.js';
import '../../chunks/bundle-BYOddwmS.js';
import '../../chunks/bundle-LnGQMQVt.js';
import 'react-dom';
import '../../ui/IconButton.js';
import '../../ui/Button.js';
import '../../ui/Icon.js';
import '../../chunks/bundle-CTkGIdt8.js';
import '../../chunks/bundle-DDZn5buS.js';
import '../../chunks/bundle-De-3zaYV.js';
import '../../ui/ImageRenderer.js';
import '../../ui/TextButton.js';
import '../../chunks/bundle-DED6q4Ti.js';
import '../../ui/EmojiReactions.js';
import '../../ui/ReactionBadge.js';
import '../../ui/ReactionButton.js';
import '../../ui/BottomSheet.js';
import '../../hooks/useModal.js';
import '../../chunks/bundle-Y2KBbxRr.js';
import '../../chunks/bundle-C4OmfIXk.js';
import '../../chunks/bundle-Ob7Cw3V-.js';
import '@sendbird/chat';
import '@sendbird/chat/groupChannel';
import '@sendbird/chat/openChannel';
import '../../ui/UserListItem.js';
import '../../ui/MutedAvatarOverlay.js';
import '../../ui/Checkbox.js';
import '../../chunks/bundle-C_ngoQD_.js';
import '../../sendbirdSelectors.js';
import '../../chunks/bundle--1cImOGd.js';
import '../../ui/SortByRow.js';
import '../../utils/message/getOutgoingMessageState.js';
import '../../chunks/bundle-BTxw_tIt.js';
import '../../ui/Tooltip.js';
import '../../ui/TooltipWrapper.js';
import '../../Message/context.js';
import '../../ui/VoiceMessageItemBody.js';
import '../../ui/ProgressBar.js';
import '../../VoicePlayer/useVoicePlayer.js';
import '../../chunks/bundle-zOZq2Vpt.js';
import '../../VoiceRecorder/context.js';
import '../../ui/PlaybackTime.js';
import '../../ui/Loader.js';
import '../../chunks/bundle-Do9UFGCD.js';
import '../../chunks/bundle-DT1VcdMs.js';
import '../../ui/MentionLabel.js';
import '../../ui/LinkLabel.js';
import '../../chunks/bundle-BFJ4ctRk.js';
import '@sendbird/chat/message';
import '../../chunks/bundle-CF8V-ET_.js';
import '../../chunks/bundle-BciLDE6M.js';
import '../../chunks/bundle-DgpfIrde.js';
import '@sendbird/uikit-tools';
import '../../chunks/bundle-DwLTEVMK.js';
import 'dompurify';
import '../../chunks/bundle-Dp_oW1ks.js';
import '../../chunks/bundle-Cn2czFA-.js';
import '../../chunks/bundle-4w9Y7uxA.js';
import '../../chunks/bundle-BCGcsRu2.js';
import '../../chunks/bundle-D_tSqvX6.js';
import '../../chunks/bundle-Djq12iSf.js';
import '../context/types.js';
import '../../chunks/bundle-C5Pz6wMm.js';

function ParentMessageInfo(_a) {
    var _this = this;
    var _b, _c, _d, _e, _f, _g;
    var className = _a.className, _h = _a.renderEmojiMenu, renderEmojiMenu = _h === void 0 ? function (props) { return React__default.createElement(MessageEmojiMenu, __assign({}, props)); } : _h, _j = _a.renderMessageMenu, renderMessageMenu = _j === void 0 ? function (props) { return React__default.createElement(MessageMenu, __assign({}, props)); } : _j;
    var _k = useSendbird().state, stores = _k.stores, config = _k.config;
    var isOnline = config.isOnline, userMention = config.userMention, logger = config.logger, groupChannel = config.groupChannel;
    var userId = (_c = (_b = stores.userStore.user) === null || _b === void 0 ? void 0 : _b.userId) !== null && _c !== void 0 ? _c : '';
    var _l = useLocalization(), dateLocale = _l.dateLocale, stringSet = _l.stringSet;
    var _m = useThread(), _o = _m.state, currentChannel = _o.currentChannel, parentMessage = _o.parentMessage, allThreadMessages = _o.allThreadMessages, emojiContainer = _o.emojiContainer, onMoveToParentMessage = _o.onMoveToParentMessage, onHeaderActionClick = _o.onHeaderActionClick, isMuted = _o.isMuted, isChannelFrozen = _o.isChannelFrozen, onBeforeDownloadFileMessage = _o.onBeforeDownloadFileMessage, filterEmojiCategoryIds = _o.filterEmojiCategoryIds, _p = _m.actions, toggleReaction = _p.toggleReaction, updateMessage = _p.updateMessage, deleteMessage = _p.deleteMessage;
    var isMobile = useMediaQueryContext().isMobile;
    var isMenuMounted = useElementObserver("#".concat(getObservingId(parentMessage.messageId), ".").concat(MENU_OBSERVING_CLASS_NAME), [
        document.getElementById(MENU_ROOT_ID),
        document.getElementById(EMOJI_MENU_ROOT_ID),
    ]);
    var _q = useState(false), showRemove = _q[0], setShowRemove = _q[1];
    var _r = useState(false), showFileViewer = _r[0], setShowFileViewer = _r[1];
    var isReactionEnabled = getIsReactionEnabled({
        channel: currentChannel,
        config: config,
    });
    var isMentionEnabled = groupChannel.enableMention;
    var replyType = getCaseResolvedReplyType(groupChannel.replyType).upperCase;
    var isByMe = userId === parentMessage.sender.userId;
    // Mobile
    var mobileMenuRef = useRef(null);
    var _s = useState(false), showMobileMenu = _s[0], setShowMobileMenu = _s[1];
    var longPress = useLongPress({
        onLongPress: function () {
            if (isMobile) {
                setShowMobileMenu(true);
            }
        },
    }, {
        shouldPreventDefault: false,
    });
    // Edit message
    var _t = useState(false), showEditInput = _t[0], setShowEditInput = _t[1];
    var disabled = !isOnline || isMuted || isChannelFrozen && !((currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.myRole) === Role.OPERATOR);
    // Mention
    var editMessageInputRef = useRef(null);
    var _u = useState(''), mentionNickname = _u[0], setMentionNickname = _u[1];
    var _v = useState([]), mentionedUsers = _v[0], setMentionedUsers = _v[1];
    var _w = useState([]), mentionedUserIds = _w[0], setMentionedUserIds = _w[1];
    var _x = useState(null), messageInputEvent = _x[0], setMessageInputEvent = _x[1];
    var _y = useState(null), selectedUser = _y[0], setSelectedUser = _y[1];
    var _z = useState([]), mentionSuggestedUsers = _z[0], setMentionSuggestedUsers = _z[1];
    var displaySuggestedMentionList = isOnline
        && isMentionEnabled
        && mentionNickname.length > 0
        && !isMuted
        && !(isChannelFrozen && !(currentChannel.myRole === Role.OPERATOR));
    var mentionNodes = useDirtyGetMentions({ ref: editMessageInputRef }, { logger: logger });
    var ableMention = (mentionNodes === null || mentionNodes === void 0 ? void 0 : mentionNodes.length) < (userMention === null || userMention === void 0 ? void 0 : userMention.maxMentionCount);
    useEffect(function () {
        setMentionedUsers(mentionedUsers.filter(function (_a) {
            var userId = _a.userId;
            var i = mentionedUserIds.indexOf(userId);
            if (i < 0) {
                return false;
            }
            else {
                mentionedUserIds.splice(i, 1);
                return true;
            }
        }));
    }, [mentionedUserIds]);
    var handleOnDownloadClick = function (e) { return __awaiter(_this, void 0, void 0, function () {
        var allowDownload, err_1;
        var _a, _b;
        return __generator(this, function (_c) {
            switch (_c.label) {
                case 0:
                    if (!onBeforeDownloadFileMessage)
                        return [2 /*return*/];
                    _c.label = 1;
                case 1:
                    _c.trys.push([1, 3, , 4]);
                    return [4 /*yield*/, onBeforeDownloadFileMessage({ message: parentMessage })];
                case 2:
                    allowDownload = _c.sent();
                    if (!allowDownload) {
                        e.preventDefault();
                        (_a = logger === null || logger === void 0 ? void 0 : logger.info) === null || _a === void 0 ? void 0 : _a.call(logger, 'ParentMessageInfo: Not allowed to download.');
                    }
                    return [3 /*break*/, 4];
                case 3:
                    err_1 = _c.sent();
                    (_b = logger === null || logger === void 0 ? void 0 : logger.error) === null || _b === void 0 ? void 0 : _b.call(logger, 'ParentMessageInfo: Error occurred while determining download continuation:', err_1);
                    return [3 /*break*/, 4];
                case 4: return [2 /*return*/];
            }
        });
    }); };
    // User Profile
    var avatarRef = useRef(null);
    var _0 = useUserProfileContext(), disableUserProfile = _0.disableUserProfile, renderUserProfile = _0.renderUserProfile;
    if (showEditInput && ((_d = parentMessage === null || parentMessage === void 0 ? void 0 : parentMessage.isUserMessage) === null || _d === void 0 ? void 0 : _d.call(parentMessage))) {
        return (React__default.createElement(React__default.Fragment, null,
            displaySuggestedMentionList && (React__default.createElement(SuggestedMentionList, { className: "parent-message-info--suggested-mention-list", targetNickname: mentionNickname, inputEvent: messageInputEvent !== null && messageInputEvent !== void 0 ? messageInputEvent : undefined, 
                // renderUserMentionItem={renderUserMentionItem}
                onUserItemClick: function (user) {
                    if (user) {
                        setMentionedUsers(__spreadArray(__spreadArray([], mentionedUsers, true), [user], false));
                    }
                    setMentionNickname('');
                    setSelectedUser(user);
                    setMessageInputEvent(null);
                }, onFocusItemChange: function () {
                    setMessageInputEvent(null);
                }, onFetchUsers: function (users) {
                    setMentionSuggestedUsers(users);
                }, ableAddMention: ableMention, maxMentionCount: userMention === null || userMention === void 0 ? void 0 : userMention.maxMentionCount, maxSuggestionCount: userMention === null || userMention === void 0 ? void 0 : userMention.maxSuggestionCount })),
            React__default.createElement(MessageInput, { channel: currentChannel, isEdit: true, disabled: disabled, ref: editMessageInputRef, mentionSelectedUser: selectedUser, isMentionEnabled: isMentionEnabled, message: parentMessage, onStartTyping: function () {
                    var _a;
                    (_a = currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.startTyping) === null || _a === void 0 ? void 0 : _a.call(currentChannel);
                }, onUpdateMessage: function (_a) {
                    var _b;
                    var messageId = _a.messageId, message = _a.message, mentionTemplate = _a.mentionTemplate;
                    updateMessage({
                        messageId: messageId,
                        message: message,
                        mentionedUsers: mentionedUsers,
                        mentionTemplate: mentionTemplate,
                    });
                    setShowEditInput(false);
                    (_b = currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.endTyping) === null || _b === void 0 ? void 0 : _b.call(currentChannel);
                }, onCancelEdit: function () {
                    var _a;
                    setMentionNickname('');
                    setMentionedUsers([]);
                    setMentionedUserIds([]);
                    setMentionSuggestedUsers([]);
                    setShowEditInput(false);
                    (_a = currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.endTyping) === null || _a === void 0 ? void 0 : _a.call(currentChannel);
                }, onUserMentioned: function (user) {
                    if ((selectedUser === null || selectedUser === void 0 ? void 0 : selectedUser.userId) === (user === null || user === void 0 ? void 0 : user.userId)) {
                        setSelectedUser(null);
                        setMentionNickname('');
                    }
                }, onMentionStringChange: function (mentionText) {
                    setMentionNickname(mentionText);
                }, onMentionedUserIdsUpdated: function (userIds) {
                    setMentionedUserIds(userIds);
                }, onKeyDown: function (e) {
                    if (displaySuggestedMentionList && (mentionSuggestedUsers === null || mentionSuggestedUsers === void 0 ? void 0 : mentionSuggestedUsers.length) > 0
                        && ((e.key === MessageInputKeys.Enter && ableMention) || e.key === MessageInputKeys.ArrowUp || e.key === MessageInputKeys.ArrowDown)) {
                        setMessageInputEvent(e);
                        return true;
                    }
                    return false;
                } })));
    }
    return (React__default.createElement("div", __assign({ className: "sendbird-parent-message-info ".concat(className) }, (isMobile) ? __assign({}, longPress) : {}, { ref: mobileMenuRef }),
        React__default.createElement(ContextMenu, { menuTrigger: function (toggleDropdown) {
                var _a, _b, _c;
                return (React__default.createElement(Avatar, { className: "sendbird-parent-message-info__sender", ref: avatarRef, src: ((_b = (_a = currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.members) === null || _a === void 0 ? void 0 : _a.find(function (m) { var _a; return (m === null || m === void 0 ? void 0 : m.userId) === ((_a = parentMessage === null || parentMessage === void 0 ? void 0 : parentMessage.sender) === null || _a === void 0 ? void 0 : _a.userId); })) === null || _b === void 0 ? void 0 : _b.profileUrl)
                        || ((_c = parentMessage === null || parentMessage === void 0 ? void 0 : parentMessage.sender) === null || _c === void 0 ? void 0 : _c.profileUrl), alt: "thread message sender", width: "40px", height: "40px", onClick: function () {
                        if (!disableUserProfile) {
                            toggleDropdown();
                        }
                    } }));
            }, menuItems: function (closeDropdown) { return (renderUserProfile
                ? renderUserProfile({
                    user: parentMessage === null || parentMessage === void 0 ? void 0 : parentMessage.sender,
                    close: closeDropdown,
                    currentUserId: userId,
                    avatarRef: avatarRef,
                })
                : (React__default.createElement(MenuItems, { parentRef: avatarRef, parentContainRef: avatarRef, closeDropdown: closeDropdown, style: { paddingTop: '0px', paddingBottom: '0px' } },
                    React__default.createElement(UserProfile, { user: parentMessage === null || parentMessage === void 0 ? void 0 : parentMessage.sender, currentUserId: userId, onSuccess: closeDropdown })))); } }),
        React__default.createElement("div", { className: "sendbird-parent-message-info__content" },
            React__default.createElement("div", { className: "sendbird-parent-message-info__content__info" },
                React__default.createElement(Label, { className: isReactionEnabled
                        ? 'sendbird-parent-message-info__content__info__sender-name--use-reaction'
                        : 'sendbird-parent-message-info__content__info__sender-name', type: LabelTypography.CAPTION_2, color: LabelColors.ONBACKGROUND_2 }, ((_f = (_e = currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.members) === null || _e === void 0 ? void 0 : _e.find(function (member) { var _a; return (member === null || member === void 0 ? void 0 : member.userId) === ((_a = parentMessage === null || parentMessage === void 0 ? void 0 : parentMessage.sender) === null || _a === void 0 ? void 0 : _a.userId); })) === null || _f === void 0 ? void 0 : _f.nickname) || (getSenderName === null || getSenderName === void 0 ? void 0 : getSenderName(parentMessage))),
                React__default.createElement(Label, { className: "sendbird-parent-message-info__content__info__sent-at", type: LabelTypography.CAPTION_3, color: LabelColors.ONBACKGROUND_2 }, format((parentMessage === null || parentMessage === void 0 ? void 0 : parentMessage.createdAt) || 0, stringSet.DATE_FORMAT__MESSAGE_CREATED_AT, { locale: dateLocale }))),
            React__default.createElement(ParentMessageInfoItem, { message: parentMessage, showFileViewer: setShowFileViewer, onBeforeDownloadFileMessage: onBeforeDownloadFileMessage })),
        !isMobile && (React__default.createElement("div", { className: 'sendbird-parent-message-info__menu-container' },
            renderMessageMenu({
                className: classnames('sendbird-parent-message-info__context-menu', isReactionEnabled && 'use-reaction', isMenuMounted && 'sendbird-mouse-hover'),
                channel: currentChannel,
                message: parentMessage,
                isByMe: userId === ((_g = parentMessage === null || parentMessage === void 0 ? void 0 : parentMessage.sender) === null || _g === void 0 ? void 0 : _g.userId),
                disableDeleteMessage: allThreadMessages.length > 0,
                replyType: replyType,
                showEdit: setShowEditInput,
                showRemove: setShowRemove,
                onMoveToParentMessage: function () {
                    onMoveToParentMessage === null || onMoveToParentMessage === void 0 ? void 0 : onMoveToParentMessage({ message: parentMessage, channel: currentChannel });
                },
                deleteMessage: deleteMessage,
            }),
            isReactionEnabled && (renderEmojiMenu({
                className: classnames('sendbird-parent-message-info__reaction-menu', isMenuMounted && 'sendbird-mouse-hover'),
                message: parentMessage,
                userId: userId,
                emojiContainer: emojiContainer,
                toggleReaction: toggleReaction,
                filterEmojiCategoryIds: filterEmojiCategoryIds,
            })))),
        showRemove && (React__default.createElement(RemoveMessage, { onCancel: function () { return setShowRemove(false); }, onSubmit: function () {
                onHeaderActionClick === null || onHeaderActionClick === void 0 ? void 0 : onHeaderActionClick();
            }, message: parentMessage })),
        showFileViewer && (React__default.createElement(FileViewer, { message: parentMessage, onClose: function () { return setShowFileViewer(false); }, onDelete: function () {
                deleteMessage(parentMessage)
                    .then(function () {
                    setShowFileViewer(false);
                });
            }, onDownloadClick: handleOnDownloadClick })),
        showMobileMenu && (React__default.createElement(MobileMenu, { parentRef: mobileMenuRef, channel: currentChannel, message: parentMessage, userId: userId, replyType: replyType, hideMenu: function () {
                setShowMobileMenu(false);
            }, deleteMessage: deleteMessage, deleteMenuState: (allThreadMessages === null || allThreadMessages === void 0 ? void 0 : allThreadMessages.length) === 0
                ? 'ACTIVE'
                : 'HIDE', isReactionEnabled: isReactionEnabled, isByMe: isByMe, emojiContainer: emojiContainer, showEdit: setShowEditInput, showRemove: setShowRemove, toggleReaction: toggleReaction, isOpenedFromThread: true, onDownloadClick: handleOnDownloadClick }))));
}

export { ParentMessageInfo as default };
//# sourceMappingURL=ParentMessageInfo.js.map
