import React__default, { useContext } from 'react';
import { L as LocalizationContext } from '../../chunks/bundle-l_QLKq1Y.js';
import { u as useMessageSearch } from '../../chunks/bundle-CinANGYQ.js';
import MessageSearchItem from '../../ui/MessageSearchItem.js';
import PlaceHolder, { PlaceHolderTypes } from '../../ui/PlaceHolder.js';
import MessageSearchFileItem from '../../ui/MessageSearchFileItem.js';
import { a as isDefaultChannelName } from '../../chunks/bundle-CgLhYjm5.js';
import '../../chunks/bundle-B8Grfnrx.js';
import '../../chunks/bundle-Y2KBbxRr.js';
import '../../chunks/bundle-BYOddwmS.js';
import '../../chunks/bundle-C4OmfIXk.js';
import '../../chunks/bundle-Ob7Cw3V-.js';
import '../../chunks/bundle-I25ah_Bv.js';
import '@sendbird/chat';
import '@sendbird/chat/groupChannel';
import '@sendbird/chat/openChannel';
import '../../chunks/bundle-Dy1IZ181.js';
import '../../chunks/bundle-DYBTopZK.js';
import '../../chunks/bundle-Dhf37wzM.js';
import '../../chunks/bundle-C8TP-mjp.js';
import '../../chunks/bundle-DgFzcADJ.js';
import '../../ui/ImageRenderer.js';
import '../../chunks/bundle-CDcu1I4f.js';
import '../../chunks/bundle-C_ngoQD_.js';
import '../../ui/Icon.js';
import '../../chunks/bundle-HspRRkmy.js';
import '../../ui/Loader.js';
import '../../chunks/bundle-tFnb24Id.js';
import '../../chunks/bundle-De-3zaYV.js';
import '../../utils/message/getOutgoingMessageState.js';
import '../../chunks/bundle-BTxw_tIt.js';

var MessageSearchUI = function (_a) {
    var renderPlaceHolderError = _a.renderPlaceHolderError, renderPlaceHolderLoading = _a.renderPlaceHolderLoading, renderPlaceHolderNoString = _a.renderPlaceHolderNoString, renderPlaceHolderEmptyList = _a.renderPlaceHolderEmptyList, renderSearchItem = _a.renderSearchItem;
    var _b = useMessageSearch(), _c = _b.state, isInvalid = _c.isInvalid, searchString = _c.searchString, requestString = _c.requestString, currentChannel = _c.currentChannel, loading = _c.loading, scrollRef = _c.scrollRef, hasMoreResult = _c.hasMoreResult, onScroll = _c.onScroll, allMessages = _c.allMessages, onResultClick = _c.onResultClick, selectedMessageId = _c.selectedMessageId, _d = _b.actions, setSelectedMessageId = _d.setSelectedMessageId, handleRetryToConnect = _d.handleRetryToConnect;
    var stringSet = useContext(LocalizationContext).stringSet;
    var handleOnScroll = function (e) {
        var scrollElement = e.target;
        var scrollTop = scrollElement.scrollTop, scrollHeight = scrollElement.scrollHeight, clientHeight = scrollElement.clientHeight;
        if (!hasMoreResult) {
            return;
        }
        if (scrollTop + clientHeight >= scrollHeight - 1) {
            onScroll(function () {
                // after load more searched messages
            });
        }
    };
    var getChannelName = function () {
        if (!(currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.name) && !(currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.members)) {
            return stringSet.NO_TITLE;
        }
        if (isDefaultChannelName(currentChannel))
            return currentChannel.members.map(function (member) { return member.nickname || stringSet.NO_NAME; }).join(', ');
        return currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.name;
    };
    if (isInvalid && searchString && requestString) {
        return (renderPlaceHolderError === null || renderPlaceHolderError === void 0 ? void 0 : renderPlaceHolderError()) || (React__default.createElement("div", { className: "sendbird-message-search" },
            React__default.createElement(PlaceHolder, { type: PlaceHolderTypes.WRONG, retryToConnect: handleRetryToConnect })));
    }
    if (loading && searchString && requestString) {
        return (renderPlaceHolderLoading === null || renderPlaceHolderLoading === void 0 ? void 0 : renderPlaceHolderLoading()) || (React__default.createElement("div", { className: "sendbird-message-search" },
            React__default.createElement(PlaceHolder, { type: PlaceHolderTypes.SEARCHING })));
    }
    if (!searchString) {
        return (renderPlaceHolderNoString === null || renderPlaceHolderNoString === void 0 ? void 0 : renderPlaceHolderNoString()) || (React__default.createElement("div", { className: "sendbird-message-search" },
            React__default.createElement(PlaceHolder, { type: PlaceHolderTypes.SEARCH_IN, searchInString: getChannelName() })));
    }
    return (React__default.createElement("div", { className: "sendbird-message-search", onScroll: handleOnScroll, ref: scrollRef }, (allMessages.length > 0)
        ? (allMessages.map(function (message) {
            if (renderSearchItem) {
                return renderSearchItem({ message: message, onResultClick: onResultClick });
            }
            if (message.messageType === 'file') {
                return (React__default.createElement(MessageSearchFileItem, { className: "sendbird-message-search__message-search-item", message: message, key: message.messageId, selected: (selectedMessageId === message.messageId), onClick: function () {
                        onResultClick === null || onResultClick === void 0 ? void 0 : onResultClick(message);
                        setSelectedMessageId(message.messageId);
                    } }));
            }
            return (React__default.createElement(MessageSearchItem, { className: "sendbird-message-search__message-search-item", message: message, key: message.messageId, selected: (selectedMessageId === message.messageId), onClick: function () {
                    onResultClick === null || onResultClick === void 0 ? void 0 : onResultClick(message);
                    setSelectedMessageId(message.messageId);
                } }));
        }))
        : (renderPlaceHolderEmptyList === null || renderPlaceHolderEmptyList === void 0 ? void 0 : renderPlaceHolderEmptyList()) || (React__default.createElement(PlaceHolder, { type: PlaceHolderTypes.NO_RESULTS }))));
};

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