import React__default, { useEffect, useState, useContext } from 'react';
import { useSendbirdStateContext } from '../useSendbirdStateContext.js';
import { u as useLocalization } from './bundle-BBjosDA0.js';
import { u as useChannelListContext } from './bundle-otzUyFE_.js';
import { g as getTotalMembers, M as MessageStatus, a as getLastMessageCreatedAt, b as getLastMessage, c as getChannelUnreadMessageCount, d as getChannelTitle } from './bundle-J3DrW9X2.js';
import { _ as __assign } from './bundle-BA7tv0JB.js';
import { GroupChannelFilter } from '@sendbird/chat/groupChannel';
import { useGroupChannelList, useGroupChannelHandler } from '@sendbird/uikit-tools';
import { U as UserProfileProvider } from './bundle-vOT5acXc.js';
import { u as useOnlineStatus, a as useMarkAsDeliveredScheduler } from './bundle-bjEEGmYA.js';
import { n as noop } from './bundle-cA_r9HRx.js';
import { u as useLongPress } from './bundle-0jbuC1o3.js';
import { u as useMediaQueryContext } from './bundle-r9j_eCHc.js';
import { i as isVoiceMessage } from './bundle-N9hMnIRl.js';
import { a as TypingIndicatorText } from './bundle-uIetzCLw.js';
import Badge from '../ui/Badge.js';
import ChannelAvatar from '../ui/ChannelAvatar.js';
import Icon, { IconTypes, IconColors } from '../ui/Icon.js';
import { L as Label, a as LabelTypography, b as LabelColors } from './bundle-I8JFwttJ.js';
import MentionUserLabel from '../ui/MentionUserLabel.js';
import { M as Modal } from './bundle-cvTUkQV_.js';
import TextButton from '../ui/TextButton.js';

var GroupChannelListContext = React__default.createContext(null);
var GroupChannelListProvider = function (props) {
    var 
    // Default
    children = props.children, _a = props.className, className = _a === void 0 ? '' : _a, selectedChannelUrl = props.selectedChannelUrl, 
    // Flags
    _b = props.allowProfileEdit, 
    // Flags
    allowProfileEdit = _b === void 0 ? true : _b, _c = props.disableAutoSelect, disableAutoSelect = _c === void 0 ? false : _c, _d = props.isTypingIndicatorEnabled, isTypingIndicatorEnabled = _d === void 0 ? false : _d, _e = props.isMessageReceiptStatusEnabled, isMessageReceiptStatusEnabled = _e === void 0 ? false : _e, 
    // Custom
    channelListQueryParams = props.channelListQueryParams, onThemeChange = props.onThemeChange, _f = props.onChannelSelect, onChannelSelect = _f === void 0 ? noop : _f, _g = props.onChannelCreated, onChannelCreated = _g === void 0 ? noop : _g, onCreateChannelClick = props.onCreateChannelClick, onBeforeCreateChannel = props.onBeforeCreateChannel, onUserProfileUpdated = props.onUserProfileUpdated, 
    // UserProfile
    disableUserProfile = props.disableUserProfile, renderUserProfile = props.renderUserProfile, onUserProfileMessage = props.onUserProfileMessage;
    var globalStore = useSendbirdStateContext();
    var config = globalStore.config, stores = globalStore.stores;
    var sdkStore = stores.sdkStore;
    var _h = config.isTypingIndicatorEnabledOnChannelList, isTypingIndicatorEnabledOnChannelList = _h === void 0 ? false : _h, _j = config.isMessageReceiptStatusEnabledOnChannelList, isMessageReceiptStatusEnabledOnChannelList = _j === void 0 ? false : _j;
    var sdk = sdkStore.sdk;
    var isConnected = useOnlineStatus(sdk, config.logger);
    var scheduler = useMarkAsDeliveredScheduler({ isConnected: isConnected }, config);
    var channelListDataSource = useGroupChannelList(sdk, {
        collectionCreator: getCollectionCreator(sdk, channelListQueryParams),
        markAsDelivered: function (channels) { return channels.forEach(scheduler.push); },
        onChannelsDeleted: function (channelUrls) {
            channelUrls.forEach(function (url) {
                if (url === selectedChannelUrl)
                    onChannelSelect(null);
            });
        },
    });
    var refreshing = channelListDataSource.refreshing, initialized = channelListDataSource.initialized, groupChannels = channelListDataSource.groupChannels, refresh = channelListDataSource.refresh, loadMore = channelListDataSource.loadMore;
    // SideEffect: Auto select channel
    useEffect(function () {
        var _a;
        if (!disableAutoSelect && stores.sdkStore.initialized && initialized) {
            if (!selectedChannelUrl)
                onChannelSelect((_a = groupChannels[0]) !== null && _a !== void 0 ? _a : null);
        }
    }, [disableAutoSelect, stores.sdkStore.initialized, initialized, selectedChannelUrl]);
    var _k = useState([]), typingChannelUrls = _k[0], setTypingChannelUrls = _k[1];
    useGroupChannelHandler(sdk, {
        onTypingStatusUpdated: function (channel) {
            var _a;
            var channelList = typingChannelUrls.filter(function (channelUrl) { return channelUrl !== channel.url; });
            if (((_a = channel.getTypingUsers()) === null || _a === void 0 ? void 0 : _a.length) > 0) {
                setTypingChannelUrls(channelList.concat(channel.url));
            }
            else {
                setTypingChannelUrls(channelList);
            }
        },
    });
    return (React__default.createElement(GroupChannelListContext.Provider, { value: {
            // Default
            className: className,
            selectedChannelUrl: selectedChannelUrl,
            // Flags
            allowProfileEdit: allowProfileEdit !== null && allowProfileEdit !== void 0 ? allowProfileEdit : config === null || config === void 0 ? void 0 : config.allowProfileEdit,
            disableAutoSelect: disableAutoSelect,
            isTypingIndicatorEnabled: isTypingIndicatorEnabled !== null && isTypingIndicatorEnabled !== void 0 ? isTypingIndicatorEnabled : isTypingIndicatorEnabledOnChannelList,
            isMessageReceiptStatusEnabled: isMessageReceiptStatusEnabled !== null && isMessageReceiptStatusEnabled !== void 0 ? isMessageReceiptStatusEnabled : isMessageReceiptStatusEnabledOnChannelList,
            // Essential
            onChannelSelect: onChannelSelect,
            onChannelCreated: onChannelCreated,
            // Partial props
            onThemeChange: onThemeChange,
            onCreateChannelClick: onCreateChannelClick,
            onBeforeCreateChannel: onBeforeCreateChannel,
            onUserProfileUpdated: onUserProfileUpdated,
            // Internal
            typingChannelUrls: typingChannelUrls,
            // ReturnType<UseGroupChannelList>
            refreshing: refreshing,
            initialized: initialized,
            groupChannels: groupChannels,
            refresh: refresh,
            loadMore: loadMore,
        } },
        React__default.createElement(UserProfileProvider, { disableUserProfile: disableUserProfile !== null && disableUserProfile !== void 0 ? disableUserProfile : config === null || config === void 0 ? void 0 : config.disableUserProfile, renderUserProfile: renderUserProfile !== null && renderUserProfile !== void 0 ? renderUserProfile : config === null || config === void 0 ? void 0 : config.renderUserProfile, onUserProfileMessage: onUserProfileMessage !== null && onUserProfileMessage !== void 0 ? onUserProfileMessage : config === null || config === void 0 ? void 0 : config.onUserProfileMessage },
            React__default.createElement("div", { className: "sendbird-channel-list ".concat(className) }, children))));
};
var useGroupChannelListContext = function () {
    var context = useContext(GroupChannelListContext);
    if (!context)
        throw new Error('GroupChannelListContext not found. Use within the GroupChannelList module.');
    return context;
};
function getCollectionCreator(sdk, channelListQueryParams) {
    return function (defaultParams) {
        var params = __assign(__assign({}, defaultParams), channelListQueryParams);
        return sdk.groupChannel.createGroupChannelCollection(__assign(__assign({}, params), { filter: new GroupChannelFilter(params) }));
    };
}

var GroupChannelListItemView = function (_a) {
    var channel = _a.channel, tabIndex = _a.tabIndex, isTyping = _a.isTyping, isSelected = _a.isSelected, channelName = _a.channelName, _b = _a.isMessageStatusEnabled, isMessageStatusEnabled = _b === void 0 ? true : _b, _c = _a.onClick, onClick = _c === void 0 ? noop : _c, _d = _a.onLeaveChannel, onLeaveChannel = _d === void 0 ? noop : _d, renderChannelAction = _a.renderChannelAction;
    var config = useSendbirdStateContext().config;
    var theme = config.theme, isMentionEnabled = config.isMentionEnabled, userId = config.userId;
    var _e = useLocalization(), dateLocale = _e.dateLocale, stringSet = _e.stringSet;
    var isMobile = useMediaQueryContext().isMobile;
    var _f = useState(false), showMobileLeave = _f[0], setShowMobileLeave = _f[1];
    var onLongPress = useLongPress({
        onLongPress: function () {
            if (isMobile) {
                setShowMobileLeave(true);
            }
        },
        onClick: onClick,
    }, {
        delay: 1000,
    });
    return (React__default.createElement(React__default.Fragment, null,
        React__default.createElement("div", __assign({ className: [
                'sendbird-channel-preview',
                isSelected ? 'sendbird-channel-preview--active' : '',
            ].join(' '), role: "link", tabIndex: tabIndex }, (isMobile ? __assign({}, onLongPress) : { onClick: onClick })),
            React__default.createElement("div", { className: "sendbird-channel-preview__avatar" },
                React__default.createElement(ChannelAvatar, { channel: channel, userId: userId, theme: theme })),
            React__default.createElement("div", { className: "sendbird-channel-preview__content" },
                React__default.createElement("div", { className: "sendbird-channel-preview__content__upper" },
                    React__default.createElement("div", { className: "sendbird-channel-preview__content__upper__header" },
                        (channel.isBroadcast || false) && (React__default.createElement("div", { className: "sendbird-channel-preview__content__upper__header__broadcast-icon" },
                            React__default.createElement(Icon, { type: IconTypes.BROADCAST, fillColor: IconColors.SECONDARY, height: "16px", width: "16px" }))),
                        React__default.createElement(Label, { className: "sendbird-channel-preview__content__upper__header__channel-name", type: LabelTypography.SUBTITLE_2, color: LabelColors.ONBACKGROUND_1 }, channelName),
                        React__default.createElement(Label, { className: "sendbird-channel-preview__content__upper__header__total-members", type: LabelTypography.CAPTION_2, color: LabelColors.ONBACKGROUND_2 }, getTotalMembers(channel)),
                        (channel.isFrozen) && (React__default.createElement("div", { title: "Frozen", className: "sendbird-channel-preview__content__upper__header__frozen-icon" },
                            React__default.createElement(Icon, { type: IconTypes.FREEZE, fillColor: IconColors.PRIMARY, height: 12, width: 12 })))),
                    !channel.isEphemeral && isMessageStatusEnabled && (React__default.createElement(MessageStatus, { className: "sendbird-channel-preview__content__upper__last-message-at", channel: channel, message: channel.lastMessage, isDateSeparatorConsidered: false })),
                    !channel.isEphemeral && !isMessageStatusEnabled && (React__default.createElement(Label, { className: "sendbird-channel-preview__content__upper__last-message-at", type: LabelTypography.CAPTION_3, color: LabelColors.ONBACKGROUND_2 }, getLastMessageCreatedAt({
                        channel: channel,
                        locale: dateLocale,
                        stringSet: stringSet,
                    })))),
                React__default.createElement("div", { className: "sendbird-channel-preview__content__lower" },
                    React__default.createElement(Label, { className: "sendbird-channel-preview__content__lower__last-message", type: LabelTypography.BODY_2, color: LabelColors.ONBACKGROUND_3 },
                        isTyping && (React__default.createElement(TypingIndicatorText, { members: channel.getTypingUsers() })),
                        !isTyping
                            && !isVoiceMessage(channel.lastMessage)
                            && getLastMessage(channel, stringSet),
                        !isTyping
                            && isVoiceMessage(channel.lastMessage)
                            && stringSet.VOICE_MESSAGE),
                    /**
                     * Do not show unread count for focused channel. This is because of the limitation where
                     * isScrollBottom and hasNext states needs to be added globally but they are from channel context
                     * so channel list cannot see them with the current architecture.
                     */
                    !isSelected && !channel.isEphemeral && (React__default.createElement("div", { className: "sendbird-channel-preview__content__lower__unread-message-count" },
                        isMentionEnabled && channel.unreadMentionCount > 0 ? (React__default.createElement(MentionUserLabel, { className: "sendbird-channel-preview__content__lower__unread-message-count__mention", color: "purple" }, '@')) : null,
                        getChannelUnreadMessageCount(channel) ? ( // return number
                        React__default.createElement(Badge, { count: getChannelUnreadMessageCount(channel) })) : null)))),
            !isMobile && (React__default.createElement("div", { className: "sendbird-channel-preview__action" }, renderChannelAction({ channel: channel })))),
        showMobileLeave && isMobile && (React__default.createElement(Modal, { className: "sendbird-channel-preview__leave--mobile", titleText: channelName, hideFooter: true, isCloseOnClickOutside: true, onCancel: function () { return setShowMobileLeave(false); } },
            React__default.createElement(TextButton, { onClick: function () {
                    onLeaveChannel();
                    setShowMobileLeave(false);
                }, className: "sendbird-channel-preview__leave-label--mobile" },
                React__default.createElement(Label, { type: LabelTypography.SUBTITLE_1, color: LabelColors.ONBACKGROUND_1 }, stringSet.CHANNEL_PREVIEW_MOBILE_LEAVE))))));
};

var ChannelPreview = function (_a) {
    var _b, _c, _d, _e, _f, _g, _h;
    var channel = _a.channel, _j = _a.isActive, isActive = _j === void 0 ? false : _j, _k = _a.isTyping, isTyping = _k === void 0 ? false : _k, renderChannelAction = _a.renderChannelAction, onLeaveChannel = _a.onLeaveChannel, onClick = _a.onClick, tabIndex = _a.tabIndex;
    var sbState = useSendbirdStateContext();
    var _l = useChannelListContext(), _m = _l.isTypingIndicatorEnabled, isTypingIndicatorEnabled = _m === void 0 ? false : _m, _o = _l.isMessageReceiptStatusEnabled, isMessageReceiptStatusEnabled = _o === void 0 ? false : _o;
    var stringSet = useLocalization().stringSet;
    var userId = (_d = (_c = (_b = sbState === null || sbState === void 0 ? void 0 : sbState.stores) === null || _b === void 0 ? void 0 : _b.userStore) === null || _c === void 0 ? void 0 : _c.user) === null || _d === void 0 ? void 0 : _d.userId;
    var isMessageStatusEnabled = isMessageReceiptStatusEnabled
        && (((_e = channel === null || channel === void 0 ? void 0 : channel.lastMessage) === null || _e === void 0 ? void 0 : _e.messageType) === 'user' || ((_f = channel === null || channel === void 0 ? void 0 : channel.lastMessage) === null || _f === void 0 ? void 0 : _f.messageType) === 'file')
        && ((_h = (_g = channel === null || channel === void 0 ? void 0 : channel.lastMessage) === null || _g === void 0 ? void 0 : _g.sender) === null || _h === void 0 ? void 0 : _h.userId) === userId;
    var channelName = getChannelTitle(channel, userId, stringSet);
    return (React__default.createElement(GroupChannelListItemView, { channel: channel, tabIndex: tabIndex, isTyping: isTypingIndicatorEnabled && isTyping, isSelected: isActive, channelName: channelName, isMessageStatusEnabled: isMessageStatusEnabled, onClick: onClick, onLeaveChannel: onLeaveChannel, renderChannelAction: renderChannelAction }));
};

export { ChannelPreview as C, GroupChannelListItemView as G, GroupChannelListProvider as a, useGroupChannelListContext as u };
//# sourceMappingURL=bundle-Wzk9dnsn.js.map
